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 120113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:45:35+00:00 2026-05-11T03:45:35+00:00

I thought that I’ll be clever and use subquery to get my report in

  • 0

I thought that I’ll be clever and use subquery to get my report in one go. But after running into problems and reading documentation I saw that my approach does not work in MySQL. My inner query returns ~100 records and outer query scans 20000 records. When I restricted outer query to 20 records then it run 20 sec – really slow.

I wonder is it possible to restructure it somehow so that inner query wouldn’t be run EVERY time for every record in the outer query?

select p1.surname ,p1.name,p1.id,r1.start_date,r1.end_date,c1.short_name FROM ejl_players p1 left JOIN ejl_registration r1 ON ( r1.player_id = p1.id ) left JOIN ejl_teams t1 ON ( r1.team_id = t1.id ) left JOIN ejl_clubs c1 ON ( t1.club_id = c1.id ) where  r1.season=2008 and p1.id in  ( SELECT p.id FROM ejl_players p  left JOIN ejl_registration r ON (r.player_id = p.id)  left JOIN ejl_teams t ON (r.team_id = t.id)  left JOIN ejl_clubs c ON (t.club_id = c.id) WHERE r.season = 2008 GROUP BY p.id HAVING COUNT(DISTINCT c.id)  > 1 ) 

Explain (I restricted outer query to maximum 20 records:

id  select_type  table  type  possible_keys  key  key_len  ref  rows  Extra   1 PRIMARY p1 range PRIMARY PRIMARY 4 NULL 19 Using where  1 PRIMARY r1 ref team_id,season season 10 const,d17528sd14898.p1.id 1 Using where  1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 d17528sd14898.r1.team_id 1    1 PRIMARY c1 eq_ref PRIMARY PRIMARY 4 d17528sd14898.t1.club_id 1    2 DEPENDENT SUBQUERY p index PRIMARY PRIMARY 5 NULL 23395 Using index  2 DEPENDENT SUBQUERY r ref team_id,season season 10 const,d17528sd14898.p.id 1 Using where; Using index  2 DEPENDENT SUBQUERY t eq_ref PRIMARY PRIMARY 4 d17528sd14898.r.team_id 1    2 DEPENDENT SUBQUERY c eq_ref PRIMARY PRIMARY 4 d17528sd14898.t.club_id 1 Using index  
  • 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. 2026-05-11T03:45:36+00:00Added an answer on May 11, 2026 at 3:45 am

    Try using an INNER JOIN (something like this):

    SELECT p1.surname ,p1.name,p1.id,r1.start_date,r1.end_date,c1.short_name FROM ejl_players p1 INNER JOIN (     SELECT p.id     FROM ejl_players p      LEFT JOIN ejl_registration r ON (r.player_id = p.id)      LEFT JOIN ejl_teams t ON (r.team_id = t.id)      LEFT JOIN ejl_clubs c ON (t.club_id = c.id)     WHERE r.season = 2008     GROUP BY p.id     HAVING COUNT(DISTINCT c.id)  > 1 ) p2 ON p1.id = p2.id LEFT JOIN ejl_registration r1 ON ( r1.player_id = p1.id ) LEFT JOIN ejl_teams t1 ON ( r1.team_id = t1.id ) LEFT JOIN ejl_clubs c1 ON ( t1.club_id = c1.id ) WHERE  r1.season=2008 

    Using the subquery in this manner should be more efficient but isn’t always. However, it does bypass the issue of having the subquery executed for every record returned in the main query. Instead the subquery is constructed as a virtual table in memory and then used for comparison with the main query.

    Edit: I should point out that you’ll want to use EXPLAIN in MySQL to verify that this query is indeed performing more efficiently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought that I should use JSON for ID/pass storing format once, but I
I thought that I should use NetworkInterface::getDisplayName. I got some name, but this name
I thought that layout is just a widget that keeps more widgets inside. But
After I thought that I've understood how they work, I tried this: NSString *str1
I thought that i can use oauth to login to my AdSense account page
I thought that C# generated compiled code (by JIT), but I have lately discovered
I thought that the query below would naturally do what I explain, but apparently
I always thought that replacing the <center> tag with <div style=text-align:center;> will get me
I thought that :folddoc execute v:foldstart.p would work, but it only repeatedly returns the
I thought that I could use SimpleDB to take care of the most challenging

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.