Solved

Query Performance

Posted on 2012-04-05
2
230 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 305 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 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