2012-07-18 55 views
1

我有一個包含ConChainNum,Control和TransDate的索引,並且我只重建了索引。我與工作表有100多萬條記錄和下面的查詢不返回任何結果:如何正確索引多個字段以獲得更快的結果

select * from sbt_pos_sales 
where conchainnum = 810 and 
     control = 852013688 and 
     transdate = 20120712 

鑑於我與where子句中的三個字段專門做出一個索引,我真的希望幾乎立即結果,但花了大約半分鐘的時間告訴我那裏沒有結果。

有人可以向我解釋爲什麼這個指數沒有使它比這更快,如果我能以某種方式做它不同,使其更快?

執行計劃:

<?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.1" Build="10.0.5500.0" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan"> 
    <BatchSequence> 
    <Batch> 
     <Statements> 
     <StmtSimple StatementCompId="1" StatementEstRows="20.2265" StatementId="1" StatementOptmLevel="FULL" StatementSubTreeCost="348.687" StatementText="SELECT * FROM [ngdwdb]..[sbt_pos_sales] WHERE [conchainnum][email protected] AND [control][email protected] AND [transdate][email protected]" StatementType="SELECT" QueryHash="0x11B117D9A8C6DF58" QueryPlanHash="0xBA3EFC04623601F6"> 
      <StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" /> 
      <QueryPlan DegreeOfParallelism="8" MemoryGrant="5504" CachedPlanSize="48" CompileTime="95" CompileCPU="4" CompileMemory="368"> 
      <RelOp AvgRowSize="193" EstimateCPU="0.0655713" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="7330.87" LogicalOp="Gather Streams" NodeId="1" Parallel="true" PhysicalOp="Parallelism" EstimatedTotalSubtreeCost="348.686"> 
       <OutputList> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="RowIdent" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ConChainNum" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Control" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="TransDate" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ControlGS" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="UPC_UCP" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="DateSold" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Price_UCP" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Store" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="QtySold" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="FileName" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="IssueCode" /> 
       <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="DealerNum" /> 
       </OutputList> 
       <RunTimeInformation> 
       <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
       </RunTimeInformation> 
       <Parallelism> 
       <RelOp AvgRowSize="193" EstimateCPU="0.00766076" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="7330.87" LogicalOp="Inner Join" NodeId="2" Parallel="true" PhysicalOp="Nested Loops" EstimatedTotalSubtreeCost="348.621"> 
        <OutputList> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="RowIdent" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ConChainNum" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Control" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="TransDate" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ControlGS" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="UPC_UCP" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="DateSold" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Price_UCP" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Store" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="QtySold" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="FileName" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="IssueCode" /> 
        <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="DealerNum" /> 
        </OutputList> 
        <RunTimeInformation> 
        <RunTimeCountersPerThread Thread="6" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
        <RunTimeCountersPerThread Thread="2" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
        <RunTimeCountersPerThread Thread="3" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
        <RunTimeCountersPerThread Thread="7" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
        <RunTimeCountersPerThread Thread="5" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
        <RunTimeCountersPerThread Thread="4" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
        <RunTimeCountersPerThread Thread="8" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
        <RunTimeCountersPerThread Thread="1" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
        <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
        </RunTimeInformation> 
        <NestedLoops Optimized="true" WithUnorderedPrefetch="true"> 
        <OuterReferences> 
         <ColumnReference Column="Bmk1000" /> 
         <ColumnReference Column="Expr1009" /> 
        </OuterReferences> 
        <RelOp AvgRowSize="58" EstimateCPU="14.9196" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="7330.87" LogicalOp="Compute Scalar" NodeId="5" Parallel="true" PhysicalOp="Compute Scalar" EstimatedTotalSubtreeCost="324.579"> 
         <OutputList> 
         <ColumnReference Column="Bmk1000" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ConChainNum" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Control" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="TransDate" /> 
         <ColumnReference Column="Expr1008" /> 
         </OutputList> 
         <ComputeScalar> 
         <DefinedValues> 
          <DefinedValue> 
          <ColumnReference Column="Expr1008" /> 
          <ScalarOperator ScalarString="BmkToPage([Bmk1000])"> 
           <Intrinsic FunctionName="BmkToPage"> 
           <ScalarOperator> 
            <Identifier> 
            <ColumnReference Column="Bmk1000" /> 
            </Identifier> 
           </ScalarOperator> 
           </Intrinsic> 
          </ScalarOperator> 
          </DefinedValue> 
         </DefinedValues> 
         <RelOp AvgRowSize="58" EstimateCPU="15.196" EstimateIO="294.463" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="7330.87" LogicalOp="Index Seek" NodeId="6" Parallel="true" PhysicalOp="Index Seek" EstimatedTotalSubtreeCost="309.659" TableCardinality="107302000"> 
          <OutputList> 
          <ColumnReference Column="Bmk1000" /> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ConChainNum" /> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Control" /> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="TransDate" /> 
          </OutputList> 
          <RunTimeInformation> 
          <RunTimeCountersPerThread Thread="6" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
          <RunTimeCountersPerThread Thread="3" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
          <RunTimeCountersPerThread Thread="2" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
          <RunTimeCountersPerThread Thread="7" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
          <RunTimeCountersPerThread Thread="8" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
          <RunTimeCountersPerThread Thread="5" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
          <RunTimeCountersPerThread Thread="1" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
          <RunTimeCountersPerThread Thread="4" ActualRows="0" ActualEndOfScans="1" ActualExecutions="1" /> 
          <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
          </RunTimeInformation> 
          <IndexScan Ordered="true" ScanDirection="FORWARD" ForcedIndex="false" ForceSeek="false" NoExpandHint="false"> 
          <DefinedValues> 
           <DefinedValue> 
           <ColumnReference Column="Bmk1000" /> 
           </DefinedValue> 
           <DefinedValue> 
           <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ConChainNum" /> 
           </DefinedValue> 
           <DefinedValue> 
           <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Control" /> 
           </DefinedValue> 
           <DefinedValue> 
           <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="TransDate" /> 
           </DefinedValue> 
          </DefinedValues> 
          <Object Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Index="[idxLoad]" IndexKind="NonClustered" /> 
          <SeekPredicates> 
           <SeekPredicateNew> 
           <SeekKeys> 
            <Prefix ScanType="EQ"> 
            <RangeColumns> 
             <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ConChainNum" /> 
            </RangeColumns> 
            <RangeExpressions> 
             <ScalarOperator ScalarString="(810)"> 
             <Const ConstValue="(810)" /> 
             </ScalarOperator> 
            </RangeExpressions> 
            </Prefix> 
           </SeekKeys> 
           </SeekPredicateNew> 
          </SeekPredicates> 
          <Predicate> 
           <ScalarOperator ScalarString="CONVERT_IMPLICIT(int,[NGDWDB].[dbo].[SBT_POS_SALES].[Control],0)=(852013688) AND CONVERT_IMPLICIT(int,[NGDWDB].[dbo].[SBT_POS_SALES].[TransDate],0)=(20120712)"> 
           <Logical Operation="AND"> 
            <ScalarOperator> 
            <Compare CompareOp="EQ"> 
             <ScalarOperator> 
             <Convert DataType="int" Style="0" Implicit="true"> 
              <ScalarOperator> 
              <Identifier> 
               <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Control" /> 
              </Identifier> 
              </ScalarOperator> 
             </Convert> 
             </ScalarOperator> 
             <ScalarOperator> 
             <Const ConstValue="(852013688)" /> 
             </ScalarOperator> 
            </Compare> 
            </ScalarOperator> 
            <ScalarOperator> 
            <Compare CompareOp="EQ"> 
             <ScalarOperator> 
             <Convert DataType="int" Style="0" Implicit="true"> 
              <ScalarOperator> 
              <Identifier> 
               <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="TransDate" /> 
              </Identifier> 
              </ScalarOperator> 
             </Convert> 
             </ScalarOperator> 
             <ScalarOperator> 
             <Const ConstValue="(20120712)" /> 
             </ScalarOperator> 
            </Compare> 
            </ScalarOperator> 
           </Logical> 
           </ScalarOperator> 
          </Predicate> 
          </IndexScan> 
         </RelOp> 
         </ComputeScalar> 
        </RelOp> 
        <RelOp AvgRowSize="136" EstimateCPU="0.0001581" EstimateIO="0.003125" EstimateRebinds="7329.87" EstimateRewinds="0" EstimateRows="1" LogicalOp="RID Lookup" NodeId="11" Parallel="true" PhysicalOp="RID Lookup" EstimatedTotalSubtreeCost="24.0341" TableCardinality="107302000"> 
         <OutputList> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="RowIdent" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ControlGS" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="UPC_UCP" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="DateSold" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Price_UCP" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Store" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="QtySold" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="FileName" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="IssueCode" /> 
         <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="DealerNum" /> 
         </OutputList> 
         <RunTimeInformation> 
         <RunTimeCountersPerThread Thread="6" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         <RunTimeCountersPerThread Thread="2" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         <RunTimeCountersPerThread Thread="3" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         <RunTimeCountersPerThread Thread="7" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         <RunTimeCountersPerThread Thread="5" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         <RunTimeCountersPerThread Thread="4" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         <RunTimeCountersPerThread Thread="8" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         <RunTimeCountersPerThread Thread="1" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         <RunTimeCountersPerThread Thread="0" ActualRows="0" ActualEndOfScans="0" ActualExecutions="0" /> 
         </RunTimeInformation> 
         <IndexScan Lookup="true" Ordered="true" ScanDirection="FORWARD" ForcedIndex="false" ForceSeek="false" NoExpandHint="false"> 
         <DefinedValues> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="RowIdent" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="ControlGS" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="UPC_UCP" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="DateSold" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Price_UCP" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="Store" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="QtySold" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="FileName" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="IssueCode" /> 
          </DefinedValue> 
          <DefinedValue> 
          <ColumnReference Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" Column="DealerNum" /> 
          </DefinedValue> 
         </DefinedValues> 
         <Object Database="[NGDWDB]" Schema="[dbo]" Table="[SBT_POS_SALES]" TableReferenceId="-1" IndexKind="Heap" /> 
         <SeekPredicates> 
          <SeekPredicateNew> 
          <SeekKeys> 
           <Prefix ScanType="EQ"> 
           <RangeColumns> 
            <ColumnReference Column="Bmk1000" /> 
           </RangeColumns> 
           <RangeExpressions> 
            <ScalarOperator ScalarString="[Bmk1000]"> 
            <Identifier> 
             <ColumnReference Column="Bmk1000" /> 
            </Identifier> 
            </ScalarOperator> 
           </RangeExpressions> 
           </Prefix> 
          </SeekKeys> 
          </SeekPredicateNew> 
         </SeekPredicates> 
         </IndexScan> 
        </RelOp> 
        </NestedLoops> 
       </RelOp> 
       </Parallelism> 
      </RelOp> 
      <ParameterList> 
       <ColumnReference Column="@3" ParameterCompiledValue="(20120712)" ParameterRuntimeValue="(20120712)" /> 
       <ColumnReference Column="@2" ParameterCompiledValue="(852013688)" ParameterRuntimeValue="(852013688)" /> 
       <ColumnReference Column="@1" ParameterCompiledValue="(810)" ParameterRuntimeValue="(810)" /> 
      </ParameterList> 
      </QueryPlan> 
     </StmtSimple> 
     </Statements> 
    </Batch> 
    </BatchSequence> 
</ShowPlanXML> 
+0

有幾件事可能會放慢速度。 1.有多少列*返回? 2,你總是使用全部3列嗎?你也可能需要分開索引。你看過預計的執行計劃嗎? – Limey 2012-07-18 16:57:07

+0

需要更多信息。您的索引是否包含其他列或僅包含這三個列?索引是否聚集?如果沒有,是否有聚集索引?你有沒有檢查過(如果可以,你能發佈)執行計劃嗎? – mwigdahl 2012-07-18 16:57:19

+0

@Limey 1.十三,但爲什麼在這種情況下,因爲沒有行返回?是的。我正在研究執行計劃。 – 2012-07-18 17:03:56

回答

1

而不是保持填補了意見,我會正式將此作爲一個答案......

根據你的執行計劃,我建議上添加一個聚集索引桌子。很難100%確定發生了什麼,但RID查找是一個問題。

當沒有與該表關聯的集羣密鑰時,會發生RID查找。在非聚簇索引中,聚簇鍵在非聚簇索引中被複制(如果存在),可以在找到索引條目後加速所需的任何行訪問。在一個堆(非聚簇索引的表)中,你沒有該聚簇鍵,所以你必須在堆中查找它。

這就是爲什麼您的執行計劃中有一個INNER JOIN,即使SQL中沒有JOIN也是如此。將聚類應用於表中的主鍵應該消除這種連接,並使用簡單的索引查找。

儘管目前的計劃顯示它現在佔用了89%的時間,但我敢打賭,通過表格中的聚集索引,您可以將其簡化爲簡單(且更快)的索引查找。

編輯:

讓我看看你的執行計劃XML後,修改這個。雖然集羣關鍵點應該仍然有幫助,但真正的問題是索引中的數據類型。在索引查找我展示

CONVERT_IMPLICIT(int,[NGDWDB].[dbo].[SBT_POS_SALES].[Control],0)=(852013688) AND CONVERT_IMPLICIT(int,[NGDWDB].[dbo].[SBT_POS_SALES].[TransDate],0)=(20120712)

,這是告訴我,控制和TransDate是不相同的數據類型,你是在你的查詢提供。如果你解決這個問題,不需要轉換,你很可能會在查詢中看到巨大的加速。

+0

謝謝。如果你知道答案,這裏有一個側面的問題:如果我在這個表中做了一個批量插入(我明白忽略了這些索引),那麼對於我每次做一個或者定期重建索引都很重要也許一週一次,如果我每天大約30k行進行一次大容量插入)? – 2012-07-18 17:54:23

+1

另外一個選擇是隻選擇索引中包含的列 - 這將消除對任何「RID查找」的需要(或者一旦聚集索引就位時就進行「查詢」) – 2012-07-18 17:58:50

+0

啊哈,轉換是最大的罪魁禍首,做「選擇1」而不是*也有幫助。現在我有了我期待的直接結果。多謝你們! – 2012-07-18 18:11:43

相關問題