101 lines
5.4 KiB
Plaintext
101 lines
5.4 KiB
Plaintext
# План запроса истории: класс medium, чат 6k
|
||
|
||
Код: src_sha256 8a7e256c540af433, commit 8e7d4183e32c; вариант индекса: 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 = 'e2fe65c1-a220-4f07-9043-7c9e30a2b5f1', $2 = '51'
|
||
Limit (cost=0.42..113.73 rows=51 width=204) (actual time=0.013..0.034 rows=51.00 loops=1)
|
||
Buffers: shared hit=51
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..13297.62 rows=5985 width=204) (actual time=0.012..0.031 rows=51.00 loops=1)
|
||
Index Cond: (chat_id = 'e2fe65c1-a220-4f07-9043-7c9e30a2b5f1'::uuid)
|
||
Index Searches: 1
|
||
Buffers: shared hit=51
|
||
|
||
### middle
|
||
|
||
duration: 0.055 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 = 'e2fe65c1-a220-4f07-9043-7c9e30a2b5f1', $2 = '2026-10-02 01:27:26.631796+00', $3 = '3a11cc86-3863-4ab8-9a96-cb1bc7c75b98', $4 = '51'
|
||
Limit (cost=0.42..24.15 rows=6 width=204) (actual time=0.008..0.033 rows=51.00 loops=1)
|
||
Buffers: shared hit=51
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..241.72 rows=61 width=204) (actual time=0.008..0.031 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=51
|
||
|
||
### last
|
||
|
||
duration: 0.054 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 = 'e2fe65c1-a220-4f07-9043-7c9e30a2b5f1', $2 = '2026-10-02 01:28:23.799856+00', $3 = 'e33a3148-3666-492b-a5d5-84828089eae7', $4 = '51'
|
||
Limit (cost=0.42..24.15 rows=6 width=204) (actual time=0.009..0.034 rows=50.00 loops=1)
|
||
Buffers: shared hit=53
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..241.72 rows=61 width=204) (actual time=0.008..0.031 rows=50.00 loops=1)
|
||
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=53
|
||
|
||
## Страница middle в psql: с индексом и без
|
||
|
||
Тот же текст запроса и те же параметры, что у плана middle.
|
||
psql планирует их заново (custom plan). DROP INDEX — внутри
|
||
транзакции, затем ROLLBACK: индекс остаётся в базе.
|
||
|
||
### С индексом ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=0.42..144.79 rows=51 width=204) (actual time=0.039..0.111 rows=51.00 loops=1)
|
||
Buffers: shared hit=51
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..8558.23 rows=3023 width=204) (actual time=0.038..0.104 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = 'e2fe65c1-a220-4f07-9043-7c9e30a2b5f1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:26.631796+00'::timestamp with time zone, '3a11cc86-3863-4ab8-9a96-cb1bc7c75b98'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=51
|
||
Planning:
|
||
Buffers: shared hit=141
|
||
Planning Time: 0.436 ms
|
||
Execution Time: 0.240 ms
|
||
|
||
### Без индекса ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=5742.66..5748.60 rows=51 width=204) (actual time=5.437..6.485 rows=51.00 loops=1)
|
||
Buffers: shared hit=3789
|
||
-> Gather Merge (cost=5742.66..6094.74 rows=3023 width=204) (actual time=5.437..6.481 rows=51.00 loops=1)
|
||
Workers Planned: 2
|
||
Workers Launched: 2
|
||
Buffers: shared hit=3789
|
||
-> Sort (cost=4742.64..4745.79 rows=1260 width=204) (actual time=4.114..4.115 rows=38.67 loops=3)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 49kB
|
||
Buffers: shared hit=3789
|
||
Worker 0: Sort Method: top-N heapsort Memory: 50kB
|
||
Worker 1: Sort Method: top-N heapsort Memory: 51kB
|
||
-> Parallel Seq Scan on messages (cost=0.00..4677.75 rows=1260 width=204) (actual time=1.293..4.021 rows=1000.00 loops=3)
|
||
Filter: ((chat_id = 'e2fe65c1-a220-4f07-9043-7c9e30a2b5f1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:26.631796+00'::timestamp with time zone, '3a11cc86-3863-4ab8-9a96-cb1bc7c75b98'::uuid)))
|
||
Rows Removed by Filter: 41000
|
||
Buffers: shared hit=3759
|
||
Planning:
|
||
Buffers: shared hit=17
|
||
Planning Time: 0.140 ms
|
||
Execution Time: 6.510 ms
|