Files
mtusi2026lab3/evidence/explain-history.txt
T
2026-10-15 00:24:46 +03:00

77 lines
4.6 KiB
Plaintext

docker compose exec -T postgres psql -X -q -U messenger -d messenger -v ON_ERROR_STOP=1 < infra/sql/explain_history.sql
=== Объём данных
messages_total | messages_in_big_chat
----------------+----------------------
21635 | 12000
=== Индекс истории (migrations/001_core.sql)
indexdef
-------------------------------------------------------------------------------------------------
CREATE INDEX messages_chat_history_idx ON public.messages USING btree (chat_id, created_at, id)
=== 1. Первая страница (limit=50, приложение просит 51 строку, чтобы понять, есть ли следующая)
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Limit (actual time=0.016..0.030 rows=51 loops=1)
Buffers: shared hit=4
-> Index Scan using messages_chat_history_idx on messages (actual time=0.015..0.026 rows=51 loops=1)
Index Cond: (chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid)
Buffers: shared hit=4
Planning:
Buffers: shared hit=7
Planning Time: 0.139 ms
Execution Time: 0.050 ms
=== 2. Страница из середины истории по keyset-курсору (после 6000-го сообщения)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.009..0.029 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (actual time=0.009..0.025 rows=51 loops=1)
Index Cond: ((chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '509a5d7d-2d2f-4322-8b64-938949155ce0'::uuid)))
Buffers: shared hit=5
Planning Time: 0.087 ms
Execution Time: 0.059 ms
=== 3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает)
QUERY PLAN
-----------------------------------------------------------------------------------------------------------
Limit (actual time=1.583..1.600 rows=51 loops=1)
Buffers: shared hit=127
-> Index Scan using messages_chat_history_idx on messages (actual time=0.021..1.393 rows=6051 loops=1)
Index Cond: (chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid)
Buffers: shared hit=127
Planning Time: 0.089 ms
Execution Time: 1.621 ms
=== 4. Для сравнения: запрос 2 без индекса (индекс удалён только внутри этой транзакции)
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=2.654..2.660 rows=51 loops=1)
Buffers: shared hit=172
-> Sort (actual time=2.653..2.655 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=172
-> Bitmap Heap Scan on messages (actual time=0.782..1.755 rows=6000 loops=1)
Recheck Cond: (chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid)
Filter: (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '509a5d7d-2d2f-4322-8b64-938949155ce0'::uuid))
Rows Removed by Filter: 6000
Heap Blocks: exact=161
Buffers: shared hit=172
-> Bitmap Index Scan on messages_idempotency_unique (actual time=0.201..0.202 rows=12000 loops=1)
Index Cond: (chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid)
Buffers: shared hit=11
Planning:
Buffers: shared hit=5
Planning Time: 0.097 ms
Execution Time: 2.681 ms
=== ROLLBACK: синтетические данные удалены, индекс на месте