101 lines
5.4 KiB
Plaintext
101 lines
5.4 KiB
Plaintext
# План запроса истории: класс big, чат 60k
|
||
|
||
Код: src_sha256 8a7e256c540af433, commit 8e7d4183e32c; вариант индекса: keyset
|
||
Страница истории читаемого чата (limit 50); позиция — сколько
|
||
сообщений истории до страницы:
|
||
- first: после 0
|
||
- middle: после 30000
|
||
- last: после 59950
|
||
|
||
## Планы приложения
|
||
|
||
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
|
||
с которыми его выполнило приложение (prepared statement asyncpg).
|
||
|
||
### first
|
||
|
||
duration: 0.053 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 = '739e4777-e10b-4ffd-9b51-8e0259933691', $2 = '51'
|
||
Limit (cost=0.42..16.18 rows=51 width=204) (actual time=0.013..0.029 rows=51.00 loops=1)
|
||
Buffers: shared hit=31
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..18607.33 rows=60190 width=204) (actual time=0.013..0.025 rows=51.00 loops=1)
|
||
Index Cond: (chat_id = '739e4777-e10b-4ffd-9b51-8e0259933691'::uuid)
|
||
Index Searches: 1
|
||
Buffers: shared hit=31
|
||
|
||
### 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 = '739e4777-e10b-4ffd-9b51-8e0259933691', $2 = '2026-10-02 01:27:27.383878+00', $3 = '5beb2261-fb86-4c93-88f8-ff9bda33c36a', $4 = '51'
|
||
Limit (cost=0.42..24.15 rows=6 width=204) (actual time=0.009..0.027 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..241.72 rows=61 width=204) (actual time=0.009..0.024 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.045 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 = '739e4777-e10b-4ffd-9b51-8e0259933691', $2 = '2026-10-02 01:28:24.647927+00', $3 = '85336a9f-a392-418a-b5c4-5007b05290c7', $4 = '51'
|
||
Limit (cost=0.42..24.15 rows=6 width=204) (actual time=0.009..0.025 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.009..0.023 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..28.72 rows=51 width=204) (actual time=0.023..0.050 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..16643.92 rows=29988 width=204) (actual time=0.022..0.046 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '739e4777-e10b-4ffd-9b51-8e0259933691'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:27.383878+00'::timestamp with time zone, '5beb2261-fb86-4c93-88f8-ff9bda33c36a'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=54
|
||
Planning:
|
||
Buffers: shared hit=141
|
||
Planning Time: 0.261 ms
|
||
Execution Time: 0.072 ms
|
||
|
||
### Без индекса ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=6094.63..6100.57 rows=51 width=204) (actual time=5.007..6.068 rows=51.00 loops=1)
|
||
Buffers: shared hit=3789
|
||
-> Gather Merge (cost=6094.63..9587.23 rows=29988 width=204) (actual time=5.006..6.062 rows=51.00 loops=1)
|
||
Workers Planned: 2
|
||
Workers Launched: 2
|
||
Buffers: shared hit=3789
|
||
-> Sort (cost=5094.61..5125.85 rows=12495 width=204) (actual time=3.972..3.974 rows=39.67 loops=3)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 50kB
|
||
Buffers: shared hit=3789
|
||
Worker 0: Sort Method: top-N heapsort Memory: 49kB
|
||
Worker 1: Sort Method: top-N heapsort Memory: 45kB
|
||
-> Parallel Seq Scan on messages (cost=0.00..4677.75 rows=12495 width=204) (actual time=0.737..3.364 rows=10000.00 loops=3)
|
||
Filter: ((chat_id = '739e4777-e10b-4ffd-9b51-8e0259933691'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:27:27.383878+00'::timestamp with time zone, '5beb2261-fb86-4c93-88f8-ff9bda33c36a'::uuid)))
|
||
Rows Removed by Filter: 32000
|
||
Buffers: shared hit=3759
|
||
Planning:
|
||
Buffers: shared hit=17
|
||
Planning Time: 0.070 ms
|
||
Execution Time: 6.094 ms
|