Skip to content

Instantly share code, notes, and snippets.

@lfittl
Created September 3, 2009 17:55
Show Gist options
  • Select an option

  • Save lfittl/180423 to your computer and use it in GitHub Desktop.

Select an option

Save lfittl/180423 to your computer and use it in GitHub Desktop.
soup_production=> EXPLAIN ANALYZE SELECT posts.id, posts.created_at FROM posts JOIN blogs ON (blogs.id = posts.blog_id) JOIN user_friend ON (blogs.user_id = user_friend.friend_id) WHERE user_friend.user_id = 2 ORDER BY posts.created_at DESC, posts.id DESC LIMIT 40;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..2192.72 rows=40 width=16) (actual time=0.584..46.198 rows=40 loops=1)
-> Nested Loop (cost=0.00..6837657.43 rows=124734 width=16) (actual time=0.583..46.186 rows=40 loops=1)
-> Nested Loop (cost=0.00..3748680.73 rows=24974683 width=24) (actual time=0.033..26.752 rows=5245 loops=1)
-> Index Scan Backward using posts_for_friendposts on posts (cost=0.00..2110713.79 rows=24992428 width=24) (actual time=0.020..6.823 rows=5245 loops=1)
-> Index Scan using blogs_pkey on blogs (cost=0.00..0.06 rows=1 width=16) (actual time=0.003..0.003 rows=1 loops=5245)
Index Cond: (blogs.id = posts.blog_id)
-> Index Scan using user_friend_user_id_key on user_friend (cost=0.00..0.12 rows=1 width=8) (actual time=0.003..0.003 rows=0 loops=5245)
Index Cond: ((user_friend.user_id = 2) AND (user_friend.friend_id = blogs.user_id))
Total runtime: 46.256 ms
Limit (cost=956756.72..956756.74 rows=40 width=16)
-> Sort (cost=956756.72..956819.06 rows=124697 width=16)
Sort Key: posts.created_at, posts.id
-> Hash Join (cost=2027.15..955968.39 rows=124697 width=16)
Hash Cond: (posts.blog_id = blogs.id)
-> Seq Scan on posts (cost=0.00..934823.72 rows=24991240 width=24)
-> Hash (cost=2026.59..2026.59 rows=161 width=8)
-> Hash Join (cost=1823.91..2026.59 rows=161 width=8)
Hash Cond: (user_friend.friend_id = blogs.user_id)
-> Bitmap Heap Scan on user_friend (cost=2.31..204.09 rows=161 width=8)
Recheck Cond: (user_id = 2)
-> Bitmap Index Scan on user_friend_user_id_key (cost=0.00..2.30 rows=161 width=0)
Index Cond: (user_id = 2)
-> Hash (cost=1708.74..1708.74 rows=32247 width=16)
-> Seq Scan on blogs (cost=0.00..1708.74 rows=32247 width=16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment