research: record keyset track2 baseline and reproducible analysis

This commit is contained in:
2026-10-02 04:26:21 +03:00
parent 6d2240ce9f
commit 8e7d4183e3
13 changed files with 1127 additions and 0 deletions
@@ -0,0 +1,100 @@
# План запроса истории: класс medium, чат 6k
Код: src_sha256 8a7e256c540af433, commit 6d2240ce9fec; вариант индекса: keyset
Страница истории читаемого чата (limit 50); позиция — сколько
сообщений истории до страницы:
- first: после 0
- middle: после 3000
- last: после 5950
## Планы приложения
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
с которыми его выполнило приложение (prepared statement asyncpg).
### first
duration: 0.057 ms plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
FROM messages
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id
LIMIT $2::INTEGER
Query Parameters: $1 = 'a77e2995-196e-4901-9238-c7a948bfcb16', $2 = '51'
Limit (cost=0.42..114.76 rows=51 width=204) (actual time=0.012..0.034 rows=51.00 loops=1)
Buffers: shared hit=47
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..13230.99 rows=5901 width=204) (actual time=0.012..0.031 rows=51.00 loops=1)
Index Cond: (chat_id = 'a77e2995-196e-4901-9238-c7a948bfcb16'::uuid)
Index Searches: 1
Buffers: shared hit=47
### middle
duration: 0.052 ms plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
FROM messages
WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id
LIMIT $4::INTEGER
Query Parameters: $1 = 'a77e2995-196e-4901-9238-c7a948bfcb16', $2 = '2026-10-02 01:18:36.967812+00', $3 = '3f451920-4028-4279-afbf-1ceed233019e', $4 = '51'
Limit (cost=0.42..24.20 rows=6 width=204) (actual time=0.009..0.031 rows=51.00 loops=1)
Buffers: shared hit=50
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..246.13 rows=62 width=204) (actual time=0.008..0.028 rows=51.00 loops=1)
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Index Searches: 1
Buffers: shared hit=50
### last
duration: 0.050 ms plan:
Query Text: SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
FROM messages
WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3::UUID) ORDER BY messages.created_at, messages.id
LIMIT $4::INTEGER
Query Parameters: $1 = 'a77e2995-196e-4901-9238-c7a948bfcb16', $2 = '2026-10-02 01:19:33.259754+00', $3 = 'b50d595c-c526-46e5-b538-ad0101f42ca0', $4 = '51'
Limit (cost=0.42..24.20 rows=6 width=204) (actual time=0.008..0.029 rows=50.00 loops=1)
Buffers: shared hit=52
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..246.13 rows=62 width=204) (actual time=0.007..0.026 rows=50.00 loops=1)
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Index Searches: 1
Buffers: shared hit=52
## Страница middle в psql: с индексом и без
Тот же текст запроса и те же параметры, что у плана middle.
psql планирует их заново (custom plan). DROP INDEX — внутри
транзакции, затем ROLLBACK: индекс остаётся в базе.
### С индексом ix_messages_chat_id_created_at_id
Limit (cost=0.42..145.28 rows=51 width=204) (actual time=0.040..0.112 rows=51.00 loops=1)
Buffers: shared hit=50
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..8552.81 rows=3011 width=204) (actual time=0.038..0.105 rows=51.00 loops=1)
Index Cond: ((chat_id = 'a77e2995-196e-4901-9238-c7a948bfcb16'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:18:36.967812+00'::timestamp with time zone, '3f451920-4028-4279-afbf-1ceed233019e'::uuid)))
Index Searches: 1
Buffers: shared hit=50
Planning:
Buffers: shared hit=141
Planning Time: 0.387 ms
Execution Time: 0.145 ms
### Без индекса ix_messages_chat_id_created_at_id
Limit (cost=5750.37..5756.31 rows=51 width=204) (actual time=4.821..6.011 rows=51.00 loops=1)
Buffers: shared hit=3797
-> Gather Merge (cost=5750.37..6101.05 rows=3011 width=204) (actual time=4.820..6.004 rows=51.00 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=3797
-> Sort (cost=4750.34..4753.48 rows=1255 width=204) (actual time=3.477..3.479 rows=40.33 loops=3)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 50kB
Buffers: shared hit=3797
Worker 0: Sort Method: top-N heapsort Memory: 48kB
Worker 1: Sort Method: top-N heapsort Memory: 47kB
-> Parallel Seq Scan on messages (cost=0.00..4685.75 rows=1255 width=204) (actual time=0.977..3.386 rows=1000.00 loops=3)
Filter: ((chat_id = 'a77e2995-196e-4901-9238-c7a948bfcb16'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:18:36.967812+00'::timestamp with time zone, '3f451920-4028-4279-afbf-1ceed233019e'::uuid)))
Rows Removed by Filter: 41000
Buffers: shared hit=3767
Planning:
Buffers: shared hit=17
Planning Time: 0.083 ms
Execution Time: 6.032 ms