Skip to content

Instantly share code, notes, and snippets.

@zerothabhishek
Last active December 1, 2015 03:19
Show Gist options
  • Save zerothabhishek/a8beba59082964a4af01 to your computer and use it in GitHub Desktop.
Save zerothabhishek/a8beba59082964a4af01 to your computer and use it in GitHub Desktop.
Explain Join with indexes
EXPLAIN
SELECT posts.*, comments.*
FROM posts LEFT JOIN comments
ON posts.id = comments.post_id;
# Before adding indexes
QUERY PLAN
------------------------------------------------------------------------
Hash Right Join (cost=394.50..2589.00 rows=50000 width=150)
Hash Cond: (comments.post_id = posts.id)
-> Seq Scan on comments (cost=0.00..1257.00 rows=50000 width=72)
-> Hash (cost=257.00..257.00 rows=11000 width=78)
-> Seq Scan on posts (cost=0.00..257.00 rows=11000 width=78)
# After adding indexes
QUERY PLAN
------------------------------------------------------------------------
Hash Right Join (cost=394.50..2589.00 rows=50000 width=150)
Hash Cond: (comments.post_id = posts.id)
-> Seq Scan on comments (cost=0.00..1257.00 rows=50000 width=72)
-> Hash (cost=257.00..257.00 rows=11000 width=78)
-> Seq Scan on posts (cost=0.00..257.00 rows=11000 width=78)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment