Last active
August 29, 2015 14:15
-
-
Save taddeimania/1d72787a68e88a3209ef to your computer and use it in GitHub Desktop.
Why for the other fast?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select mt.id from massive_table mt | |
where not exists | |
(select id from big_table bt where bt.massive_table_id=mt.id); | |
select mt.id from massive_table mt | |
left join big_table bt on bt.massive_table_id = mt.id | |
where bt.massive_table_id is null; | |
-- The left join query appears to return results 100x faster. | |
-- Is the 2nd query that much more efficient than the 1st query or | |
-- is MySQL just caching the query better? |
Looks like query 1 is using a correlated subquery, where the inner query is using a column from the outer query. So for every row, you make another query to see if rows exist in the big_table. The left join will always be faster than a correlated subquery.
👍 you rock spencer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I actually think the 1st query isn't getting cached while the 2nd one is. Thoughts?