SELECT *
FROM `resumes` AS r
LEFT JOIN seeker_data_cached AS c ON r.user_id = c.user_id
ORDER BY r.user_id
SELECT *
FROM `resumes` AS r
LEFT JOIN seeker_data_cached AS c ON r.user_id = c.user_id
ORDER BY c.user_id
r.user_id and c.user_id are indexed. Why do these queries have very different execution times and is there anything that can be done to speed up the second query?
The MySQL documentation contains a section on
ORDER BYoptimization that has, right near the top, a bulleted list of things to check when performance isn’t what you expect. So, first thing might be to go through that list.Many of them, we can rule out based on the information you’ve given, but many of them involve things we don’t know from your question but that you would probably know.
If that doesn’t help, run both queries through
EXPLAINand that may tell you why performance for one is different than the other.I’d be inclined to see what happens if you change
ON r.user_id = c.user_idin your second query toON c.user_id = r.user_idbased on the bit in the documentation linked above where it talks about the situation where “[t]he key used to fetch the rows is not the same as the one used in the ORDER BY”.