?
Solved

Query Performance

Posted on 2012-04-05
2
Medium Priority
?
236 Views
Last Modified: 2012-04-17
I’ve got the following query, it takes ages to run. Can any of you guys suggest any ideas to either improve the query or performance.

Basically I have three union joins each with the same joins. Can I rearrange the query so that the joins are only done at the end.


S1L1CT
     
      --,[76imwry Diwgnosis Cod1]
      ,L1FT([76imwry Diwgnosis Cod1],5) wS 76IMwRY_DIwGNOSIS_qqq10
        ,qqq_76IM_DIwG.qqq104NM wS 76IMwRY_DIwGNOSIS_D1SC
     
      --,[S1condwry Diwgnosis Cod1 1]
      ,L1FT([S1condwry Diwgnosis Cod1 1],5) wS [1ST_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C1_DIwG.qqq104NM wS S1C_DIwG1ST_D1SC
     
      --,[S1condwry Diwgnosis Cod1 2]
      ,L1FT([S1condwry Diwgnosis Cod1 2],5) wS [2ND_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C2_DIwG.qqq104NM wS S1C_DIwG2ND_D1SC
     
      --,[S1condwry Diwgnosis Cod1 3]
      ,L1FT([S1condwry Diwgnosis Cod1 3],5) wS [3RD_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C3_DIwG.qqq104NM wS S1C_DIwG3RD_D1SC
     
      --,[S1condwry Diwgnosis Cod1 4]
      ,L1FT([S1condwry Diwgnosis Cod1 4],5) wS [4TH_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C4_DIwG.qqq104NM wS S1C_DIwG4TH_D1SC
     

      ,[S1condwry 76oc1dur1 Cod1 1]
      ,[S1condwry 76oc1dur1 Dwt1 1]
      ,[S1condwry 76oc1dur1 Cod1 2]
      ,[S1condwry 76oc1dur1 Dwt1 2]
      ,[S1condwry 76oc1dur1 Cod1 3]
      ,[S1condwry 76oc1dur1 Dwt1 3]
      ,[S1condwry 76oc1dur1 Cod1 4]
      ,[S1condwry 76oc1dur1 Dwt1 4]
      ,[S1condwry 76oc1dur1 Cod1 5]
      ,[S1condwry 76oc1dur1 Dwt1 5]
      ,[S1condwry 76oc1dur1 Cod1 6]
      ,[S1condwry 76oc1dur1 Dwt1 6]
      ,[S1condwry 76oc1dur1 Cod1 7]
      ,[S1condwry 76oc1dur1 Dwt1 7]
      ,[S1condwry 76oc1dur1 Cod1 8]
      ,[S1condwry 76oc1dur1 Dwt1 8]
      ,[S1condwry 76oc1dur1 Cod1 9]
      ,[S1condwry 76oc1dur1 Dwt1 9]
      ,[S1condwry 76oc1dur1 Cod1 10]
      ,[S1condwry 76oc1dur1 Dwt1 10]
      ,[S1condwry 76oc1dur1 Cod1 11]
      ,[S1condwry 76oc1dur1 Dwt1 11]
      ,[S1condwry 76oc1dur1 Cod1 12]
      ,[S1condwry 76oc1dur1 Dwt1 12]

            
FROM  
            --[CBS_DW].[TISDwtw].[PbR].[sus_PbR_0910_PostR1con_1p]

            [CBS_DW].[TISDwtw].[5NG_CL1wR].[SUS_PbR_0910_PostR1CON_1P] Clr --Cl1wr 5NG Dwtw
                  RIGHT JOIN
                  
            [CBS_DW].[TISDwtw].[PbR].[SUS_PbR_0910_PostR1CON_1P] PbR       --PostR1CON 1pisod1s
                  ON (Clr.[G1n1rwt1d R1cord ID]=PbR.[G1n1rwt1d R1cord ID]
                  wND Clr.[PBR P1riod]=PbR.[PBR P1riod])

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_76IM_DIwG
                  ON qqq_76IM_DIwG.qqq104CD = L1FT([76imwry Diwgnosis Cod1],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C1_DIwG
                  ON qqq_S1C1_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 1],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C2_DIwG
                  ON qqq_S1C2_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 2],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C3_DIwG
                  ON qqq_S1C3_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 3],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C4_DIwG
                  ON qqq_S1C4_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 4],4)

      UNION wLL

      
S1L1CT  

     
      --,[76imwry Diwgnosis Cod1]
      ,L1FT([76imwry Diwgnosis Cod1],5) wS 76IMwRY_DIwGNOSIS_qqq10
        ,qqq_76IM_DIwG.qqq104NM wS 76IMwRY_DIwGNOSIS_D1SC
     
      --,[S1condwry Diwgnosis Cod1 1]
      ,L1FT([S1condwry Diwgnosis Cod1 1],5) wS [1ST_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C1_DIwG.qqq104NM wS S1C_DIwG1ST_D1SC
     
      --,[S1condwry Diwgnosis Cod1 2]
      ,L1FT([S1condwry Diwgnosis Cod1 2],5) wS [2ND_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C2_DIwG.qqq104NM wS S1C_DIwG2ND_D1SC
     
      --,[S1condwry Diwgnosis Cod1 3]
      ,L1FT([S1condwry Diwgnosis Cod1 3],5) wS [3RD_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C3_DIwG.qqq104NM wS S1C_DIwG3RD_D1SC
     
      --,[S1condwry Diwgnosis Cod1 4]
      ,L1FT([S1condwry Diwgnosis Cod1 4],5) wS [4TH_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C4_DIwG.qqq104NM wS S1C_DIwG4TH_D1SC
     

      ,[S1condwry 76oc1dur1 Cod1 1]
      ,[S1condwry 76oc1dur1 Dwt1 1]
      ,[S1condwry 76oc1dur1 Cod1 2]
      ,[S1condwry 76oc1dur1 Dwt1 2]
      ,[S1condwry 76oc1dur1 Cod1 3]
      ,[S1condwry 76oc1dur1 Dwt1 3]
      ,[S1condwry 76oc1dur1 Cod1 4]
      ,[S1condwry 76oc1dur1 Dwt1 4]
      ,[S1condwry 76oc1dur1 Cod1 5]
      ,[S1condwry 76oc1dur1 Dwt1 5]
      ,[S1condwry 76oc1dur1 Cod1 6]
      ,[S1condwry 76oc1dur1 Dwt1 6]
      ,[S1condwry 76oc1dur1 Cod1 7]
      ,[S1condwry 76oc1dur1 Dwt1 7]
      ,[S1condwry 76oc1dur1 Cod1 8]
      ,[S1condwry 76oc1dur1 Dwt1 8]
      ,[S1condwry 76oc1dur1 Cod1 9]
      ,[S1condwry 76oc1dur1 Dwt1 9]
      ,[S1condwry 76oc1dur1 Cod1 10]
      ,[S1condwry 76oc1dur1 Dwt1 10]
      ,[S1condwry 76oc1dur1 Cod1 11]
      ,[S1condwry 76oc1dur1 Dwt1 11]
      ,[S1condwry 76oc1dur1 Cod1 12]
      ,[S1condwry 76oc1dur1 Dwt1 12]

      
FROM
            --[CBS_DW].[TISDwtw].[PbR].[SUS_PBR_1011_POSTR1CON_1P]

            [CBS_DW].[TISDwtw].[5NG_CL1wR].[SUS_PbR_1011_PostR1CON_1P] Clr --Cl1wr 5NG Dwtw
                  RIGHT JOIN
                  
            [CBS_DW].[TISDwtw].[PbR].[SUS_PbR_1011_PostR1CON_1P] PbR       --PostR1CON 1pisod1s
                  ON (Clr.[G1n1rwt1d R1cord ID]=PbR.[G1n1rwt1d R1cord ID]
                  wND Clr.[PBR P1riod]=PbR.[PBR P1riod])

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_76IM_DIwG
                  ON qqq_76IM_DIwG.qqq104CD = L1FT([76imwry Diwgnosis Cod1],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C1_DIwG
                  ON qqq_S1C1_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 1],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C2_DIwG
                  ON qqq_S1C2_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 2],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C3_DIwG
                  ON qqq_S1C3_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 3],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C4_DIwG
                  ON qqq_S1C4_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 4],4)


      UNION wLL

S1L1CT



      --,[76imwry Diwgnosis Cod1]
      ,L1FT([76imwry Diwgnosis Cod1],5) wS 76IMwRY_DIwGNOSIS_qqq10
        ,qqq_76IM_DIwG.qqq104NM wS 76IMwRY_DIwGNOSIS_D1SC

      --,[S1condwry Diwgnosis Cod1 1]
      ,L1FT([S1condwry Diwgnosis Cod1 1],5) wS [1ST_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C1_DIwG.qqq104NM wS S1C_DIwG1ST_D1SC
     
      --,[S1condwry Diwgnosis Cod1 2]
      ,L1FT([S1condwry Diwgnosis Cod1 2],5) wS [2ND_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C2_DIwG.qqq104NM wS S1C_DIwG2ND_D1SC
     
      --,[S1condwry Diwgnosis Cod1 3]
      ,L1FT([S1condwry Diwgnosis Cod1 3],5) wS [3RD_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C3_DIwG.qqq104NM wS S1C_DIwG3RD_D1SC
     
      --,[S1condwry Diwgnosis Cod1 4]
      ,L1FT([S1condwry Diwgnosis Cod1 4],5) wS [4TH_S1C_DIwGNOSIS_qqq10]
        ,qqq_S1C4_DIwG.qqq104NM wS S1C_DIwG4TH_D1SC
     

      ,[S1condwry 76oc1dur1 Cod1 1]
      ,[S1condwry 76oc1dur1 Dwt1 1]
      ,[S1condwry 76oc1dur1 Cod1 2]
      ,[S1condwry 76oc1dur1 Dwt1 2]
      ,[S1condwry 76oc1dur1 Cod1 3]
      ,[S1condwry 76oc1dur1 Dwt1 3]
      ,[S1condwry 76oc1dur1 Cod1 4]
      ,[S1condwry 76oc1dur1 Dwt1 4]
      ,[S1condwry 76oc1dur1 Cod1 5]
      ,[S1condwry 76oc1dur1 Dwt1 5]
      ,[S1condwry 76oc1dur1 Cod1 6]
      ,[S1condwry 76oc1dur1 Dwt1 6]
      ,[S1condwry 76oc1dur1 Cod1 7]
      ,[S1condwry 76oc1dur1 Dwt1 7]
      ,[S1condwry 76oc1dur1 Cod1 8]
      ,[S1condwry 76oc1dur1 Dwt1 8]
      ,[S1condwry 76oc1dur1 Cod1 9]
      ,[S1condwry 76oc1dur1 Dwt1 9]
      ,[S1condwry 76oc1dur1 Cod1 10]
      ,[S1condwry 76oc1dur1 Dwt1 10]
      ,[S1condwry 76oc1dur1 Cod1 11]
      ,[S1condwry 76oc1dur1 Dwt1 11]
      ,[S1condwry 76oc1dur1 Cod1 12]
      ,[S1condwry 76oc1dur1 Dwt1 12]

      
FROM      
            --[CBS_DW].[TISDwtw].[PbR].[SUS_PBR_1112_POSTR1CON_1P]

            [CBS_DW].[TISDwtw].[5NG_CL1wR].[SUS_PbR_1112_PostR1CON_1P] Clr --Cl1wr 5NG Dwtw
                  RIGHT JOIN
                  
            [CBS_DW].[TISDwtw].[PbR].[SUS_PbR_1112_PostR1CON_1P] PbR       --PostR1CON 1pisod1s
                  ON (Clr.[G1n1rwt1d R1cord ID]=PbR.[G1n1rwt1d R1cord ID]
                  wND Clr.[PBR P1riod]=PbR.[PBR P1riod])

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_76IM_DIwG
                  ON qqq_76IM_DIwG.qqq104CD = L1FT([76imwry Diwgnosis Cod1],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C1_DIwG
                  ON qqq_S1C1_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 1],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C2_DIwG
                  ON qqq_S1C2_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 2],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C3_DIwG
                  ON qqq_S1C3_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 3],4)

                  L1FT OUT1R JOIN
            [LookUp].[dbo].[tbl_qqq10] wS qqq_S1C4_DIwG
                  ON qqq_S1C4_DIwG.qqq104CD = L1FT([S1condwry Diwgnosis Cod1 4],4)
0
Comment
Question by:aneilg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
lcohan earned 1220 total points
ID: 37813955
First the query above is very hard to read but regardless here are a few things you can try:

Too many outer (RIGHE and LEFT) JOINS in UNION ALL statements - try see if you can use CTE (common table expressions) or sql VIEWs instead;
Check each subquery in a UNION ALL execution plan and make sure is using indexes.
0
 

Author Closing Comment

by:aneilg
ID: 37855758
thanks
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question