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 @@
# План запроса истории: класс small, чат 200
Код: src_sha256 8a7e256c540af433, commit 6d2240ce9fec; вариант индекса: keyset
Страница истории читаемого чата (limit 50); позиция — сколько
сообщений истории до страницы:
- first: после 0
- middle: после 100
- last: после 150
## Планы приложения
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
с которыми его выполнило приложение (prepared statement asyncpg).
### first
duration: 0.096 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 = '31d92451-4a01-41f6-89ce-cdeedc71f887', $2 = '51'
Limit (cost=0.42..197.30 rows=51 width=204) (actual time=0.014..0.070 rows=51.00 loops=1)
Buffers: shared hit=54
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..826.57 rows=214 width=204) (actual time=0.013..0.066 rows=51.00 loops=1)
Index Cond: (chat_id = '31d92451-4a01-41f6-89ce-cdeedc71f887'::uuid)
Index Searches: 1
Buffers: shared hit=54
### middle
duration: 0.079 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 = '31d92451-4a01-41f6-89ce-cdeedc71f887', $2 = '2026-10-02 01:18:47.043523+00', $3 = '83f8e35a-40fb-4624-9f15-e60f4b907f7b', $4 = '51'
Limit (cost=0.42..24.20 rows=6 width=204) (actual time=0.010..0.057 rows=51.00 loops=1)
Buffers: shared hit=54
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..246.13 rows=62 width=204) (actual time=0.009..0.054 rows=51.00 loops=1)
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
Index Searches: 1
Buffers: shared hit=54
### last
duration: 0.062 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 = '31d92451-4a01-41f6-89ce-cdeedc71f887', $2 = '2026-10-02 01:19:14.56679+00', $3 = '0d4d0ef8-1c06-4f4a-9543-2c299986bee7', $4 = '51'
Limit (cost=0.42..24.20 rows=6 width=204) (actual time=0.008..0.043 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.040 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..201.59 rows=51 width=204) (actual time=0.035..0.274 rows=51.00 loops=1)
Buffers: shared hit=54
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..351.48 rows=89 width=204) (actual time=0.034..0.269 rows=51.00 loops=1)
Index Cond: ((chat_id = '31d92451-4a01-41f6-89ce-cdeedc71f887'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:18:47.043523+00'::timestamp with time zone, '83f8e35a-40fb-4624-9f15-e60f4b907f7b'::uuid)))
Index Searches: 1
Buffers: shared hit=54
Planning:
Buffers: shared hit=141
Planning Time: 0.375 ms
Execution Time: 0.307 ms
### Без индекса ix_messages_chat_id_created_at_id
Limit (cost=5686.74..5692.68 rows=51 width=204) (actual time=5.001..6.120 rows=51.00 loops=1)
Buffers: shared hit=3797
-> Gather Merge (cost=5686.74..5697.10 rows=89 width=204) (actual time=5.000..6.114 rows=51.00 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=3797
-> Sort (cost=4686.71..4686.81 rows=37 width=204) (actual time=3.568..3.570 rows=28.33 loops=3)
Sort Key: created_at, id
Sort Method: quicksort Memory: 34kB
Buffers: shared hit=3797
Worker 0: Sort Method: quicksort Memory: 32kB
Worker 1: Sort Method: quicksort Memory: 33kB
-> Parallel Seq Scan on messages (cost=0.00..4685.75 rows=37 width=204) (actual time=1.232..3.547 rows=33.33 loops=3)
Filter: ((chat_id = '31d92451-4a01-41f6-89ce-cdeedc71f887'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:18:47.043523+00'::timestamp with time zone, '83f8e35a-40fb-4624-9f15-e60f4b907f7b'::uuid)))
Rows Removed by Filter: 41967
Buffers: shared hit=3767
Planning:
Buffers: shared hit=17
Planning Time: 0.104 ms
Execution Time: 6.147 ms