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
Solved

improving a query with many inner joins and grouping

Posted on 2013-10-22
7
168 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query assistance 16 36
average of calculation (TSQL) 4 26
Sql query with where clause 2 33
AJAX pass along a variable 3 46
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore 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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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