Files
mtusi2026lab3/evidence/explain-history.txt
T
2026-10-15 19:44: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
----------------+----------------------
39193 | 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.031 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (actual time=0.015..0.026 rows=51 loops=1)
Index Cond: (chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid)
Buffers: shared hit=5
Planning:
Buffers: shared hit=6
Planning Time: 0.186 ms
Execution Time: 0.054 ms
=== 2. Страница из середины истории по keyset-курсору (после 6000-го сообщения)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.012..0.031 rows=51 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_history_idx on messages (actual time=0.011..0.026 rows=51 loops=1)
Index Cond: ((chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '4cf0e1fe-ddbf-4da7-b5d3-e610ab7325b9'::uuid)))
Buffers: shared hit=6
Planning Time: 0.105 ms
Execution Time: 0.115 ms
=== 3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает)
QUERY PLAN
-----------------------------------------------------------------------------------------------------------
Limit (actual time=1.711..1.729 rows=51 loops=1)
Buffers: shared hit=169
-> Index Scan using messages_chat_history_idx on messages (actual time=0.025..1.504 rows=6051 loops=1)
Index Cond: (chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid)
Buffers: shared hit=169
Planning Time: 0.112 ms
Execution Time: 1.751 ms
=== 4. Для сравнения: запрос 2 без индекса (индекс удалён только внутри этой транзакции)
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=4.045..4.053 rows=51 loops=1)
Buffers: shared hit=180
-> Sort (actual time=4.044..4.047 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=180
-> Bitmap Heap Scan on messages (actual time=0.996..2.398 rows=6000 loops=1)
Recheck Cond: (chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid)
Filter: (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '4cf0e1fe-ddbf-4da7-b5d3-e610ab7325b9'::uuid))
Rows Removed by Filter: 6000
Heap Blocks: exact=168
Buffers: shared hit=180
-> Bitmap Index Scan on messages_idempotency_unique (actual time=0.253..0.253 rows=12000 loops=1)
Index Cond: (chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid)
Buffers: shared hit=12
Planning:
Buffers: shared hit=5
Planning Time: 0.126 ms
Execution Time: 4.078 ms
=== ROLLBACK: синтетические данные удалены, индекс на месте