EXPLAIN ANALYZE for a query that takes about 3 secs.
This is consistent meaning each time I run it I get similar results.
Append (cost=224504.33..298195.43 rows=4264 width=32) (actual time=2164.210..2811.605 rows=151 loops=1)
-> Hash Anti Join (cost=224504.33..247949.29 rows=3309 width=32) (actual time=2164.208..2787.392 rows=85 loops=1)
Hash Cond: (topics.category_id = cu.category_id)
Join Filter: (tu.last_read_post_number IS NULL)
-> Hash Join (cost=224495.29..247898.44 rows=3315 width=36) (actual time=2164.146..2787.263 rows=85 loops=1)
Hash Cond: (topics.category_id = c.id)
Join Filter: (((NOT c.read_restricted) OR u.admin OR (hashed SubPlan 1)) AND (topics.created_at >= GREATEST(CASE WHEN (COALESCE(uo.new_topic_duration_minutes, 2880) = '-1'::integer) THEN u.created_at WHEN (COALESCE(uo.new_topic_duration_minutes, 2880) = '-2'::integer) THEN COALESCE(u.previous_visit_at, u.created_at) ELSE ('2018-02-01 08:14:09.813495'::timestamp without time zone - ('00:01:00'::interval * (COALESCE(uo.new_topic_duration_minutes, 2880))::double precision)) END, us.new_since, '2018-01-26 19:25:08'::timestamp without time zone)))
Rows Removed by Join Filter: 531113
-> Hash Right Join (cost=224440.81..247511.19 rows=11073 width=53) (actual time=2153.460..2416.891 rows=531198 loops=1)
Hash Cond: ((tu.user_id = u.id) AND (tu.topic_id = topics.id))
Filter: ((tu.last_read_post_number IS NULL) AND (COALESCE(tu.notification_level, 2) >= 2))
Rows Removed by Filter: 1284
-> Bitmap Heap Scan on topic_users tu (cost=230.39..23105.41 rows=10059 width=16) (actual time=2.795..32.127 rows=11147 loops=1)
Recheck Cond: (user_id = 29622)
Heap Blocks: exact=4085
-> Bitmap Index Scan on index_topic_users_on_user_id_and_topic_id (cost=0.00..227.87 rows=10059 width=0) (actual time=2.055..2.055 rows=11179 loops=1)
Index Cond: (user_id = 29622)
-> Hash (cost=213487.15..213487.15 rows=714885 width=45) (actual time=2097.736..2097.736 rows=532482 loops=1)
Buckets: 1048576 Batches: 1 Memory Usage: 53953kB
-> Nested Loop (cost=0.84..213487.15 rows=714885 width=45) (actual time=1.925..1786.062 rows=532482 loops=1)
Join Filter: (topics.visible OR u.admin OR u.moderator)
-> Nested Loop (cost=0.84..16.89 rows=1 width=26) (actual time=0.259..0.265 rows=1 loops=1)
-> Index Scan using users_pkey on users u (cost=0.42..8.44 rows=1 width=22) (actual time=0.120..0.122 rows=1 loops=1)
Index Cond: (id = 29622)
-> Index Scan using index_user_options_on_user_id on user_options uo (cost=0.42..8.44 rows=1 width=8) (actual time=0.131..0.134 rows=1 loops=1)
Index Cond: (user_id = 29622)
-> Seq Scan on topics (cost=0.00..206318.55 rows=715171 width=21) (actual time=1.650..1675.564 rows=532482 loops=1)
Filter: ((deleted_at IS NULL) AND (deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text))
Rows Removed by Filter: 23895
-> Hash (cost=40.08..40.08 rows=82 width=17) (actual time=0.387..0.387 rows=82 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 13kB
-> Nested Loop (cost=0.42..40.08 rows=82 width=17) (actual time=0.154..0.321 rows=82 loops=1)
-> Index Scan using user_stats_pkey on user_stats us (cost=0.42..8.44 rows=1 width=12) (actual time=0.131..0.133 rows=1 loops=1)
Index Cond: (user_id = 29622)
-> Seq Scan on categories c (cost=0.00..30.82 rows=82 width=5) (actual time=0.009..0.124 rows=82 loops=1)
SubPlan 1
-> Nested Loop (cost=0.69..13.37 rows=1 width=4) (never executed)
-> Nested Loop (cost=0.42..10.61 rows=1 width=4) (never executed)
Join Filter: (cg.group_id = gu.group_id)
-> Index Only Scan using index_group_users_on_user_id_and_group_id on group_users gu (cost=0.42..8.44 rows=1 width=4) (never executed)
Index Cond: (user_id = 29622)
Heap Fetches: 0
-> Seq Scan on category_groups cg (cost=0.00..1.52 rows=52 width=8) (never executed)
-> Index Scan using categories_pkey on categories c2 (cost=0.27..2.75 rows=1 width=4) (never executed)
Index Cond: (id = cg.category_id)
Filter: read_restricted
-> Hash (cost=9.02..9.02 rows=2 width=4) (actual time=0.022..0.022 rows=0 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 8kB
-> Bitmap Heap Scan on category_users cu (cost=4.20..9.02 rows=2 width=4) (actual time=0.021..0.021 rows=0 loops=1)
Recheck Cond: ((user_id = 29622) AND (notification_level = 0))
-> Bitmap Index Scan on idx_category_users_u1 (cost=0.00..4.19 rows=2 width=0) (actual time=0.019..0.019 rows=0 loops=1)
Index Cond: ((user_id = 29622) AND (notification_level = 0))
-> Nested Loop Anti Join (cost=244.18..50203.50 rows=955 width=32) (actual time=3.616..24.173 rows=66 loops=1)
Join Filter: (tu_1.last_read_post_number IS NULL)
-> Nested Loop (cost=244.04..50033.59 rows=957 width=36) (actual time=3.594..23.965 rows=66 loops=1)
Join Filter: ((NOT c_1.read_restricted) OR u_1.admin OR (hashed SubPlan 2))
-> Nested Loop (cost=230.40..49702.88 rows=1066 width=33) (actual time=3.584..23.687 rows=66 loops=1)
Join Filter: (topics_1.visible OR u_1.admin OR u_1.moderator)
-> Nested Loop (cost=1.27..25.34 rows=1 width=6) (actual time=0.160..0.165 rows=1 loops=1)
-> Nested Loop (cost=0.84..16.89 rows=1 width=6) (actual time=0.119..0.122 rows=1 loops=1)
-> Index Scan using users_pkey on users u_1 (cost=0.42..8.44 rows=1 width=6) (actual time=0.029..0.030 rows=1 loops=1)
Index Cond: (id = 29622)
-> Index Only Scan using user_stats_pkey on user_stats us_1 (cost=0.42..8.44 rows=1 width=4) (actual time=0.087..0.088 rows=1 loops=1)
Index Cond: (user_id = 29622)
Heap Fetches: 1
-> Index Only Scan using index_user_options_on_user_id on user_options uo_1 (cost=0.42..8.44 rows=1 width=4) (actual time=0.038..0.040 rows=1 loops=1)
Index Cond: (user_id = 29622)
Heap Fetches: 1
-> Nested Loop (cost=229.14..49666.88 rows=1066 width=33) (actual time=3.413..23.485 rows=66 loops=1)
-> Bitmap Heap Scan on topic_users tu_1 (cost=228.71..23128.88 rows=3353 width=16) (actual time=2.767..14.108 rows=691 loops=1)
Recheck Cond: (user_id = 29622)
Filter: (COALESCE(notification_level, 1) >= 2)
Rows Removed by Filter: 10456
Heap Blocks: exact=4085
-> Bitmap Index Scan on index_topic_users_on_user_id_and_topic_id (cost=0.00..227.87 rows=10059 width=0) (actual time=2.000..2.000 rows=11179 loops=1)
Index Cond: (user_id = 29622)
-> Index Scan using topics_pkey on topics topics_1 (cost=0.42..7.90 rows=1 width=21) (actual time=0.013..0.013 rows=0 loops=691)
Index Cond: (id = tu_1.topic_id)
Filter: ((deleted_at IS NULL) AND (deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text) AND (tu_1.last_read_post_number < highest_staff_post_number))
Rows Removed by Filter: 1
-> Index Scan using categories_pkey on categories c_1 (cost=0.27..0.29 rows=1 width=5) (actual time=0.003..0.003 rows=1 loops=66)
Index Cond: (id = topics_1.category_id)
SubPlan 2
-> Nested Loop (cost=0.69..13.37 rows=1 width=4) (never executed)
-> Nested Loop (cost=0.42..10.61 rows=1 width=4) (never executed)
Join Filter: (cg_1.group_id = gu_1.group_id)
-> Index Only Scan using index_group_users_on_user_id_and_group_id on group_users gu_1 (cost=0.42..8.44 rows=1 width=4) (never executed)
Index Cond: (user_id = 29622)
Heap Fetches: 0
-> Seq Scan on category_groups cg_1 (cost=0.00..1.52 rows=52 width=8) (never executed)
-> Index Scan using categories_pkey on categories c2_1 (cost=0.27..2.75 rows=1 width=4) (never executed)
Index Cond: (id = cg_1.category_id)
Filter: read_restricted
-> Index Only Scan using idx_category_users_u2 on category_users cu_1 (cost=0.14..0.17 rows=1 width=4) (actual time=0.002..0.002 rows=0 loops=66)
Index Cond: ((category_id = topics_1.category_id) AND (user_id = 29622) AND (notification_level = 0))
Heap Fetches: 0
Planning time: 25.623 ms
Execution time: 2815.374 ms