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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:45:14+00:00 2026-06-10T15:45:14+00:00

I’ve got a pice of code which looks up a few CD’s in a

  • 0

I’ve got a pice of code which looks up a few CD’s in a database by their ID. This is done using an ‘IN’ condition :

(1) SELECT * FROM album WHERE id IN (?,?,?,?,?)

The next step is to get tracks associated with these albums. I do this by modifying the ‘base’ query a little.

(2) SELECT track.* FROM album 
LEFT JOIN track ON track.album_id = album.id 
WHERE album.id IN(?,?,?,?,?)

Now, I’ve got both albums and tracks. However, I need to load composer details for the tracks. For a couple of reasons, I can’t do it along with the above query, so I need to do it separately.

What I could do is to look up composers based on the tracks I’ve got, where I would use the track ids and look up composers in the composers table based on these track ids. Alternatively, I could modify the ‘base’ query even further and do one more join with the composers table. However; is there a general rule here (with regards to performance) which easily point out one of the queries as favourable? I’ve been testing a little, but I’ve done it on such a small scale that I can’t really see any difference…

(3) SELECT composer.* FROM album 
LEFT JOIN track ON track.album_id = album.id
LEFT JOIN composer ON composer.track_id = track.id
WHERE album.id IN (?,?,?,?,?)

…or…

[get track ids from query (2)]

(4) SELECT composer.* FROM composer
WHERE composer.track_id IN (?,...);

For the record: I’ve got indexes in place on all criteria and join columns.

  • 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-10T15:45:16+00:00Added an answer on June 10, 2026 at 3:45 pm

    There is no reason to use LEFT JOIN in query 3 (nice of you to number them) since you only care about composers. (In general outer joins are slower.)

    You don’t need to join the albums table in query 3 – just use the IN on track.album_id. (I assume you are not worried about rogue tracks with missing albums.)

    You mentioned you have indexes on various things. But remember that MySQL can only use one index per table per query. So if you have multiple things to check you must make a composite index.

    Surprisingly doing a join is usually faster then a large IN statement, this is because the values in the IN are not indexed, so MySQL can not do an index join on them. But this only applies with lots of values – for small number of them using IN could be faster.

    Personally, I would use the JOIN method until the point that you see this query becoming a problem. (Which would only happen if you had some very complex conditionals to check, that could get slow to do twice). The join is simpler code, and most likely will be super fast – so don’t make things more complicated without a specific reason.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
Does anyone know how can I replace this 2 symbol below from the string
I have a view passing on information from a database: def serve_article(request, id): served_article

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.