95 lines
5.2 KiB
Plaintext
95 lines
5.2 KiB
Plaintext
# План запроса истории: размер 10k
|
||
|
||
Код: src_sha256 8a7e256c540af433, commit c7d36bcaa592; вариант индекса: keyset
|
||
Страница истории читаемого чата (limit 50); позиция — сколько
|
||
сообщений истории до страницы:
|
||
- first: после 0
|
||
- middle: после 5000
|
||
- last: после 9950
|
||
|
||
## Планы приложения
|
||
|
||
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
|
||
с которыми его выполнило приложение (prepared statement asyncpg).
|
||
|
||
### first
|
||
|
||
duration: 0.040 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 = '4dea5917-2a87-4eed-bad1-cd3f129dd4f4', $2 = '51'
|
||
Limit (cost=0.29..6.60 rows=51 width=204) (actual time=0.012..0.020 rows=51.00 loops=1)
|
||
Buffers: shared hit=13
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..1238.41 rows=10000 width=204) (actual time=0.011..0.018 rows=51.00 loops=1)
|
||
Index Cond: (chat_id = '4dea5917-2a87-4eed-bad1-cd3f129dd4f4'::uuid)
|
||
Index Searches: 1
|
||
Buffers: shared hit=13
|
||
|
||
### middle
|
||
|
||
duration: 0.046 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 = '4dea5917-2a87-4eed-bad1-cd3f129dd4f4', $2 = '2026-10-02 00:47:43.137147+00', $3 = '052a5d48-4cd9-4aa5-91d7-4ceed4002bcf', $4 = '51'
|
||
Limit (cost=0.29..9.69 rows=51 width=204) (actual time=0.006..0.025 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..922.60 rows=5000 width=204) (actual time=0.006..0.023 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '4dea5917-2a87-4eed-bad1-cd3f129dd4f4'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:47:43.137147+00'::timestamp with time zone, '052a5d48-4cd9-4aa5-91d7-4ceed4002bcf'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=54
|
||
|
||
### last
|
||
|
||
duration: 0.041 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 = '4dea5917-2a87-4eed-bad1-cd3f129dd4f4', $2 = '2026-10-02 00:47:50.05267+00', $3 = '3ae3ceb9-d3cb-4373-bbda-5cbb4b805f4b', $4 = '51'
|
||
Limit (cost=0.29..92.09 rows=51 width=204) (actual time=0.007..0.022 rows=50.00 loops=1)
|
||
Buffers: shared hit=52
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..93.89 rows=52 width=204) (actual time=0.006..0.020 rows=50.00 loops=1)
|
||
Index Cond: ((chat_id = '4dea5917-2a87-4eed-bad1-cd3f129dd4f4'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:47:50.05267+00'::timestamp with time zone, '3ae3ceb9-d3cb-4373-bbda-5cbb4b805f4b'::uuid)))
|
||
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.29..9.69 rows=51 width=204) (actual time=0.019..0.055 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..922.60 rows=5000 width=204) (actual time=0.018..0.050 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '4dea5917-2a87-4eed-bad1-cd3f129dd4f4'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:47:43.137147+00'::timestamp with time zone, '052a5d48-4cd9-4aa5-91d7-4ceed4002bcf'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=54
|
||
Planning:
|
||
Buffers: shared hit=111
|
||
Planning Time: 0.255 ms
|
||
Execution Time: 0.083 ms
|
||
|
||
### Без индекса ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=672.81..672.94 rows=51 width=204) (actual time=1.256..1.261 rows=51.00 loops=1)
|
||
Buffers: shared hit=337
|
||
-> Sort (cost=672.81..685.31 rows=5000 width=204) (actual time=1.255..1.258 rows=51.00 loops=1)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 46kB
|
||
Buffers: shared hit=337
|
||
-> Seq Scan on messages (cost=0.00..506.00 rows=5000 width=204) (actual time=0.389..0.892 rows=5000.00 loops=1)
|
||
Filter: ((chat_id = '4dea5917-2a87-4eed-bad1-cd3f129dd4f4'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:47:43.137147+00'::timestamp with time zone, '052a5d48-4cd9-4aa5-91d7-4ceed4002bcf'::uuid)))
|
||
Rows Removed by Filter: 5000
|
||
Buffers: shared hit=331
|
||
Planning:
|
||
Buffers: shared hit=17
|
||
Planning Time: 0.090 ms
|
||
Execution Time: 1.274 ms
|