Solved

improving a query with many inner joins and grouping

Posted on 2013-10-22
7
170 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
[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
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 66

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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 74

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

630 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