sql-server - 日期部分(年份,...)与年份(...)

标签 sql-server tsql

使用其中一种相对于另一种有哪些优势:

DATEPART(YEAR, GETDATE())

相对于:

YEAR(GETDATE())

有性能差异吗?如果是的话,哪一个最快?

最佳答案

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

关于sql-server - 日期部分(年份,...)与年份(...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14850239/

相关文章:

php - 在 Windows 7 中的 wamp 上安装 SuiteCRM 时出错

sql-server - SQL : Does disabling triggers count as schema change? ("Could not complete cursor operation because the table schema changed")

c# - sql 2008 支持 SqlCacheDependency 吗?

SQL 和 ALL 运算符

c# - 在 SQL 或 C# 中对列求和

sql - 如何在 SQL 数据库中存储树

sql - 多个计数和分组依据

sql - 通过使用 T-SQL 解析另一列来更新列

sql-server - Xpath 为有效的 Xpath 表达式返回 NULL

sql - 在两列上检查唯一的有效方法?