长时间运行的数据库查询

时间:2010-06-15 15:58:18

标签: sql-server query-optimization

我有一个长期运行的SQL Server 2005查询,我一直希望优化它。

当我查看实际的执行计划时,它表示Clustered Index Seek有66%的成本。

执行计划Snippit:

<RelOp AvgRowSize="31" EstimateCPU="0.0113754" EstimateIO="0.0609028" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="10198.5" LogicalOp="Clustered Index Seek" NodeId="16" Parallel="false" PhysicalOp="Clustered Index Seek" EstimatedTotalSubtreeCost="0.0722782">
                              <OutputList>
                                <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="quoteDate" />
                                <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="price" />
                                <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="tenure" />
                              </OutputList>
                              <RunTimeInformation>
                                <RunTimeCountersPerThread Thread="0" ActualRows="1067" ActualEndOfScans="1" ActualExecutions="1" />
                              </RunTimeInformation>
                              <IndexScan Ordered="true" ScanDirection="FORWARD" ForcedIndex="false" NoExpandHint="false">
                                <DefinedValues>
                                  <DefinedValue>
                                    <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="quoteDate" />
                                  </DefinedValue>
                                  <DefinedValue>
                                    <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="price" />
                                  </DefinedValue>
                                  <DefinedValue>
                                    <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="tenure" />
                                  </DefinedValue>
                                </DefinedValues>
                                <Object Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Index="[_dta_index_Indices_14_320720195__K5_K2_K1_3]" Alias="[I]" />
                                <SeekPredicates>
                                  <SeekPredicate>
                                    <Prefix ScanType="EQ">
                                      <RangeColumns>
                                        <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="HedgeProduct" ComputedColumn="true" />
                                      </RangeColumns>
                                      <RangeExpressions>
                                        <ScalarOperator ScalarString="(1)">
                                          <Const ConstValue="(1)" />
                                        </ScalarOperator>
                                      </RangeExpressions>
                                    </Prefix>
                                    <StartRange ScanType="GE">
                                      <RangeColumns>
                                        <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="tenure" />
                                      </RangeColumns>
                                      <RangeExpressions>
                                        <ScalarOperator ScalarString="[@StartMonth]">
                                          <Identifier>
                                            <ColumnReference Column="@StartMonth" />
                                          </Identifier>
                                        </ScalarOperator>
                                      </RangeExpressions>
                                    </StartRange>
                                    <EndRange ScanType="LE">
                                      <RangeColumns>
                                        <ColumnReference Database="[wf_1]" Schema="[dbo]" Table="[Indices]" Alias="[I]" Column="tenure" />
                                      </RangeColumns>
                                      <RangeExpressions>
                                        <ScalarOperator ScalarString="[@EndMonth]">
                                          <Identifier>
                                            <ColumnReference Column="@EndMonth" />
                                          </Identifier>
                                        </ScalarOperator>
                                      </RangeExpressions>
                                    </EndRange>
                                  </SeekPredicate>
                                </SeekPredicates>
                              </IndexScan>
                            </RelOp>

从这个,有没有人看到一个明显的问题,导致这需要这么长时间?

以下是查询:

(SELECT quotedate, tenure, price, ActualVolume, HedgePortfolioValue, Price AS UnhedgedPrice, ((ActualVolume*Price - HedgePortfolioValue)/ActualVolume) AS HedgedPrice
        FROM
            (
            SELECT [quoteDate]
                  ,[price]
                  , tenure
                ,isnull(wf_1.[Risks].[HedgePortValueAsOfDate2](1,tenureMonth,quotedate,price),0) as HedgePortfolioValue
            ,[TotalOperatingGasVolume] as ActualVolume
              FROM [wf_1].[dbo].[Indices] I
            inner join
                (
                SELECT DISTINCT tenureMonth
                FROM [wf_1].[Risks].[KnowRiskTrades]
                WHERE HedgeProduct = 1
                    AND portfolio <> 'Natural Gas Hedge Transactions'
                ) B ON I.tenure=B.tenureMonth
            inner join
                (
                SELECT [Month],[TotalOperatingGasVolume]
                FROM [wf_1].[Risks].[ActualGasVolumes]
                ) C ON C.[Month]=B.tenureMonth
            WHERE HedgeProduct = 1
            AND quoteDate>=dateadd(day, -3*365, tenureMonth)
            AND quoteDate<=dateadd(day,-3,tenureMonth)
            )A 
    ) 

2 个答案:

答案 0 :(得分:1)

听起来您的统计信息已关闭。

该查询正在扼杀我的眼睛,我不想猜测并选择错误的表格,无论CI寻求的是哪个表格,都至少更新该表格的统计数据并再次检查查询。

更新统计数据: http://msdn.microsoft.com/en-us/library/ms187348.aspx

答案 1 :(得分:1)

这一点引起了人们的注意,你有投资组合的指数吗?你为什么使用DISTINCT?

SELECT DISTINCT tenureMonth
FROM [wf_1].[Risks].[KnowRiskTrades]
WHERE HedgeProduct = 1
AND portfolio <> 'Natural Gas Hedge Transactions'

这样做:

WHERE HedgeProduct = 1
AND quoteDate>=dateadd(day, -3*365, tenureMonth)
AND quoteDate<=dateadd(day,-3,tenureMonth)

但是,如果不了解索引的位置以及相关表中的数据量,很难提供好的建议。

另外:查询需要多长时间?你想要多长时间?你多久运行一次?您的数据库服务器有多忙?