DATEPART(年,...)与年度(...)(Datepart(year, …) vs. Year

2019-07-19 19:21发布

什么是使用了另一种在以下的优点:

DATEPART(YEAR, GETDATE())

而不是:

YEAR(GETDATE())

是否存在性能差异? 如果是这样,哪一个是最快的?

Answer 1:

其实-使用YEAR(..)最好是对我来说,因为它被认为是一个确定性的功能,因此,如果我用这个在计算列定义

ALTER TABLE dbo.MyTable
ADD YearOfDate AS YEAR(SomeDateColumn)

我可以让这个专栏坚持 (并将其存储到表):

ALTER TABLE dbo.MyTable
ADD YearOfDate AS YEAR(SomeDateColumn) PERSISTED

并不适用于工作DATEPART(YEAR, SomeDateColumn) -只注意到这个启发式不要问我为什么)。

这同样适用于MONTH(SomeDate)DATEPART(MONTH, SomeDate)

如果您有需要从基础上的日期(如月份和年份选择表SalesDate再有一个月,年持久化计算列(和索引他们)什么的),可以是一个巨大的性能提升。



Answer 2:

没有区别。 在执行计划都被转换为datepart(year,getdate())

这是SQL Server 2005中,2008年和2012年真实的。

select datepart(year, getdate())
from (select 1 x) x

select year(getdate())
from (select 1 x) x

执行计划。

<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.0" Build="9.00.5057.00" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="1" StatementEstRows="1" StatementId="1" StatementOptmLevel="TRIVIAL" StatementSubTreeCost="1.157E-06" StatementText="select datepart(year, getdate())&#xD;&#xA;from (select 1 x) x&#xD;&#xA;&#xD;" StatementType="SELECT">
          <StatementSetOptions ANSI_NULLS="false" ANSI_PADDING="false" ANSI_WARNINGS="false" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="false" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="false" />
          <QueryPlan DegreeOfParallelism="0" CachedPlanSize="8" CompileTime="23" CompileCPU="23" CompileMemory="64">
            <RelOp AvgRowSize="11" EstimateCPU="1.157E-06" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Constant Scan" NodeId="0" Parallel="false" PhysicalOp="Constant Scan" EstimatedTotalSubtreeCost="1.157E-06">
              <OutputList>
                <ColumnReference Column="Expr1001" />
              </OutputList>
              <RunTimeInformation>
                <RunTimeCountersPerThread Thread="0" ActualRows="1" ActualEndOfScans="1" ActualExecutions="1" />
              </RunTimeInformation>
              <ConstantScan>
                <Values>
                  <Row>
                    <ScalarOperator ScalarString="datepart(year,getdate())">
                      <Identifier>
                        <ColumnReference Column="ConstExpr1002">
                          <ScalarOperator>
                            <Intrinsic FunctionName="datepart">
                              <ScalarOperator>
                                <Const ConstValue="(0)" />
                              </ScalarOperator>
                              <ScalarOperator>
                                <Intrinsic FunctionName="getdate" />
                              </ScalarOperator>
                            </Intrinsic>
                          </ScalarOperator>
                        </ColumnReference>
                      </Identifier>
                    </ScalarOperator>
                  </Row>
                </Values>
              </ConstantScan>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="2" StatementEstRows="1" StatementId="2" StatementOptmLevel="TRIVIAL" StatementSubTreeCost="1.157E-06" StatementText="select year(getdate())&#xD;&#xA;from (select 1 x) x" StatementType="SELECT">
          <StatementSetOptions ANSI_NULLS="false" ANSI_PADDING="false" ANSI_WARNINGS="false" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="false" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="false" />
          <QueryPlan DegreeOfParallelism="0" CachedPlanSize="8" CompileTime="0" CompileCPU="0" CompileMemory="64">
            <RelOp AvgRowSize="11" EstimateCPU="1.157E-06" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Constant Scan" NodeId="0" Parallel="false" PhysicalOp="Constant Scan" EstimatedTotalSubtreeCost="1.157E-06">
              <OutputList>
                <ColumnReference Column="Expr1001" />
              </OutputList>
              <RunTimeInformation>
                <RunTimeCountersPerThread Thread="0" ActualRows="1" ActualEndOfScans="1" ActualExecutions="1" />
              </RunTimeInformation>
              <ConstantScan>
                <Values>
                  <Row>
                    <ScalarOperator ScalarString="datepart(year,getdate())">
                      <Identifier>
                        <ColumnReference Column="ConstExpr1002">
                          <ScalarOperator>
                            <Intrinsic FunctionName="datepart">
                              <ScalarOperator>
                                <Const ConstValue="(0)" />
                              </ScalarOperator>
                              <ScalarOperator>
                                <Intrinsic FunctionName="getdate" />
                              </ScalarOperator>
                            </Intrinsic>
                          </ScalarOperator>
                        </ColumnReference>
                      </Identifier>
                    </ScalarOperator>
                  </Row>
                </Values>
              </ConstantScan>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>


文章来源: Datepart(year, …) vs. Year(…)