101 lines
5.4 KiB
Plaintext
101 lines
5.4 KiB
Plaintext
# План запроса истории: класс big, чат 60k
|
||
|
||
Код: src_sha256 8a7e256c540af433, commit 6d2240ce9fec; вариант индекса: keyset
|
||
Страница истории читаемого чата (limit 50); позиция — сколько
|
||
сообщений истории до страницы:
|
||
- first: после 0
|
||
- middle: после 30000
|
||
- last: после 59950
|
||
|
||
## Планы приложения
|
||
|
||
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
|
||
с которыми его выполнило приложение (prepared statement asyncpg).
|
||
|
||
### first
|
||
|
||
duration: 0.049 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 = '713e41cf-931c-490c-930e-81da041d316a', $2 = '51'
|
||
Limit (cost=0.42..16.33 rows=51 width=204) (actual time=0.012..0.026 rows=51.00 loops=1)
|
||
Buffers: shared hit=31
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..18642.57 rows=59749 width=204) (actual time=0.012..0.024 rows=51.00 loops=1)
|
||
Index Cond: (chat_id = '713e41cf-931c-490c-930e-81da041d316a'::uuid)
|
||
Index Searches: 1
|
||
Buffers: shared hit=31
|
||
|
||
### middle
|
||
|
||
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 = '713e41cf-931c-490c-930e-81da041d316a', $2 = '2026-10-02 01:18:37.730273+00', $3 = 'e9d09e50-c4db-4bd9-8578-ace1129350a4', $4 = '51'
|
||
Limit (cost=0.42..24.20 rows=6 width=204) (actual time=0.011..0.029 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.010..0.026 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.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 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 = '713e41cf-931c-490c-930e-81da041d316a', $2 = '2026-10-02 01:19:34.10731+00', $3 = '89f52dfd-45fe-4dc1-852e-20e885ca3e40', $4 = '51'
|
||
Limit (cost=0.42..24.20 rows=6 width=204) (actual time=0.008..0.036 rows=50.00 loops=1)
|
||
Buffers: shared hit=53
|
||
-> 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.033 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.78 rows=51 width=204) (actual time=0.037..0.067 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..16710.37 rows=30046 width=204) (actual time=0.036..0.063 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '713e41cf-931c-490c-930e-81da041d316a'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:18:37.730273+00'::timestamp with time zone, 'e9d09e50-c4db-4bd9-8578-ace1129350a4'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=54
|
||
Planning:
|
||
Buffers: shared hit=141
|
||
Planning Time: 0.274 ms
|
||
Execution Time: 0.092 ms
|
||
|
||
### Без индекса ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=6103.43..6109.37 rows=51 width=204) (actual time=5.497..6.604 rows=51.00 loops=1)
|
||
Buffers: shared hit=3797
|
||
-> Gather Merge (cost=6103.43..9602.79 rows=30046 width=204) (actual time=5.496..6.601 rows=51.00 loops=1)
|
||
Workers Planned: 2
|
||
Workers Launched: 2
|
||
Buffers: shared hit=3797
|
||
-> Sort (cost=5103.41..5134.71 rows=12519 width=204) (actual time=4.204..4.206 rows=42.00 loops=3)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 46kB
|
||
Buffers: shared hit=3797
|
||
Worker 0: Sort Method: top-N heapsort Memory: 44kB
|
||
Worker 1: Sort Method: top-N heapsort Memory: 50kB
|
||
-> Parallel Seq Scan on messages (cost=0.00..4685.75 rows=12519 width=204) (actual time=0.885..3.596 rows=10000.00 loops=3)
|
||
Filter: ((chat_id = '713e41cf-931c-490c-930e-81da041d316a'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:18:37.730273+00'::timestamp with time zone, 'e9d09e50-c4db-4bd9-8578-ace1129350a4'::uuid)))
|
||
Rows Removed by Filter: 32000
|
||
Buffers: shared hit=3767
|
||
Planning:
|
||
Buffers: shared hit=17
|
||
Planning Time: 0.092 ms
|
||
Execution Time: 6.628 ms
|