[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Query Performance

Posted on 2012-04-05
2
Medium Priority
?
243 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
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

829 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