Skip to content

Instantly share code, notes, and snippets.

@taddeimania
Last active August 29, 2015 14:15
Show Gist options
  • Save taddeimania/1d72787a68e88a3209ef to your computer and use it in GitHub Desktop.
Save taddeimania/1d72787a68e88a3209ef to your computer and use it in GitHub Desktop.
Why for the other fast?
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?
@taddeimania
Copy link
Author

I actually think the 1st query isn't getting cached while the 2nd one is. Thoughts?

@sherzberg
Copy link

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.

@taddeimania
Copy link
Author

👍 you rock spencer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment