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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:06:06+00:00 2026-06-11T14:06:06+00:00

Consider the 2 MySQL queries: SELECT ue.userid,e.courseid FROM (SELECT id,courseid FROM mdl_enrol WHERE status

  • 0

Consider the 2 MySQL queries:

SELECT ue.userid,e.courseid 
FROM (SELECT id,courseid FROM mdl_enrol WHERE status = 0 AND courseid IN (46)) e 
INNER JOIN (SELECT enrolid,userid FROM mdl_user_enrolments ) ue ON ue.enrolid = e.id 
INNER JOIN (SELECT userid FROM mdl_userdata) ud ON ue.userid = ud.userid

—

SELECT ue.userid,e.courseid 
FROM mdl_enrol e 
INNER JOIN mdl_user_enrolments ue ON ue.enrolid = e.id 
INNER JOIN mdl_userdata ud ON ue.userid = ud.userid
WHERE e.status = 0 AND e.courseid IN (46)

The bottom query is much much faster than the top query, but why? I’ve read that to increase performance you should only select the columns you need. Also, to me the top query should perform better because in each JOIN, you’re reducing the amount of data you’re joining. Obviously my understanding of how databases work is wrong, but if anyone could clear this up it would be much appreciated. An EXPLAIN also confirms that the bottom query is much faster.

Many 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-06-11T14:06:07+00:00Added an answer on June 11, 2026 at 2:06 pm

    In the first query mysql should select a subset from mdl_enrol table and complete mdl_user_enrolments and mdl_userdata into the memory. So you select a lot of data in memory. After you’ve done that – you join the data. If there is not enough memory to put all the data until it’s joined and sent back to the client – then temporary table on the hard drive is created. Most likely mysql optimizer isn’t cool enough to fix your mistake and try to improve execution plan. That’s why it’s slow.

    Whereas for the second query mysql knows what exactly it needs to select and only selects the small amount of required data. In this scenario it’s possible to use indexes (assuming all the necessary indexes have been created). So it’s fast.

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

Sidebar

Related Questions

Consider the following MySQL statement SELECT AccountNumber, Currency, Balance FROM clients JOIN balances ON
Consider the following two queries: select a.*, c.* from account a join customer c
Consider these three mysql statements: select * from Users; select id, title, value from
Consider these two queries: SELECT *, MAX(age) AS maxAge FROM someTable ORDER BY age
Consider these two queries: SELECT *, 'b' AS b FROM someTable ORDER BY a
Here is my mysql table scheme; status table // has statusID, userID,date, subject, date
Consider the query: SELECT @xxx As thevar FROM (SELECT @xxx:=1) as temp Will the
So I don't consider myself a novice at MySQL but this one has me
First off, here's my SQL query: SELECT research_cost, tech_name, (SELECT research_cost FROM technologies WHERE
I have a Perl script that gets data from a MySQL database on one

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.