2013-02-13 59 views

回答

12

其实 - 用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或东西),然后有月和年持续计算列(以及它们编索引)可以是一个巨大性能提升。

+0

有趣的是,我其实并没有看到这个。 [SQL Fiddle](http://sqlfiddle.com/#!3/ab2f5/1/0)。我错过了什么? – 2013-02-13 10:50:01

+0

[SQL Server 2008和2012]中的文档(http://msdn.microsoft.com/zh-cn/library/ms186724(v = sql.110).aspx)表示'datepart'不确定。对于[SQL Server 2005](http://msdn.microsoft.com/zh-cn/library/ms186724(v = sql.90).aspx),有'datepart'的非确定性参数列表。看起来像文档已更改,但SQL Server没有。 – 2013-02-13 11:07:10

+0

@MikaelEriksson:有趣......我可以**发誓**当它试过时它不能与'DATEPART()AS PERSISTED'一起工作......但它现在可以。谢谢澄清! – 2013-02-13 12:08:37

22

没有区别。在执行计划中,两者都被翻译为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>