在解释计划中无法摆脱Key Lookup

时间:2015-12-15 14:46:12

标签: sql-server indexing sql-server-2012

我试图摆脱以下查询的解释计划中的Key Lookup操作:

            SELECT  s.CompanyId ,
                    t.PeriodEndDate ,
                    t.DurationId ,
                    s.conceptid AS SConceptId ,
                    c.ConceptId AS CConceptId,
                    t.NumOfPeriods ,
                    cast(cast(s.Value as numeric) as varchar(100)) as Value,
                    s.ConceptId * 17.0 AS ConceptOrdering , 
                    t.CompoundSortKeyLogicalKey,
                    1980 + (s.NumberOfQuarters / 4) AS FiscalYear,
                    (s.NumberOfQuarters % 4) + 1 AS FiscalQuarter,
                    cam.Alias
            FROM    [dbo].[TmpCompanyOrderedAndFilteredPKs] t
                    INNER JOIN [dbo].[synt_ScreenerDb_dbo_ScreenerHistoricalYTD_Number_t] s ON s.CompanyId = t.CompanyId
                                                          AND s.numberofquarters = t.numberofquarters  AND ( ( t.numberOfQuarters % 4 ) + 1 ) = 4 
                    INNER JOIN [##FinancialsConcepts7FD96D75-FCDB-44B0-9DED-6FE0BC128982] c ON c.ConceptMapId = s.ConceptId
                    LEFT JOIN dbo.ConceptAliasMapping cam ON cam.ConceptId = c.ConceptId
            WHERE   t.OperationGUID = '7FD96D75-FCDB-44B0-9DED-6FE0BC128982'

解释计划的屏幕截图: enter image description here

我尝试在以下列上创建索引:

Value, ConceptId, CompanyId, NumberOfQuarters

在INDEX和INCLUDE列上使用不同的组合。我错过了什么?

1 个答案:

答案 0 :(得分:1)

您的查询中有许多性能问题。按照下面提到的步骤来避免关键的查找。

在非聚集索引

中包含select语句中的所有列
        create nonclustered index ncli_1 on TmpCompanyOrderedAndFilteredPKs(CompanyId)
        include(PeriodEndDate,DurationId ,NumOfPeriods,CompoundSortKeyLogicalKey,numberofquarters )

        create nonclustered index ncli_2 on synt_ScreenerDb_dbo_ScreenerHistoricalYTD_Number_t(CompanyId)
        include(conceptid ,Value ,NumberOfQuarters )

        create nonclustered index ncli_3 on ##FinancialsConcepts7FD96D75-FCDB-44B0-9DED-6FE0BC128982(ConceptId)

    `create unique clustered index cli_4 on ##FinancialsConcepts7FD96D75-FCDB-44B0-9DED-6FE0BC128982(ConceptMapId)` -- This will make sql server use 
merge join` instead of hash join which will provide performance gain.