慢速SQL查询

时间:2016-03-11 16:29:43

标签: sql sql-server inner-join

我有一个包含3个内部联接的脚本。似乎当我添加第三个内连接时,脚本运行速度非常慢。前两个表有大约3-4,000行,第三个有63.3万行,最后一个表只有36行。我试过索引,但我没有注意到变化。我在编制索引之前和之后都包含了我的脚本。返回前5个条目大约需要12分钟。如果需要更多信息,请询问。我是数据库管理的新手。

在索引之前:

SELECT DISTINCT TOP 5 sn.NAME, cd.ASSIGNEDPOLICY, cd.COMPANYPOLICYNUMBER, ad.COVERAGECODE, co."DESCRIPTION"
From SNAMES sn
inner join TCUSDTL cd on sn.NAMEID = cd.NAMEID 
inner join TPOLACTD ad on sn.NAMEID = ad.NAMEID 
inner join SCOVTBL co on ad.COVERAGECODE = co.COVERAGE
ORDER BY NAME

索引后:

SELECT DISTINCT TOP 5 sn.NAME, cd.ASSIGNEDPOLICY, cd.COMPANYPOLICYNUMBER, ad.COVERAGECODE, co."DESCRIPTION"
From SNAMES sn WITH(INDEX(SNAMES_names))
inner join TCUSDTL cd WITH(INDEX(TCUSDTL_policies)) on sn.NAMEID = cd.NAMEID 
inner join TPOLACTD ad WITH(INDEX(TPOLACTD_coverage)) on sn.NAMEID = ad.NAMEID 
inner join SCOVTBL co WITH(INDEX(SCOVTBL_coverage)) on ad.COVERAGECODE = co.COVERAGE
ORDER BY NAME

执行计划:

<?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.5" Build="13.0.703.859" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="1" StatementEstRows="1000" StatementId="1" StatementOptmLevel="FULL" CardinalityEstimationModelVersion="120" StatementSubTreeCost="5270.92" StatementText="SELECT DISTINCT TOP 1000 sn.NAME, cd.ASSIGNEDPOLICY, cd.COMPANYPOLICYNUMBER, bd.COVERAGECODE, sd.&quot;DESCRIPTION&quot;&#xD;&#xA;FROM SNAMES sn&#xD;&#xA;inner join TCUSDTL cd on sn.NAMEID = cd.NAMEID &#xD;&#xA;inner join TPOLACTD bd on sn.NAMEID = bd.NAMEID &#xD;&#xA;inner join SCOVTBL sd on bd.COVERAGECODE = sd.COVERAGE&#xD;&#xA;ORDER BY NAME" StatementType="SELECT" QueryHash="0x5C4838D1FC1E530E" QueryPlanHash="0x854BE2475051DE74" RetrievedFromCache="true" SecurityPolicyApplied="false">
          <StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
          <QueryPlan NonParallelPlanReason="EstimatedDOPIsOne" CachedPlanSize="104" CompileTime="27" CompileCPU="26" CompileMemory="1144">
            <MemoryGrantInfo SerialRequiredMemory="3584" SerialDesiredMemory="7383960" />
            <OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="11744005" EstimatedPagesCached="734000" EstimatedAvailableDegreeOfParallelism="1" />
            <RelOp AvgRowSize="94" EstimateCPU="0.0001" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="1000" LogicalOp="Top" NodeId="0" Parallel="false" PhysicalOp="Top" EstimatedTotalSubtreeCost="5270.92">
              <OutputList>
                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAME" />
                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="ASSIGNEDPOLICY" />
                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="COMPANYPOLICYNUMBER" />
                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="DESCRIPTION" />
              </OutputList>
              <Top RowCount="false" IsPercent="false" WithTies="false">
                <TopExpression>
                  <ScalarOperator ScalarString="(1000)">
                    <Const ConstValue="(1000)" />
                  </ScalarOperator>
                </TopExpression>
                <RelOp AvgRowSize="94" EstimateCPU="5177.31" EstimateIO="0.0112613" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="1000" LogicalOp="Distinct Sort" NodeId="1" Parallel="false" PhysicalOp="Sort" EstimatedTotalSubtreeCost="5270.92">
                  <OutputList>
                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAME" />
                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="ASSIGNEDPOLICY" />
                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="COMPANYPOLICYNUMBER" />
                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="DESCRIPTION" />
                  </OutputList>
                  <MemoryFractions Input="0.985219" Output="1" />
                  <Sort Distinct="true">
                    <OrderBy>
                      <OrderByColumn Ascending="true">
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAME" />
                      </OrderByColumn>
                      <OrderByColumn Ascending="true">
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="ASSIGNEDPOLICY" />
                      </OrderByColumn>
                      <OrderByColumn Ascending="true">
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="COMPANYPOLICYNUMBER" />
                      </OrderByColumn>
                      <OrderByColumn Ascending="true">
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                      </OrderByColumn>
                      <OrderByColumn Ascending="true">
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="DESCRIPTION" />
                      </OrderByColumn>
                    </OrderBy>
                    <RelOp AvgRowSize="94" EstimateCPU="75.5786" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="44400500" LogicalOp="Inner Join" NodeId="2" Parallel="false" PhysicalOp="Hash Match" EstimatedTotalSubtreeCost="93.6068">
                      <OutputList>
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAME" />
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="ASSIGNEDPOLICY" />
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="COMPANYPOLICYNUMBER" />
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="DESCRIPTION" />
                      </OutputList>
                      <MemoryFractions Input="0.816955" Output="0.0120753" />
                      <Hash>
                        <DefinedValues />
                        <HashKeysBuild>
                          <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="NAMEID" />
                        </HashKeysBuild>
                        <HashKeysProbe>
                          <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="NAMEID" />
                        </HashKeysProbe>
                        <ProbeResidual>
                          <ScalarOperator ScalarString="[DC02001].[dbo].[TCUSDTL].[NAMEID] as [cd].[NAMEID]=[DC02001].[dbo].[TPOLACTD].[NAMEID] as [bd].[NAMEID]">
                            <Compare CompareOp="EQ">
                              <ScalarOperator>
                                <Identifier>
                                  <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="NAMEID" />
                                </Identifier>
                              </ScalarOperator>
                              <ScalarOperator>
                                <Identifier>
                                  <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="NAMEID" />
                                </Identifier>
                              </ScalarOperator>
                            </Compare>
                          </ScalarOperator>
                        </ProbeResidual>
                        <RelOp AvgRowSize="33" EstimateCPU="0.283312" EstimateIO="2.24609" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="257414" LogicalOp="Table Scan" NodeId="3" Parallel="false" PhysicalOp="Table Scan" EstimatedTotalSubtreeCost="2.5294" TableCardinality="257414">
                          <OutputList>
                            <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="NAMEID" />
                            <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="ASSIGNEDPOLICY" />
                            <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="COMPANYPOLICYNUMBER" />
                          </OutputList>
                          <TableScan Ordered="false" ForcedIndex="false" ForceScan="false" NoExpandHint="false" Storage="RowStore">
                            <DefinedValues>
                              <DefinedValue>
                                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="NAMEID" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="ASSIGNEDPOLICY" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" Column="COMPANYPOLICYNUMBER" />
                              </DefinedValue>
                            </DefinedValues>
                            <Object Database="[DC02001]" Schema="[dbo]" Table="[TCUSDTL]" Alias="[cd]" IndexKind="Heap" Storage="RowStore" />
                          </TableScan>
                        </RelOp>
                        <RelOp AvgRowSize="86" EstimateCPU="7.96164" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="1405060" LogicalOp="Inner Join" NodeId="4" Parallel="false" PhysicalOp="Hash Match" EstimatedTotalSubtreeCost="15.4988">
                          <OutputList>
                            <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAME" />
                            <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="NAMEID" />
                            <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                            <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="DESCRIPTION" />
                          </OutputList>
                          <MemoryFractions Input="0.179818" Output="0.00265786" />
                          <Hash>
                            <DefinedValues />
                            <HashKeysBuild>
                              <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAMEID" />
                            </HashKeysBuild>
                            <HashKeysProbe>
                              <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="NAMEID" />
                            </HashKeysProbe>
                            <ProbeResidual>
                              <ScalarOperator ScalarString="[DC02001].[dbo].[SNAMES].[NAMEID] as [sn].[NAMEID]=[DC02001].[dbo].[TPOLACTD].[NAMEID] as [bd].[NAMEID]">
                                <Compare CompareOp="EQ">
                                  <ScalarOperator>
                                    <Identifier>
                                      <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAMEID" />
                                    </Identifier>
                                  </ScalarOperator>
                                  <ScalarOperator>
                                    <Identifier>
                                      <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="NAMEID" />
                                    </Identifier>
                                  </ScalarOperator>
                                </Compare>
                              </ScalarOperator>
                            </ProbeResidual>
                            <RelOp AvgRowSize="56" EstimateCPU="0.0416996" EstimateIO="0.221644" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="37766" LogicalOp="Index Scan" NodeId="5" Parallel="false" PhysicalOp="Index Scan" EstimatedTotalSubtreeCost="0.263343" TableCardinality="37766">
                              <OutputList>
                                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAMEID" />
                                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAME" />
                              </OutputList>
                              <IndexScan Ordered="false" ForcedIndex="false" ForceSeek="false" ForceScan="false" NoExpandHint="false" Storage="RowStore">
                                <DefinedValues>
                                  <DefinedValue>
                                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAMEID" />
                                  </DefinedValue>
                                  <DefinedValue>
                                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Alias="[sn]" Column="NAME" />
                                  </DefinedValue>
                                </DefinedValues>
                                <Object Database="[DC02001]" Schema="[dbo]" Table="[SNAMES]" Index="[SNAMES_names]" Alias="[sn]" IndexKind="NonClustered" Storage="RowStore" />
                              </IndexScan>
                            </RelOp>
                            <RelOp AvgRowSize="46" EstimateCPU="4.94636" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="702122" LogicalOp="Inner Join" NodeId="6" Parallel="false" PhysicalOp="Hash Match" EstimatedTotalSubtreeCost="7.27381">
                              <OutputList>
                                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="NAMEID" />
                                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                                <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="DESCRIPTION" />
                              </OutputList>
                              <MemoryFractions Input="0.00322675" Output="4.7694E-05" />
                              <Hash>
                                <DefinedValues />
                                <HashKeysBuild>
                                  <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="COVERAGE" />
                                </HashKeysBuild>
                                <HashKeysProbe>
                                  <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                                </HashKeysProbe>
                                <ProbeResidual>
                                  <ScalarOperator ScalarString="[DC02001].[dbo].[SCOVTBL].[COVERAGE] as [sd].[COVERAGE]=[DC02001].[dbo].[TPOLACTD].[COVERAGECODE] as [bd].[COVERAGECODE]">
                                    <Compare CompareOp="EQ">
                                      <ScalarOperator>
                                        <Identifier>
                                          <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="COVERAGE" />
                                        </Identifier>
                                      </ScalarOperator>
                                      <ScalarOperator>
                                        <Identifier>
                                          <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                                        </Identifier>
                                      </ScalarOperator>
                                    </Compare>
                                  </ScalarOperator>
                                </ProbeResidual>
                                <RelOp AvgRowSize="37" EstimateCPU="0.0001966" EstimateIO="0.003125" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="36" LogicalOp="Index Scan" NodeId="7" Parallel="false" PhysicalOp="Index Scan" EstimatedTotalSubtreeCost="0.0033216" TableCardinality="36">
                                  <OutputList>
                                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="COVERAGE" />
                                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="DESCRIPTION" />
                                  </OutputList>
                                  <IndexScan Ordered="false" ForcedIndex="false" ForceSeek="false" ForceScan="false" NoExpandHint="false" Storage="RowStore">
                                    <DefinedValues>
                                      <DefinedValue>
                                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="COVERAGE" />
                                      </DefinedValue>
                                      <DefinedValue>
                                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Alias="[sd]" Column="DESCRIPTION" />
                                      </DefinedValue>
                                    </DefinedValues>
                                    <Object Database="[DC02001]" Schema="[dbo]" Table="[SCOVTBL]" Index="[SCOVTBL_coverage]" Alias="[sd]" IndexKind="NonClustered" Storage="RowStore" />
                                  </IndexScan>
                                </RelOp>
                                <RelOp AvgRowSize="21" EstimateCPU="0.6973" EstimateIO="1.62683" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="633766" LogicalOp="Index Scan" NodeId="8" Parallel="false" PhysicalOp="Index Scan" EstimatedTotalSubtreeCost="2.32413" TableCardinality="633766">
                                  <OutputList>
                                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="NAMEID" />
                                    <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                                  </OutputList>
                                  <IndexScan Ordered="false" ForcedIndex="false" ForceSeek="false" ForceScan="false" NoExpandHint="false" Storage="RowStore">
                                    <DefinedValues>
                                      <DefinedValue>
                                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="NAMEID" />
                                      </DefinedValue>
                                      <DefinedValue>
                                        <ColumnReference Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Alias="[bd]" Column="COVERAGECODE" />
                                      </DefinedValue>
                                    </DefinedValues>
                                    <Object Database="[DC02001]" Schema="[dbo]" Table="[TPOLACTD]" Index="[TPOLACTD_coverage]" Alias="[bd]" IndexKind="NonClustered" Storage="RowStore" />
                                  </IndexScan>
                                </RelOp>
                              </Hash>
                            </RelOp>
                          </Hash>
                        </RelOp>
                      </Hash>
                    </RelOp>
                  </Sort>
                </RelOp>
              </Top>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>

0 个答案:

没有答案