140 lines
6.7 KiB
Markdown
140 lines
6.7 KiB
Markdown
# Трек 2. EXPLAIN (ANALYZE, BUFFERS): 3 варианта индекса
|
|
|
|
Данные: 250002 сообщений, неравномерные чаты. Запрос: `SELECT ... FROM messages WHERE chat_id = <id> AND (created_at, id) > (<середина>, …) ORDER BY created_at, id LIMIT 51`.
|
|
Большой чат ~151818, маленький чат ~1.
|
|
|
|
## 1. Без индекса, большой чат
|
|
|
|
`Parallel Seq Scan` по всей таблице, затем `Sort`.
|
|
|
|
```
|
|
Limit (cost=6593.26..6599.20 rows=51 width=70) (actual time=10.966..12.790 rows=51.00 loops=1)
|
|
Buffers: shared hit=3210
|
|
-> Gather Merge (cost=6593.26..11539.13 rows=42466 width=70) (actual time=10.964..12.782 rows=51.00 loops=1)
|
|
Workers Planned: 2
|
|
Workers Launched: 2
|
|
Buffers: shared hit=3210
|
|
-> Sort (cost=5593.24..5637.48 rows=17694 width=70) (actual time=7.715..7.718 rows=39.00 loops=3)
|
|
Sort Key: created_at, id
|
|
Sort Method: top-N heapsort Memory: 36kB
|
|
Buffers: shared hit=3210
|
|
Worker 0: Sort Method: top-N heapsort Memory: 36kB
|
|
Worker 1: Sort Method: top-N heapsort Memory: 36kB
|
|
-> Parallel Seq Scan on messages (cost=0.00..5002.93 rows=17694 width=70) (actual time=3.301..5.795 rows=26666.67 loops=3)
|
|
Filter: ((chat_id = '362231ce-de25-4394-a32c-9dcddc4fabe0'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:00:32.39108+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
|
|
Rows Removed by Filter: 56667
|
|
Buffers: shared hit=3180
|
|
Planning:
|
|
Buffers: shared hit=88
|
|
Planning Time: 0.571 ms
|
|
Execution Time: 12.922 ms
|
|
```
|
|
|
|
## 2. Без индекса, маленький чат
|
|
|
|
Тот же `Parallel Seq Scan` по всей таблице.
|
|
|
|
```
|
|
Limit (cost=6003.04..6003.05 rows=1 width=70) (actual time=7.881..10.103 rows=1.00 loops=1)
|
|
Buffers: shared hit=3186
|
|
-> Sort (cost=6003.04..6003.05 rows=1 width=70) (actual time=7.879..10.099 rows=1.00 loops=1)
|
|
Sort Key: created_at, id
|
|
Sort Method: quicksort Memory: 25kB
|
|
Buffers: shared hit=3186
|
|
-> Gather (cost=1000.00..6003.03 rows=1 width=70) (actual time=7.667..10.055 rows=1.00 loops=1)
|
|
Workers Planned: 2
|
|
Workers Launched: 2
|
|
Buffers: shared hit=3180
|
|
-> Parallel Seq Scan on messages (cost=0.00..5002.93 rows=1 width=70) (actual time=3.431..4.199 rows=0.33 loops=3)
|
|
Filter: ((chat_id = '61004e52-701f-4f72-ab45-62c0a1ab97ec'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 14:24:06.811443+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
|
|
Rows Removed by Filter: 83334
|
|
Buffers: shared hit=3180
|
|
Planning:
|
|
Buffers: shared hit=88
|
|
Planning Time: 0.662 ms
|
|
Execution Time: 10.192 ms
|
|
```
|
|
|
|
## 3. Индекс (chat_id), большой чат
|
|
|
|
`Bitmap Index Scan` по `idx_messages_chat_only`, затем `Bitmap Heap Scan` и `Sort`.
|
|
|
|
```
|
|
Limit (cost=6594.20..6600.14 rows=51 width=70) (actual time=9.705..11.642 rows=51.00 loops=1)
|
|
Buffers: shared hit=3210
|
|
-> Gather Merge (cost=6594.20..11547.87 rows=42533 width=70) (actual time=9.688..11.619 rows=51.00 loops=1)
|
|
Workers Planned: 2
|
|
Workers Launched: 2
|
|
Buffers: shared hit=3210
|
|
-> Sort (cost=5594.17..5638.48 rows=17722 width=70) (actual time=6.872..6.874 rows=37.33 loops=3)
|
|
Sort Key: created_at, id
|
|
Sort Method: top-N heapsort Memory: 36kB
|
|
Buffers: shared hit=3210
|
|
Worker 0: Sort Method: top-N heapsort Memory: 36kB
|
|
Worker 1: Sort Method: top-N heapsort Memory: 37kB
|
|
-> Parallel Seq Scan on messages (cost=0.00..5002.93 rows=17722 width=70) (actual time=2.667..5.224 rows=26666.67 loops=3)
|
|
Filter: ((chat_id = '362231ce-de25-4394-a32c-9dcddc4fabe0'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:00:32.39108+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
|
|
Rows Removed by Filter: 56667
|
|
Buffers: shared hit=3180
|
|
Planning:
|
|
Buffers: shared hit=100
|
|
Planning Time: 0.552 ms
|
|
Execution Time: 11.745 ms
|
|
```
|
|
|
|
## 4. Индекс (chat_id), маленький чат
|
|
|
|
`Index Scan using idx_messages_chat_only` + `Sort`.
|
|
|
|
```
|
|
Limit (cost=32.15..32.16 rows=4 width=70) (actual time=0.055..0.056 rows=1.00 loops=1)
|
|
Buffers: shared hit=10
|
|
-> Sort (cost=32.15..32.16 rows=4 width=70) (actual time=0.054..0.055 rows=1.00 loops=1)
|
|
Sort Key: created_at, id
|
|
Sort Method: quicksort Memory: 25kB
|
|
Buffers: shared hit=10
|
|
-> Index Scan using idx_messages_chat_only on messages (cost=0.42..32.11 rows=4 width=70) (actual time=0.022..0.023 rows=1.00 loops=1)
|
|
Index Cond: (chat_id = '61004e52-701f-4f72-ab45-62c0a1ab97ec'::uuid)
|
|
Filter: (ROW(created_at, id) > ROW('2026-10-02 14:24:06.811443+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))
|
|
Index Searches: 1
|
|
Buffers: shared hit=4
|
|
Planning:
|
|
Buffers: shared hit=100
|
|
Planning Time: 0.642 ms
|
|
Execution Time: 0.112 ms
|
|
```
|
|
|
|
## 5. Полный индекс (chat_id, created_at, id), большой чат
|
|
|
|
`Index Scan using idx_messages_chat_created`, **без `Sort`**.
|
|
|
|
```
|
|
Limit (cost=0.42..17.05 rows=51 width=70) (actual time=0.093..0.150 rows=51.00 loops=1)
|
|
Buffers: shared hit=53 read=1
|
|
-> Index Scan using idx_messages_chat_created on messages (cost=0.42..13689.96 rows=41992 width=70) (actual time=0.092..0.146 rows=51.00 loops=1)
|
|
Index Cond: ((chat_id = '362231ce-de25-4394-a32c-9dcddc4fabe0'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:00:32.39108+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
|
|
Index Searches: 1
|
|
Buffers: shared hit=53 read=1
|
|
Planning:
|
|
Buffers: shared hit=111
|
|
Planning Time: 0.751 ms
|
|
Execution Time: 0.208 ms
|
|
```
|
|
|
|
## 6. Полный индекс (chat_id, created_at, id), маленький чат
|
|
|
|
`Index Scan` без `Sort`.
|
|
|
|
```
|
|
Limit (cost=0.42..8.44 rows=1 width=70) (actual time=0.028..0.029 rows=1.00 loops=1)
|
|
Buffers: shared hit=4
|
|
-> Index Scan using idx_messages_chat_created on messages (cost=0.42..8.44 rows=1 width=70) (actual time=0.027..0.028 rows=1.00 loops=1)
|
|
Index Cond: ((chat_id = '61004e52-701f-4f72-ab45-62c0a1ab97ec'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 14:24:06.811443+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
|
|
Index Searches: 1
|
|
Buffers: shared hit=4
|
|
Planning:
|
|
Buffers: shared hit=111
|
|
Planning Time: 0.846 ms
|
|
Execution Time: 0.105 ms
|
|
```
|