Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query Performance

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 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