我很难理解为什么一个看似简单的查询(对2个表进行内部联接)会导致估计的行数为1,而实际却接近200万。我看不到缺少索引的任何问题,而98%的查询成本是作为索引查找发生的。我在查询运行时看不到任何I / O或CPU红色标志,这大约需要12秒。在这里添加统计信息似乎没有意义,因为查询只是在id上联接,并且两个表在这些id上都有聚集索引作为主键。

此查询是遇到相同问题的较大查询的简化版本,但我将其归结为遇到相同问题的简化查询。我认为估计行与实际行之间的差异至少会导致次优查询性能。有趣的是,用左外连接代替内连接会导致大致相同的性能,即使它确实将估计行固定为正确。

查询:

SELECT StationId, Readings.Power, Readings.TimeCovered
FROM  Readings
INNER JOIN Stations ON Readings.StationId = Stations.Id


实际计划XML:

<?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.1" Build="10.50.1600.1" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="1" StatementEstRows="1" StatementId="1" StatementOptmLevel="FULL" StatementOptmEarlyAbortReason="GoodEnoughPlanFound" StatementSubTreeCost="0.388499" StatementText="SELECT StationId,&#xD;&#xA;[Readings].[Power], [Readings].[TimeCovered]&#xD;&#xA;FROM  [dbo].[Readings]&#xD;&#xA;INNER JOIN [dbo].[Stations] ON [Readings].[StationId] = [Stations].[Id]" StatementType="SELECT" QueryHash="0x540DF2384788314E" QueryPlanHash="0x7546B31B38AC8153">
          <StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="false" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
          <QueryPlan DegreeOfParallelism="1" CachedPlanSize="24" CompileTime="3" CompileCPU="3" CompileMemory="224">
            <RelOp AvgRowSize="17" EstimateCPU="0.00118294" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Inner Join" NodeId="0" Parallel="false" PhysicalOp="Nested Loops" EstimatedTotalSubtreeCost="0.388499">
              <OutputList>
                <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="StationId" />
                <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="Power" />
                <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="TimeCovered" />
              </OutputList>
              <RunTimeInformation>
                <RunTimeCountersPerThread Thread="0" ActualRows="1898419" ActualEndOfScans="1" ActualExecutions="1" />
              </RunTimeInformation>
              <NestedLoops Optimized="false">
                <OuterReferences>
                  <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Stations]" Column="Id" />
                </OuterReferences>
                <RelOp AvgRowSize="11" EstimateCPU="0.0004683" EstimateIO="0.00608796" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="283" LogicalOp="Clustered Index Scan" NodeId="1" Parallel="false" PhysicalOp="Clustered Index Scan" EstimatedTotalSubtreeCost="0.00655626" TableCardinality="283">
                  <OutputList>
                    <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Stations]" Column="Id" />
                  </OutputList>
                  <RunTimeInformation>
                    <RunTimeCountersPerThread Thread="0" ActualRows="283" ActualEndOfScans="1" ActualExecutions="1" />
                  </RunTimeInformation>
                  <IndexScan Ordered="false" ForcedIndex="false" NoExpandHint="false">
                    <DefinedValues>
                      <DefinedValue>
                        <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Stations]" Column="Id" />
                      </DefinedValue>
                    </DefinedValues>
                    <Object Database="[MyDB]" Schema="[dbo]" Table="[Stations]" Index="[PK_Stations]" IndexKind="Clustered" />
                  </IndexScan>
                </RelOp>
                <RelOp AvgRowSize="17" EstimateCPU="0.0012571" EstimateIO="0.025" EstimateRebinds="0" EstimateRewinds="282" EstimateRows="1" LogicalOp="Index Seek" NodeId="2" Parallel="false" Partitioned="true" PhysicalOp="Index Seek" EstimatedTotalSubtreeCost="0.380759" TableCardinality="4246720">
                  <OutputList>
                    <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="StationId" />
                    <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="Power" />
                    <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="TimeCovered" />
                  </OutputList>
                  <RunTimeInformation>
                    <RunTimeCountersPerThread Thread="0" ActualRows="1898419" ActualEndOfScans="283" ActualExecutions="283" />
                  </RunTimeInformation>
                  <RunTimePartitionSummary>
                    <PartitionsAccessed PartitionCount="8">
                      <PartitionRange Start="1" End="8" />
                    </PartitionsAccessed>
                  </RunTimePartitionSummary>
                  <IndexScan Ordered="true" ScanDirection="FORWARD" ForcedIndex="false" ForceSeek="false" NoExpandHint="false">
                    <DefinedValues>
                      <DefinedValue>
                        <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="StationId" />
                      </DefinedValue>
                      <DefinedValue>
                        <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="Power" />
                      </DefinedValue>
                      <DefinedValue>
                        <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="TimeCovered" />
                      </DefinedValue>
                    </DefinedValues>
                    <Object Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Index="[IX_Readings_StationId_SecondsSinceEpoch]" IndexKind="NonClustered" />
                    <SeekPredicates>
                      <SeekPredicateNew>
                        <SeekKeys>
                          <StartRange ScanType="GE">
                            <RangeColumns>
                              <ColumnReference Column="PtnId1000" />
                            </RangeColumns>
                            <RangeExpressions>
                              <ScalarOperator ScalarString="(1)">
                                <Const ConstValue="(1)" />
                              </ScalarOperator>
                            </RangeExpressions>
                          </StartRange>
                          <EndRange ScanType="LE">
                            <RangeColumns>
                              <ColumnReference Column="PtnId1000" />
                            </RangeColumns>
                            <RangeExpressions>
                              <ScalarOperator ScalarString="(8)">
                                <Const ConstValue="(8)" />
                              </ScalarOperator>
                            </RangeExpressions>
                          </EndRange>
                        </SeekKeys>
                        <SeekKeys>
                          <Prefix ScanType="EQ">
                            <RangeColumns>
                              <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Readings]" Column="StationId" />
                            </RangeColumns>
                            <RangeExpressions>
                              <ScalarOperator ScalarString="[MyDB].[dbo].[Stations].[Id]">
                                <Identifier>
                                  <ColumnReference Database="[MyDB]" Schema="[dbo]" Table="[Stations]" Column="Id" />
                                </Identifier>
                              </ScalarOperator>
                            </RangeExpressions>
                          </Prefix>
                        </SeekKeys>
                      </SeekPredicateNew>
                    </SeekPredicates>
                  </IndexScan>
                </RelOp>
              </NestedLoops>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>

最佳答案

未更新的统计信息可能与此有关。尝试运行exec sp_updatestats,看看计划是否改变。另外,请确保您在SQL服务器上运行了一项维护作业,该作业会定期更新统计信息。

09-15 14:20