Can UNION ALL be faster than JOINs or do my JOINs just suck?

后端 未结 5 1032
旧时难觅i
旧时难觅i 2020-12-18 10:19

I have a Notes table with a uniqueidentifier column that I use as a FK for a variety of other tables in the database (don\'t worry, the uniqu

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 10:29

    The UNION ALL version would probably be satisfied quite easily by 2 index seeks. OR can lead to scans. What do the execution plans look like?

    Also have you tried this to avoid accessing Notes twice?

    ;WITH J AS
    (
    SELECT UniqueID FROM Leads WHERE LeadID = @LeadID
    UNION ALL
    SELECT UniqueID FROM Quotes WHERE LeadID = @LeadID
    )
    
    SELECT N.*  /*Don't use * though!*/
    FROM Notes N  
    JOIN J ON N.TargetUniqueID = J.UniqueID  
    

提交回复
热议问题