Files
mtusi2026lab2/evidence/lab2_track2_explain.md
T

7.0 KiB

Трек 2. EXPLAIN (ANALYZE, BUFFERS): 3 варианта индекса

Данные: 160 001 сообщение, неравномерные чаты (61818 / 54545 / 18182 / 9777 / … / 1). Запрос: SELECT ... FROM messages WHERE chat_id = <id> AND (created_at, id) > (<середина>, …) ORDER BY created_at, id LIMIT 51. Большой чат ~62k, маленький чат ~1.

1. Без индекса, большой чат

Parallel Seq Scan по всей таблице, затем Sort (top-N heapsort). Прочитано ~2013 буферов. Execution Time = 39.988 ms.

Limit  (cost=4887.94..4893.75 rows=51 width=69) (actual time=36.242..39.932 rows=51.00 loops=1)
  Buffers: shared hit=2028
  ->  Gather Merge  (cost=4887.94..6211.25 rows=11611 width=69) (actual time=36.240..39.926 rows=51.00 loops=1)
        Workers Planned: 1
        Workers Launched: 1
        Buffers: shared hit=2028
        ->  Sort  (cost=3887.93..3905.01 rows=6830 width=69) (actual time=7.197..7.202 rows=25.50 loops=2)
              Sort Key: created_at, id
              Sort Method: top-N heapsort  Memory: 35kB
              Buffers: shared hit=2028
              Worker 0:  Sort Method: quicksort  Memory: 25kB
              ->  Parallel Seq Scan on messages  (cost=0.00..3660.07 rows=6830 width=69) (actual time=3.983..6.075 rows=20000.00 loops=2)
                    Filter: ((chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
                    Rows Removed by Filter: 60000
                    Buffers: shared hit=2013
Planning:
  Buffers: shared hit=88
Planning Time: 1.550 ms
Execution Time: 39.988 ms

2. Без индекса, маленький чат

Тот же Parallel Seq Scan по всей таблице, но условий мало — Sort (quicksort). Execution Time = 41.093 ms.

Limit  (cost=4660.63..4660.64 rows=5 width=69) (actual time=36.246..41.037 rows=1.00 loops=1)
  Buffers: shared hit=2019
  ->  Sort  (cost=4660.63..4660.64 rows=5 width=69) (actual time=36.242..41.032 rows=1.00 loops=1)
        Sort Key: created_at, id
        Sort Method: quicksort  Memory: 25kB
        Buffers: shared hit=2019
        ->  Gather  (cost=1000.00..4660.57 rows=5 width=69) (actual time=0.522..41.001 rows=1.00 loops=1)
              Workers Planned: 1
              Workers Launched: 1
              Buffers: shared hit=2013
              ->  Parallel Seq Scan on messages  (cost=0.00..3660.07 rows=3 width=69) (actual time=0.052..5.988 rows=0.50 loops=2)
                    Filter: ((chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
                    Rows Removed by Filter: 80000
                    Buffers: shared hit=2013
Planning:
  Buffers: shared hit=88
Planning Time: 1.837 ms
Execution Time: 41.093 ms

3. Индекс (chat_id), большой чат

Bitmap Index Scan по idx_messages_chat_only, затем Bitmap Heap Scan и Sort. ~863 буфера. Execution Time = 9.220 ms.

Limit  (cost=4135.73..4135.86 rows=51 width=69) (actual time=9.172..9.177 rows=51.00 loops=1)
  Buffers: shared hit=863
  ->  Sort  (cost=4135.73..4164.10 rows=11346 width=69) (actual time=9.169..9.172 rows=51.00 loops=1)
        Sort Key: created_at, id
        Sort Method: top-N heapsort  Memory: 35kB
        Buffers: shared hit=863
        ->  Bitmap Heap Scan on messages  (cost=676.65..3757.21 rows=11346 width=69) (actual time=2.808..6.995 rows=40000.00 loops=1)
              Recheck Cond: (chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))
              Rows Removed by Filter: 21818
              Heap Blocks: exact=803
              Buffers: shared hit=857
              ->  Bitmap Index Scan on idx_messages_chat_only  (cost=0.00..673.82 rows=61003 width=0) (actual time=0.873..0.873 rows=61818.00 loops=1)
                    Index Cond: (chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid)
                    Index Searches: 1
                    Buffers: shared hit=54
Planning:
  Buffers: shared hit=100
Planning Time: 1.562 ms
Execution Time: 9.220 ms

4. Индекс (chat_id), маленький чат

Index Scan using idx_messages_chat_only + Sort (quicksort). ~9 буферов. Execution Time = 0.069 ms.

Limit  (cost=28.62..28.64 rows=11 width=69) (actual time=0.040..0.041 rows=1.00 loops=1)
  Buffers: shared hit=9
  ->  Sort  (cost=28.62..28.64 rows=11 width=69) (actual time=0.038..0.038 rows=1.00 loops=1)
        Sort Key: created_at, id
        Sort Method: quicksort  Memory: 25kB
        Buffers: shared hit=9
        ->  Index Scan using idx_messages_chat_only on messages  (cost=0.29..28.43 rows=11 width=69) (actual time=0.017..0.017 rows=1.00 loops=1)
              Index Cond: (chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid)
              Filter: (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))
              Index Searches: 1
              Buffers: shared hit=3
Planning:
  Buffers: shared hit=100
Planning Time: 1.450 ms
Execution Time: 0.069 ms

5. Полный индекс (chat_id, created_at, id), большой чат

Index Scan using idx_messages_chat_created, без Sort. ~52 буфера. Execution Time = 0.214 ms.

Limit  (cost=0.42..28.88 rows=51 width=69) (actual time=0.072..0.187 rows=51.00 loops=1)
  Buffers: shared hit=51 read=1
  ->  Index Scan using idx_messages_chat_created on messages  (cost=0.42..6391.22 rows=11452 width=69) (actual time=0.070..0.182 rows=51.00 loops=1)
        Index Cond: ((chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
        Index Searches: 1
        Buffers: shared hit=51 read=1
Planning:
  Buffers: shared hit=111
Planning Time: 2.685 ms
Execution Time: 0.214 ms

6. Полный индекс (chat_id, created_at, id), маленький чат

Index Scan без Sort. ~4 буфера. Execution Time = 0.072 ms.

Limit  (cost=0.42..28.71 rows=8 width=69) (actual time=0.029..0.030 rows=1.00 loops=1)
  Buffers: shared hit=4
  ->  Index Scan using idx_messages_chat_created on messages  (cost=0.42..28.71 rows=8 width=69) (actual time=0.026..0.027 rows=1.00 loops=1)
        Index Cond: ((chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
        Index Searches: 1
        Buffers: shared hit=4
Planning:
  Buffers: shared hit=111
Planning Time: 2.160 ms
Execution Time: 0.072 ms