Files
mtusi2026lab2/evidence/explain.md
2026-10-16 05:27:13 +03:00

100 lines
4.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# EXPLAIN (ANALYZE, BUFFERS) — индекс истории
- PostgreSQL 16 (локальный контейнер)
- Таблица `messages`: 15 000 строк в чате `22222222-2222-4222-8222-222222222222`
- `created_at` распределено равномерно по 30 дням назад
- `PAGE_LIMIT` = 50, `DEEP_OFFSET` = 7000
## Сводка
| # | Сценарий | Узел | Buffers | Время (ms) |
|---|---|---|---|---|
| 1 | Первая страница, с индексом | Index Scan (messages_chat_keyset_idx) | 4 | 0.32 |
| 2 | Первая страница, без индекса | Seq Scan + Sort top-N | 191 | 15.08 |
| 3 | Глубокая страница (keyset, OFFSET 7000), с индексом | Index Scan | 5 | 0.27 |
| 4 | OFFSET 7000 (контраст) | Index Scan (прочитал 7051 строку) | 178 | 4.42 |
**Выводы:**
- Индекс `messages_chat_keyset_idx` используется и даёт **47× выигрыш** по времени на первой странице (0.32 ms vs 15.08 ms).
- Keyset-пагинация **не деградирует с глубиной**: страница на OFFSET 7000 стоит столько же, сколько первая (0.27 vs 0.32 ms, 5 vs 4 буфера).
- OFFSET читает **все пропущенные строки** (7051 вместо 51) и в 16× медленнее keyset на той же глубине.
## 1. Первая страница, с индексом
```
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.29..5.52 rows=51 width=73) (actual time=0.179..0.230 rows=51 loops=1)
Buffers: shared hit=4
-> Index Scan using messages_chat_keyset_idx on messages (cost=0.29..1538.99 rows=15000 width=73) (actual time=0.177..0.220 rows=51 loops=1)
Index Cond: (chat_id = '22222222-2222-4222-8222-222222222222'::uuid)
Buffers: shared hit=4
Planning:
Buffers: shared hit=109
Planning Time: 1.835 ms
Execution Time: 0.321 ms
(9 rows)
```
## 2. Первая страница, без индекса (контраст)
```
BEGIN
DROP INDEX
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
Limit (cost=948.14..948.27 rows=51 width=73) (actual time=15.017..15.027 rows=51 loops=1)
Buffers: shared hit=191
-> Sort (cost=948.14..985.64 rows=15000 width=73) (actual time=15.015..15.019 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 38kB
Buffers: shared hit=191
-> Seq Scan on messages (cost=0.00..447.71 rows=15000 width=73) (actual time=0.079..7.720 rows=15000 loops=1)
Filter: (chat_id = '22222222-2222-4222-8222-222222222222'::uuid)
Rows Removed by Filter: 17
Buffers: shared hit=185
Planning:
Buffers: shared hit=79
Planning Time: 0.654 ms
Execution Time: 15.083 ms
(14 rows)
ROLLBACK
```
## 3. Глубокая страница (keyset, OFFSET 7000), с индексом
```
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.29..6.64 rows=51 width=73) (actual time=0.116..0.160 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_keyset_idx on messages (cost=0.29..997.87 rows=8007 width=73) (actual time=0.114..0.152 rows=51 loops=1)
Index Cond: ((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW('2026-10-10 09:09:28+00'::timestamp with time zone, '6b69ffd1-ea21-41a5-a222-e415775be088'::uuid)))
Buffers: shared hit=5
Planning:
Buffers: shared hit=106
Planning Time: 0.995 ms
Execution Time: 0.268 ms
(9 rows)
```
## 4. OFFSET 7000 (контраст)
```
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=718.35..723.58 rows=51 width=73) (actual time=4.338..4.373 rows=51 loops=1)
Buffers: shared hit=178
-> Index Scan using messages_chat_keyset_idx on messages (cost=0.29..1538.99 rows=15000 width=73) (actual time=0.105..4.010 rows=7051 loops=1)
Index Cond: (chat_id = '22222222-2222-4222-8222-222222222222'::uuid)
Buffers: shared hit=178
Planning:
Buffers: shared hit=107
Planning Time: 0.817 ms
Execution Time: 4.421 ms
(9 rows)
```