我有一個包含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>
有幾件事可能會放慢速度。 1.有多少列*返回? 2,你總是使用全部3列嗎?你也可能需要分開索引。你看過預計的執行計劃嗎? – Limey 2012-07-18 16:57:07
需要更多信息。您的索引是否包含其他列或僅包含這三個列?索引是否聚集?如果沒有,是否有聚集索引?你有沒有檢查過(如果可以,你能發佈)執行計劃嗎? – mwigdahl 2012-07-18 16:57:19
@Limey 1.十三,但爲什麼在這種情況下,因爲沒有行返回?是的。我正在研究執行計劃。 – 2012-07-18 17:03:56