Solved

improving a query with many inner joins and grouping

Posted on 2013-10-22
7
165 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 73

Accepted Solution

by:
sdstuber earned 167 total points
Comment Utility
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
Comment Utility
please see attached xml and plan
PDSR.sqlplan
PDSR.xml
0
 
LVL 5

Author Comment

by:25112
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

12 Experts available now in Live!

Get 1:1 Help Now