explain.depesz.com

PostgreSQL's explain analyze made readable

Result: M3V9 : Optimization for: plan #zGcF

Settings

Optimization path:

# exclusive inclusive rows x rows loops node
1. 0.382 18.820 ↑ 1.0 1 1

GroupAggregate (cost=228.23..271.63 rows=1 width=44) (actual time=18.819..18.820 rows=1 loops=1)

  • Group Key: s.user_id, s.question_set_id, qs.question_set_id
2.          

CTE subtopic_breakdown

3. 1.597 16.020 ↑ 40.0 5 1

HashAggregate (cost=193.68..195.68 rows=200 width=24) (actual time=16.007..16.020 rows=5 loops=1)

  • Group Key: q.topic_id, q.subtopic_id
4. 3.001 14.423 ↓ 4.0 1,000 1

Merge Join (cost=0.85..191.16 rows=252 width=16) (actual time=0.184..14.423 rows=1,000 loops=1)

  • Merge Cond: (q.question_id = q2.question_id)
5. 3.395 3.395 ↑ 1.0 2,000 1

Index Scan using ix_questions_question_set_id_question_id_answer_index on questions q (cost=0.28..85.97 rows=2,000 width=12) (actual time=0.037..3.395 rows=2,000 loops=1)

  • Index Cond: (question_set_id = 2)
6. 2.389 8.027 ↓ 2.0 1,000 1

GroupAggregate (cost=0.57..91.37 rows=504 width=8) (actual time=0.138..8.027 rows=1,000 loops=1)

  • Group Key: q2.question_id
7. 2.781 5.638 ↓ 2.0 1,000 1

Merge Join (cost=0.57..81.29 rows=504 width=12) (actual time=0.110..5.638 rows=1,000 loops=1)

  • Merge Cond: (q2.question_id = aq.question_id)
8. 1.751 1.751 ↑ 1.0 2,000 1

Index Only Scan using ix_questions_question_set_id_question_id_answer_index on questions q2 (cost=0.28..44.28 rows=2,000 width=8) (actual time=0.049..1.751 rows=2,000 loops=1)

  • Index Cond: (question_set_id = 2)
  • Heap Fetches: 0
9. 1.106 1.106 ↑ 1.0 1,000 1

Index Only Scan using ix_answered_questions_user_id_q_set_id_q_id_answer_index on subscriptions_questions_association aq (cost=0.29..24.47 rows=1,009 width=8) (actual time=0.044..1.106 rows=1,000 loops=1)

  • Index Cond: ((user_id = 1) AND (question_set_id = 2))
  • Heap Fetches: 0
10.          

Initplan (forGroupAggregate)

11. 0.011 0.011 ↑ 1.0 1 1

Seq Scan on question_counts_mv qc (cost=0.00..1.01 rows=1 width=1,444) (actual time=0.010..0.011 rows=1 loops=1)

12. 0.015 16.988 ↑ 1.0 5 1

Nested Loop (cost=31.54..34.71 rows=5 width=106) (actual time=16.970..16.988 rows=5 loops=1)

13. 0.009 0.113 ↑ 1.0 1 1

Nested Loop (cost=0.28..3.33 rows=1 width=74) (actual time=0.103..0.113 rows=1 loops=1)

14. 0.069 0.069 ↑ 1.0 1 1

Index Scan using pk_user_question_set_association on user_question_set_association s (cost=0.28..2.30 rows=1 width=24) (actual time=0.065..0.069 rows=1 loops=1)

  • Index Cond: ((user_id = 1) AND (question_set_id = 2))
15. 0.035 0.035 ↑ 1.0 1 1

Seq Scan on question_sets qs (cost=0.00..1.02 rows=1 width=50) (actual time=0.030..0.035 rows=1 loops=1)

  • Filter: (question_set_id = 2)
  • Rows Removed by Filter: 1
16. 0.020 16.860 ↑ 1.0 5 1

Sort (cost=31.26..31.28 rows=5 width=44) (actual time=16.858..16.860 rows=5 loops=1)

  • Sort Key: ts.name
  • Sort Method: quicksort Memory: 18kB
17.          

Initplan (forSort)

18. 0.012 0.012 ↑ 1.0 1 1

Seq Scan on question_counts_mv qc_1 (cost=0.00..1.01 rows=1 width=1,444) (actual time=0.011..0.012 rows=1 loops=1)

19. 0.283 16.828 ↑ 1.0 5 1

GroupAggregate (cost=29.92..30.20 rows=5 width=44) (actual time=16.647..16.828 rows=5 loops=1)

  • Group Key: ts.topic_id
20. 0.024 16.545 ↑ 1.0 5 1

Sort (cost=29.92..29.93 rows=5 width=44) (actual time=16.542..16.545 rows=5 loops=1)

  • Sort Key: ts.topic_id
  • Sort Method: quicksort Memory: 18kB
21. 0.044 16.521 ↑ 1.0 5 1

Hash Right Join (cost=26.83..29.86 rows=5 width=44) (actual time=16.493..16.521 rows=5 loops=1)

  • Hash Cond: (subtopic_breakdown.topic_id = ts.topic_id)
22. 0.008 16.427 ↑ 40.0 5 1

Sort (cost=25.64..26.14 rows=200 width=55) (actual time=16.424..16.427 rows=5 loops=1)

  • Sort Key: sts.name
  • Sort Method: quicksort Memory: 18kB
23.          

Initplan (forSort)

24. 0.015 0.015 ↑ 1.0 1 1

Seq Scan on question_counts_mv qc_2 (cost=0.00..1.01 rows=1 width=1,444) (actual time=0.014..0.015 rows=1 loops=1)

25. 0.243 16.404 ↑ 40.0 5 1

HashAggregate (cost=8.49..16.99 rows=200 width=55) (actual time=16.295..16.404 rows=5 loops=1)

  • Group Key: sts.subtopic_id, subtopic_breakdown.topic_id, subtopic_breakdown.subtopic_id
26. 0.037 16.161 ↑ 40.0 5 1

Hash Left Join (cost=1.45..5.99 rows=200 width=43) (actual time=16.129..16.161 rows=5 loops=1)

  • Hash Cond: (subtopic_breakdown.subtopic_id = sts.subtopic_id)
27. 16.040 16.040 ↑ 40.0 5 1

CTE Scan on subtopic_breakdown (cost=0.00..4.00 rows=200 width=24) (actual time=16.016..16.040 rows=5 loops=1)

28. 0.035 0.084 ↑ 1.0 20 1

Hash (cost=1.20..1.20 rows=20 width=19) (actual time=0.084..0.084 rows=20 loops=1)

  • Buckets: 1024 Batches: 1 Memory Usage: 5kB
29. 0.049 0.049 ↑ 1.0 20 1

Seq Scan on subtopics sts (cost=0.00..1.20 rows=20 width=19) (actual time=0.030..0.049 rows=20 loops=1)

30. 0.022 0.050 ↑ 1.0 5 1

Hash (cost=1.12..1.12 rows=5 width=12) (actual time=0.049..0.050 rows=5 loops=1)

  • Buckets: 1024 Batches: 1 Memory Usage: 5kB
31. 0.028 0.028 ↑ 1.0 5 1

Seq Scan on topics ts (cost=0.00..1.12 rows=5 width=12) (actual time=0.020..0.028 rows=5 loops=1)

  • Filter: (question_set_id = 2)
  • Rows Removed by Filter: 5
32.          

SubPlan (forGroupAggregate)

33. 0.086 1.424 ↑ 1.0 1 1

Aggregate (cost=39.05..39.06 rows=1 width=8) (actual time=1.424..1.424 rows=1 loops=1)

34. 1.338 1.338 ↑ 1.0 20 1

Seq Scan on user_question_set_association centile_rank_table (cost=0.00..39.00 rows=20 width=4) (actual time=1.300..1.338 rows=20 loops=1)

  • Filter: ((score IS NOT NULL) AND (question_set_id = s.question_set_id))
  • Rows Removed by Filter: 1980
35. 0.015 0.015 ↓ 0.0 0 1

Seq Scan on tests active_test (cost=0.00..1.02 rows=1 width=1) (actual time=0.015..0.015 rows=0 loops=1)

  • Filter: ((user_id = s.user_id) AND (question_set_id = s.question_set_id))