Solved

improving a query with many inner joins and grouping

Posted on 2013-10-22
7
166 Views
Last Modified: 2013-11-04
The below code is slow..  (12 seconds.. is it mainly because of joins? could the groupby be a concern?
0
Comment
Question by:25112
7 Comments
 
LVL 5

Author Comment

by:25112
ID: 39591908
SELECT     
   A.ProgramCode, 
   Reg.RegStatusDesc, 
   A.EPR, 
   A.PermAccount,
   A.FieldFK, 
   A.POI, 
   C.FieldName, 
   A.ManagerTypeCodeFK, 
   ReviewCode.ReviewDesc, 
   ReviewCode.StartProgramCodeFK, 
   ReviewCode.EndProgramCodeFK, 
   ReviewCode.ReviewCodePK
FROM         
(
SELECT DISTINCT 
   ProgramCode.ProgramCode, 
   RegStatusCode.RegStatusCodePK, 
   PermAccount.EPRCode AS EPR, 
   MIN (PermAccount.PermAccount) AS PermAccount, 
   PermAccount.ReviewCodeFK, 
   POI.FieldFK, 
   POI.ManagerName AS POI, 
   POI.ManagerTypeCodeFK
                                              
FROM          RegStatusCode
   INNER JOIN PermAccount ON RegStatusCode.RegStatusCodePK = PermAccount.RegStatusCodeFK 
   INNER JOIN  POIForm ON PermAccount.POIFormFK = POIForm.POIFormPK 
   INNER JOIN  ProgramCode ON POIForm.ProgramCodeFK = ProgramCode.ProgramCodePK 
   INNER JOIN  POI ON POIForm.IC_POIManagerFK = POI.IC_POIManagerFK 
   INNER JOIN  Field ON POI.FieldFK = Field.FieldPK
                                              
GROUP BY ProgramCode.ProgramCode, 
   RegStatusCode.RegStatusCodePK, 
   PermAccount.EPRCode, 
    PermAccount.ReviewCodeFK, 
   POI.FieldFK, 
   POI.ManagerName, 
   POI.ManagerTypeCodeFK ) AS A 
                       
    INNER JOIN ReviewCode ON A.ReviewCodeFK = ReviewCode.ReviewCodePK 
    INNER JOIN RegStatusCode AS Reg ON Reg.RegStatusCodePK = A.RegStatusCodePK 
    INNER JOIN Field AS C ON C.FieldPK = A.FieldFK

Open in new window

the reason for the additional joins at the end, were that when i brought in FieldName & RegStatusDesc, it was much slower.. so i pulled it into a seperate join with FieldFK & RegStatusCodePK, and they are faster. but overall, could use improvement.
0
 
LVL 10

Assisted Solution

by:PadawanDBA
PadawanDBA earned 167 total points
ID: 39591952
Have you taken a look at the execution plan to see if you are getting any table scans?
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 166 total points
ID: 39591953
Every join will add to the query cost.

If you wish, you can capture the XML execution plan and post it in this question, and we'll give it an eyeball.

DISTINCT and GROUP BY perform the same task, so that's one redundant step that can be removed.   This article may be a good read for you:  DISTINCT and GROUP BY... and why does it not work for my query? by SQL expert angelIII

Also, the A subquery has eight columns, but only six are used in the return set, so the remaining two can be removed from the SELECT.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Accepted Solution

by:
sdstuber earned 167 total points
ID: 39591958
why the distinct and the group by?

functionally it will do nothing, so it's safe, but if there was an intent to do something, maybe you're missing some functionality.

Is it necessary to create groups on the entire set of columns of the inner query?
or would it be sufficient to join to something smaller like this?

(SELECT permaccount.eprcode AS epr,
        MIN(permaccount.permaccount) AS permaccount,
        permaccount.reviewcodefk
   FROM permaccount)
0
 
LVL 5

Author Comment

by:25112
ID: 39592109
please see attached xml and plan
PDSR.sqlplan
PDSR.xml
0
 
LVL 5

Author Comment

by:25112
ID: 39592136
I will remove the Distinct.. thanks.

ReviewCodeFK is used to bring 4 records.
FieldFK brings 2 into the final results, from the subquery.

>>Is it necessary to create groups on the entire set of columns of the inner query?
Yes, according to business logic.. if it were possible, would you recommend to group a small set and then join it?
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now