Query Performance

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)
aneilgAsked:
Who is Participating?
 
lcohanConnect With a Mentor Database AnalystCommented:
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
 
aneilgAuthor Commented:
thanks
0
All Courses

From novice to tech pro — start learning today.