-1

I am running the following query:

It takes 5 min to return about 68000 records. But If I remove, the LKP_PrivateSource (i.e. The last Left Join) from the query, then it takes 1 SECOND to return 68000 records. BTY, there are no records in LKP_PrivateSource table. Any idea about what's causing the issue?

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 IsNull(PS.PSourceId,0)=IsNull(AE.PSourceID,0)
Where AE.ProjectID IN (13)
AND AE.LinkID IS NOT NULL

Please see below the plan:

  |--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)
9
  • 1
    Show your plans. SSMS, Query, Display Estimated Execution Plan. Jun 8, 2012 at 12:48
  • Use EXPLAIN and try to check where it fails.
    – aF.
    Jun 8, 2012 at 12:49
  • 2
    @aF.: there is no EXPLAIN in SQL Server
    – Quassnoi
    Jun 8, 2012 at 12:49
  • To produce the plan, please run SET SHOWPLAN_TEXT ON GO SELECT CASE ...
    – Quassnoi
    Jun 8, 2012 at 12:49
  • @Quassnoi yep, you got what I meant :P
    – aF.
    Jun 8, 2012 at 12:51

1 Answer 1

0

I think it is the fact that you are using functions on both sides of the comparison in the left join.

How to fix it will really depend on the business logic, but I'd look into maybe doing two or more left joins on that table.

One with a straight value to value comparison, and others to handle just the null value logic.

You would need to add them to your coalesce ordered by how they would fit the requirements.

1
  • It's not because of the IsNull, I have tried removing it. But the output stays the same.
    – user899055
    Jun 8, 2012 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.