Files
mtusi2026lab2/evidence/explain.txt
T

58 lines
3.2 KiB
Plaintext
Raw 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.4
- 10000 сообщений в чате ccdc7cd6-da87-4327-a845-2c540305813c
- Таблица messages, индекс idx_messages_chat_created ON (chat_id, created_at, id)
- Запрос (keyset-пагинация, LIMIT 51):
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = 'ccdc7cd6-da87-4327-a845-2c540305813c'
AND (created_at, id) > ('2026-01-01T00:00:00Z'::timestamptz,
'00000000-0000-0000-0000-000000000000'::uuid)
ORDER BY created_at, id
LIMIT 51;
## С индексом (по умолчанию)
Limit (cost=0.29..4.49 rows=51 width=65) (actual time=0.033..0.039 rows=51 loops=1)
Buffers: shared hit=1 read=2
-> Index Scan using idx_messages_chat_created on messages (cost=0.29..824.21 rows=9999 width=65) (actual time=0.032..0.036 rows=51 loops=1)
Index Cond: ((chat_id = 'ccdc7cd6-da87-4327-a845-2c540305813c'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Buffers: shared hit=1 read=2
Planning:
Buffers: shared hit=78 read=1
Planning Time: 0.469 ms
Execution Time: 0.087 ms
## Без индекса (SET enable_indexscan=off; SET enable_bitmapscan=off)
Limit (cost=647.92..648.04 rows=51 width=65) (actual time=2.491..2.499 rows=51 loops=1)
Buffers: shared hit=136
-> Sort (cost=647.92..672.91 rows=9999 width=65) (actual time=2.489..2.492 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 35kB
Buffers: shared hit=136
-> Seq Scan on messages (cost=0.00..314.33 rows=9999 width=65) (actual time=0.012..1.395 rows=10000 loops=1)
Filter: ((chat_id = 'ccdc7cd6-da87-4327-a845-2c540305813c'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Rows Removed by Filter: 533
Buffers: shared hit=130
Planning Time: 0.071 ms
Execution Time: 2.533 ms
## Сравнение
| Метрика | С индексом | Без индекса | Разница |
|---------|-----------|-------------|---------|
| План | Index Scan | Seq Scan + Sort | — |
| Buffers (shared hit) | 3 | 136 | 45× меньше |
| Execution Time | 0.087 ms | 2.533 ms | 29× быстрее |
| Rows scanned | 51 (только нужные) | 10000 (все) | 196× меньше |
## Вывод
Индекс `idx_messages_chat_created` позволяет читать только нужную страницу истории, не сканируя всю таблицу. Это подтверждается планом: `Index Scan` вместо `Seq Scan`. Разница в 29 раз по времени и в 45 раз по прочитанным буферам.
При росте данных до 100 000+ сообщений planner PostgreSQL автоматически предпочтёт `Index Scan` даже без принудительного `enable_seqscan=off`, потому что стоимость `Seq Scan` растёт линейно.