Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:21:50+00:00 2026-05-29T21:21:50+00:00

I have a performance issue regarding a quite simple query that run for more

  • 0

I have a performance issue regarding a quite simple query that run for more than 30 minutes:

SELECT   P.pID
    ,COUNT(T1.ID) AS NB1
    ,COUNT(T2.ID) AS NB2
    ,COUNT(T3.ID) AS NB3
    ,COUNT(T4.ID) AS NB4
    ,COUNT(T5.ID) AS NB5

FROM MainTable P 

LEFT OUTER JOIN Table1 T1 ON  P.pID = T1.pID
LEFT OUTER JOIN Table2 T2 ON  P.pID = T2.pID
LEFT OUTER JOIN Table3 T3 ON  P.pID = T3.pID
LEFT OUTER JOIN Table4 T4 ON  P.pID = T4.pID
LEFT OUTER JOIN Table5 T5 ON  P.pID = T5.pID

GROUP BY P.pID

Where as each query would reply in few ms:

ex.

  SELECT   P.pID
        ,COUNT(T1.ID) AS NB1

    FROM MainTable P 
    LEFT OUTER JOIN Table1 T1 ON  P.pID = T1.pID

    GROUP BY P.pID

If I don’t use any aggregation (COUNT or anything else) the query run in few ms:
ex.
SELECT P.pID

FROM MainTable P 

LEFT OUTER JOIN Table1 T1 ON  P.pID = T1.pID
LEFT OUTER JOIN Table2 T2 ON  P.pID = T2.pID
LEFT OUTER JOIN Table3 T3 ON  P.pID = T3.pID
LEFT OUTER JOIN Table4 T4 ON  P.pID = T4.pID
LEFT OUTER JOIN Table5 T5 ON  P.pID = T5.pID

GROUP BY P.pID

Obviously all indexes are set etc…
The only “slowing down” element is that pID is a varchar (50) but I can’t change it and in my opinion this is not be the main problem here.

I used a workaround including union all that work fine but I really wondering why these is so long and how could I optimize this as aggregating over multiple left join are really common stuff in reporting project and should not be so slow.

thank you for your help.

[EDIT]
thx to ARION i got a nice query working really nice.

But my main concern is about understanding what’s wrong in the sql engine writing the query with multiple left join.

Tables descr would be :

Table P (500 rows) 
pID varchar(50) NOT NULL as primary key
p.* doesn't matter 

Table Tn (between 2000 and 8000 rows)
Tn.ID int NOT NULL as primary key 
pID varchar(50) NOT NULL as Foreign key

[EDIT] Thanks to Erland Sommarskog on social.msdn.microsoft.com that point me my error of analyse.
— detail about the answer

Keep in mind :
LEFT JOIN forms cartesian product

I was wrong assuming that the Cartesian product might have been filtered as I always refere to the same table.

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:21:52+00:00Added an answer on May 29, 2026 at 9:21 pm

    Maybe something like this:

    SELECT
        P.pID,
        (SELECT COUNT(*) FROM Table1 T1 WHERE P.pID = T1.pID) AS NB1,
        (SELECT COUNT(*) FROM Table2 T2 WHERE P.pID = T2.pID) AS NB2,
        (SELECT COUNT(*) FROM Table3 T3 WHERE P.pID = T3.pID) AS NB3,
        (SELECT COUNT(*) FROM Table4 T4 WHERE P.pID = T4.pID) AS NB4,
        (SELECT COUNT(*) FROM Table5 T5 WHERE P.pID = T5.pID) AS NB5
    
    FROM MainTable P 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that exceptions have a performance penalty, and that it's generally more efficient
I have a web design issue regarding to performance to ask advice. On a
I've got an performance issue in my application using a MSSQL DB. I have
I have a performance problem on a query. First table is a Customer table
I have a question regarding performance. Is there a performance penalty when I call
Does anyone have any experience that indicates what kind of performance hit a developer
A little question regarding performance in a Java web app. Let's assume I have
Application I am working on an MMO and have run into an issue. The
EDIT: I have added Slug column to address performance issues on specific record selection.
I just started out with C and have very little knowledge about performance issues

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.