Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

improving a query with many inner joins and grouping

Posted on 2013-10-22
7
Medium Priority
?
173 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
6 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 668 total points
ID: 39591952
Have you taken a look at the execution plan to see if you are getting any table scans?
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 664 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 74

Accepted Solution

by:
sdstuber earned 668 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Integration Management Part 2
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

824 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