SQL Server 2008 - 连接导致性能下降

Posted

技术标签:

【中文标题】SQL Server 2008 - 连接导致性能下降【英文标题】:SQL Server 2008 - Join causing slow performance 【发布时间】:2012-06-08 13:40:09 【问题描述】:

我之前询问了以下查询,但由于未提供我不知道的查询计划,因此被否决了。这次我提供了查询计划,希望对如何改进以下查询提出一些建议/cmets:

以下查询需要 5 分钟才能返回大约 68000 条记录。但如果我从查询中删除 LKP_PrivateSource(即最后一个左连接),则需要 1 秒才能返回 68000 条记录。 BTY,LKP_PrivateSource 表中没有记录。知道导致问题的原因吗?

Select 
    Case IsNull(LNK.StockID,0)
        When 0 Then AE.StockID
        Else LNK.StockID
    End StockID,
    IsNull(AE.LinkID,0) LinkID,
    IsNull(LNK.CapitalID,0) CapitalID,
    DE.SourceName,
    AE.SourceDate 'Date',
    IsNull(AE.Formula,'') Formula
From 
    AE_RevenuData AE
Left Join 
    Linking LNK With(NoLock) ON LNK.LinkID = AE.LinkID
Inner Join 
    DE_DataEntities DE ON DE.EntityID = AE.EntityID
Inner Join 
    DataEntityIDs TE ON TE.EntityID = DE.EntityID
Inner Join 
    STG_LockedEntityData STG ON STG.StockID = IsNull(LNK.StockID, AE.StockID) AND STG.CapitalID = IsNull(LNK.CapitalID, 0) 
Left Join 
    LKP_PrivateSource PS ON PS.PSourceId = AE.PSourceID
Where 
    AE.ProjectID IN (13)
    AND AE.LinkID IS NOT NULL

请看下面的计划:

  |--Compute Scalar(DEFINE:([Expr1017]=CASE WHEN [Expr1026]=(0) THEN [MYDBNAME].[dbo].[AE_RevenueData].[StockID] as [AE].[StockID] ELSE [MYDBNAME].[dbo].[INV_InvestorFundLinking].[StockID] as [LNK].[StockID] END))
   |--Nested Loops(Left Outer Join, OUTER REFERENCES:([Expr1027]))
        |--Filter(WHERE:([MYDBNAME].[dbo].[STG_LockedEntityData].[StockID] as [STG].[StockID]=isnull([MYDBNAME].[dbo].[INV_InvestorFundLinking].[StockID] as [LNK].[StockID],[MYDBNAME].[dbo].[AE_RevenueData].[StockID] as [AE].[StockID]) AND [MYDBNAME].[dbo].[STG_LockedEntityData].[CapitalID] as [STG].[CapitalID]=[Expr1019]))
        |    |--Compute Scalar(DEFINE:([Expr1019]=isnull([MYDBNAME].[dbo].[INV_InvestorFundLinking].[CapitalID] as [LNK].[CapitalID],(0)), [Expr1026]=isnull([MYDBNAME].[dbo].[INV_InvestorFundLinking].[StockID] as [LNK].[StockID],(0))))
        |         |--Nested Loops(Left Outer Join, OUTER REFERENCES:([AE].[LinkID]))
        |              |--Nested Loops(Inner Join, WHERE:([MYDBNAME].[dbo].[AE_RevenueData].[LinkID] as [AE].[LinkID]=[MYDBNAME].[dbo].[STG_LockedEntityData].[LinkID] as [STG].[LinkID]))
        |              |    |--Sort(ORDER BY:([Expr1027] ASC))
        |              |    |    |--Hash Match(Inner Join, HASH:([TE].[EntityID])=([AE].[EntityID]))
        |              |    |         |--Nested Loops(Inner Join, OUTER REFERENCES:([Uniq1008], [DE].[ListingID]))
        |              |    |         |    |--Nested Loops(Inner Join, OUTER REFERENCES:([TE].[EntityID]))
        |              |    |         |    |    |--Table Scan(OBJECT:([MYDBNAME].[dbo].[DataEntityIDs] AS [TE]))
        |              |    |         |    |    |--Index Seek(OBJECT:([MYDBNAME].[dbo].[DE_DataEntities].[PK_DE_DataSources] AS [DE]), SEEK:([DE].[EntityID]=[MYDBNAME].[dbo].[DataEntityIDs].[EntityID] as [TE].[EntityID]) ORDERED FORWARD)
        |              |    |         |    |--Clustered Index Seek(OBJECT:([MYDBNAME].[dbo].[DE_DataEntities].[IX_DE_DataSources] AS [DE]), SEEK:([DE].[ListingID]=[MYDBNAME].[dbo].[DE_DataEntities].[ListingID] as [DE].[ListingID] AND [Uniq1008]=[Uniq1008]) LOOKUP ORDERED FORWARD)
        |              |    |         |--Compute Scalar(DEFINE:([Expr1018]=[MYDBNAME].[dbo].[AE_RevenueData].[LinkID] as [AE].[LinkID], [Expr1020]=isnull([MYDBNAME].[dbo].[AE_RevenueData].[Formula] as [AE].[Formula],''), [Expr1021]=CONVERT(bit,[MYDBNAME].[dbo].[AE_RevenueData].[IsSumOfFunds] as [AE].[IsSumOfFunds],0), [Expr1022]=isnull([MYDBNAME].[dbo].[AE_RevenueData].[ClientSpecificSource] as [AE].[ClientSpecificSource],N''), [Expr1023]=isnull([MYDBNAME].[dbo].[AE_RevenueData].[GenericSource] as [AE].[GenericSource],N''), [Expr1027]=isnull([MYDBNAME].[dbo].[AE_RevenueData].[PSourceID] as [AE].[PSourceID],(0))))
        |              |    |              |--Clustered Index Seek(OBJECT:([MYDBNAME].[dbo].[AE_RevenueData].[IX_AE_RevenueData] AS [AE]), SEEK:([AE].[ProjectID]=(13)),  WHERE:([MYDBNAME].[dbo].[AE_RevenueData].[LinkID] as [AE].[LinkID] IS NOT NULL) ORDERED FORWARD)
        |              |    |--Table Scan(OBJECT:([MYDBNAME].[dbo].[STG_LockedEntityData] AS [STG]))
        |              |--Index Seek(OBJECT:([MYDBNAME].[dbo].[INV_InvestorFundLinking].[PK_Linking_1] AS [LNK]), SEEK:([LNK].[LinkID]=[MYDBNAME].[dbo].[AE_RevenueData].[LinkID] as [AE].[LinkID]) ORDERED FORWARD)
        |--Clustered Index Seek(OBJECT:([MYDBNAME].[dbo].[LKP_PrivateSource].[PK_LKP_FactsetSource] AS [PS]), SEEK:([PS].[PSourceID]=[Expr1027]) ORDERED FORWARD)

请看下方未加入 LKP_PrivateSource 的计划

  |--Compute Scalar(DEFINE:([Expr1015]=CASE WHEN [Expr1024]=(0) THEN [MYDBNAME].[dbo].[AE_RevenueData].[StockID] as [AE].[StockID] ELSE [MYDBNAME].[dbo].[Linking].[StockID] as [LNK].[StockID] END))
   |--Nested Loops(Left Outer Join, OUTER REFERENCES:([Expr1027]))
       |--Filter(WHERE:([MYDBNAME].[dbo].[STG_LockedEntityData].[StockID] as [STG].[StockID]=isnull([MYDBNAME].[dbo].[Linking].[StockID] as [LNK].[StockID],[MYDBNAME].[dbo].[AE_RevenueData].[StockID] as [AE].[StockID]) AND [MYDBNAME].[dbo].[STG_LockedEntityData].[CapitalID] as [STG].[CapitalID]=[Expr1017]))
            |--Compute Scalar(DEFINE:([Expr1017]=isnull([MYDBNAME].[dbo].[Linking].[CapitalID] as [LNK].[CapitalID],(0)), [Expr1024]=isnull([MYDBNAME].[dbo].[Linking].[StockID] as [LNK].[StockID],(0))))
                 |--Nested Loops(Left Outer Join, OUTER REFERENCES:([AE].[LinkID]))
                      |--Hash Match(Inner Join, HASH:([AE].[LinkID])=([STG].[LinkID]))
                      |    |--Hash Match(Inner Join, HASH:([TE].[EntityID])=([AE].[EntityID]))
                      |    |    |--Nested Loops(Inner Join, OUTER REFERENCES:([Uniq1008], [DE].[ListingID]))
                      |    |    |    |--Nested Loops(Inner Join, OUTER REFERENCES:([TE].[EntityID]))
                      |    |    |    |    |--Table Scan(OBJECT:([MYDBNAME].[dbo].[DataEntityIDs] AS [TE]))
                      |    |    |    |    |--Index Seek(OBJECT:([MYDBNAME].[dbo].[DE_DataEntities].[PK_DE_DataSources] AS [DE]), SEEK:([DE].[EntityID]=[MYDBNAME].[dbo].[DataEntityIDs].[EntityID] as [TE].[EntityID]) ORDERED FORWARD)
                      |    |    |    |--Clustered Index Seek(OBJECT:([MYDBNAME].[dbo].[DE_DataEntities].[IX_DE_DataSources] AS [DE]), SEEK:([DE].[ListingID]=[MYDBNAME].[dbo].[DE_DataEntities].[ListingID] as [DE].[ListingID] AND [Uniq1008]=[Uniq1008]) LOOKUP ORDERED FORWARD)
                      |    |    |--Compute Scalar(DEFINE:([Expr1016]=[MYDBNAME].[dbo].[AE_RevenueData].[LinkID] as [AE].[LinkID], [Expr1018]=isnull([MYDBNAME].[dbo].[AE_RevenueData].[Formula] as [AE].[Formula],''), [Expr1019]=CONVERT(bit,[MYDBNAME].[dbo].[AE_RevenueData].[IsSumOfFunds] as [AE].[IsSumOfFunds],0), [Expr1020]=isnull([MYDBNAME].[dbo].[AE_RevenueData].[ClientSpecificSource] as [AE].[ClientSpecificSource],N''), [Expr1021]=isnull([MYDBNAME].[dbo].[AE_RevenueData].[GenericSource] as [AE].[GenericSource],N'')))
                      |    |         |--Clustered Index Seek(OBJECT:([MYDBNAME].[dbo].[AE_RevenueData].[IX_AE_RevenueData] AS [AE]), SEEK:([AE].[ProjectID]=(13)),  WHERE:([MYDBNAME].[dbo].[AE_RevenueData].[LinkID] as [AE].[LinkID] IS NOT NULL) ORDERED FORWARD)
                      |    |--Table Scan(OBJECT:([MYDBNAME].[dbo].[STG_LockedEntityData] AS [STG]))
                      |--Index Seek(OBJECT:([MYDBNAME].[dbo].[Linking].[PK_Linking_1] AS [LNK]), SEEK:([LNK].[LinkID]=[MYDBNAME].[dbo].[AE_RevenueData].[LinkID] as [AE].[LinkID]) ORDERED FORWARD)

【问题讨论】:

在没有连接的情况下执行查询计划,看看会发生什么。 您还应该在删除最后一个LEFT JOIN 时发布执行计划。此外,这可能更适合dba.stackexchange.com 如果您还没有这样做,您应该检查一些事情:(a) 过时的统计信息,(b) 统计信息样本量太小,(c) 索引碎片。 dbo.STG_LockedEntityDatadbo.DataEntityIDs 上的表扫描效果不佳。 是否所有外键列都被索引了?这适用于所有 【参考方案1】:

出于某种原因,将LEFT JOIN 添加到LKP_PrivateSource 会使优化器使用NESTED LOOPS 而不是HASH JOIN 加入STG_LockedEntityData(更早)。

很难说为什么会这样,但要改进嵌套循环,请在 STG_LockedEntityData (stock_id, capital_id) 上创建索引

【讨论】:

以上是关于SQL Server 2008 - 连接导致性能下降的主要内容,如果未能解决你的问题,请参考以下文章

SQL中多分支If语句的性能(Sql Server 2008)

在 SQL Server 2008 中使用 FOR XML PATH 提高 SQL 查询的性能

也谈SQL Server 2008 处理隐式数据类型转换在运行计划中的增强

ODBC 性能 Access 2010 和 SQL Server 2008 R2

sql server 2008中“Merge”子句的性能如何?

sqlserver2008无法连接