【问题标题】:Can not get rid of Key Lookup in the explain plan无法摆脱解释计划中的 Key Lookup
【发布时间】:2016-03-21 09:57:46
【问题描述】:

我正在尝试摆脱以下查询的解释计划中的 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'

解释计划截图:

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

Value, ConceptId, CompanyId, NumberOfQuarters

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

【问题讨论】:

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


    【解决方案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.
    

    【讨论】:

    • 只需在 ScreenerHistoricalYTD_Number_t(CompanyId) 上创建非聚集索引 IX__ScreenerHistoricalYTD_Number_t_CompanyId --include(conceptid ,Value ,NumberOfQuarters ) 就足够了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    相关资源
    最近更新 更多