141 lines
7.6 KiB
Plaintext
141 lines
7.6 KiB
Plaintext
# План запроса истории: класс big, чат 60k
|
||
|
||
Код: src_sha256 8a7e256c540af433, commit fee6f77c3bf1; вариант индекса: chat-only
|
||
Страница истории читаемого чата (limit 50); позиция — сколько
|
||
сообщений истории до страницы:
|
||
- first: после 0
|
||
- middle: после 30000
|
||
- last: после 59950
|
||
|
||
## Планы приложения
|
||
|
||
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
|
||
с которыми его выполнило приложение (prepared statement asyncpg).
|
||
|
||
### first
|
||
|
||
duration: 4.895 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 = '1e7be80c-6ea6-4d25-931b-218ba391b32d', $2 = '51'
|
||
Limit (cost=6238.72..6244.66 rows=51 width=204) (actual time=4.040..4.859 rows=51.00 loops=1)
|
||
Buffers: shared hit=3842
|
||
-> Gather Merge (cost=6238.72..13196.55 rows=59741 width=204) (actual time=4.039..4.856 rows=51.00 loops=1)
|
||
Workers Planned: 2
|
||
Workers Launched: 2
|
||
Buffers: shared hit=3842
|
||
-> Sort (cost=5238.70..5300.93 rows=24892 width=204) (actual time=3.196..3.198 rows=51.00 loops=3)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 44kB
|
||
Buffers: shared hit=3842
|
||
Worker 0: Sort Method: top-N heapsort Memory: 40kB
|
||
Worker 1: Sort Method: top-N heapsort Memory: 41kB
|
||
-> Parallel Seq Scan on messages (cost=0.00..4408.25 rows=24892 width=204) (actual time=0.004..2.333 rows=20000.00 loops=3)
|
||
Filter: (chat_id = '1e7be80c-6ea6-4d25-931b-218ba391b32d'::uuid)
|
||
Rows Removed by Filter: 22000
|
||
Buffers: shared hit=3752
|
||
|
||
### middle
|
||
|
||
duration: 6.572 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 = '1e7be80c-6ea6-4d25-931b-218ba391b32d', $2 = '2026-10-02 01:37:07.977303+00', $3 = 'cd02316c-24ba-4c9a-ba33-df19122e22ef', $4 = '51'
|
||
Limit (cost=603.27..603.29 rows=6 width=204) (actual time=6.537..6.541 rows=51.00 loops=1)
|
||
Buffers: shared hit=3836
|
||
-> Sort (cost=603.27..603.42 rows=60 width=204) (actual time=6.536..6.538 rows=51.00 loops=1)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 43kB
|
||
Buffers: shared hit=3836
|
||
-> Bitmap Heap Scan on messages (cost=5.67..601.50 rows=60 width=204) (actual time=1.874..5.320 rows=30000.00 loops=1)
|
||
Recheck Cond: (chat_id = $1)
|
||
Filter: (ROW(created_at, id) > ROW($2, $3))
|
||
Rows Removed by Filter: 30000
|
||
Heap Blocks: exact=3748
|
||
Buffers: shared hit=3836
|
||
-> Bitmap Index Scan on ix_messages_chat_id (cost=0.00..5.65 rows=181 width=0) (actual time=0.812..0.813 rows=60000.00 loops=1)
|
||
Index Cond: (chat_id = $1)
|
||
Index Searches: 1
|
||
Buffers: shared hit=88
|
||
|
||
### last
|
||
|
||
duration: 5.068 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 = '1e7be80c-6ea6-4d25-931b-218ba391b32d', $2 = '2026-10-02 01:38:04.171789+00', $3 = 'abf99c64-c63d-4c0f-b3f1-db071697e3ad', $4 = '51'
|
||
Limit (cost=603.27..603.29 rows=6 width=204) (actual time=5.035..5.039 rows=50.00 loops=1)
|
||
Buffers: shared hit=3836
|
||
-> Sort (cost=603.27..603.42 rows=60 width=204) (actual time=5.034..5.036 rows=50.00 loops=1)
|
||
Sort Key: created_at, id
|
||
Sort Method: quicksort Memory: 37kB
|
||
Buffers: shared hit=3836
|
||
-> Bitmap Heap Scan on messages (cost=5.67..601.50 rows=60 width=204) (actual time=5.013..5.025 rows=50.00 loops=1)
|
||
Recheck Cond: (chat_id = $1)
|
||
Filter: (ROW(created_at, id) > ROW($2, $3))
|
||
Rows Removed by Filter: 59950
|
||
Heap Blocks: exact=3748
|
||
Buffers: shared hit=3836
|
||
-> Bitmap Index Scan on ix_messages_chat_id (cost=0.00..5.65 rows=181 width=0) (actual time=0.809..0.809 rows=60000.00 loops=1)
|
||
Index Cond: (chat_id = $1)
|
||
Index Searches: 1
|
||
Buffers: shared hit=88
|
||
|
||
## Страница middle в psql: с индексом и без
|
||
|
||
Тот же текст запроса и те же параметры, что у плана middle.
|
||
psql планирует их заново (custom plan). DROP INDEX — внутри
|
||
транзакции, затем ROLLBACK: индекс остаётся в базе.
|
||
|
||
### С индексом ix_messages_chat_id
|
||
|
||
Limit (cost=6085.87..6091.81 rows=51 width=204) (actual time=4.570..5.531 rows=51.00 loops=1)
|
||
Buffers: shared hit=3782
|
||
-> Gather Merge (cost=6085.87..9563.67 rows=29861 width=204) (actual time=4.569..5.527 rows=51.00 loops=1)
|
||
Workers Planned: 2
|
||
Workers Launched: 2
|
||
Buffers: shared hit=3782
|
||
-> Sort (cost=5085.84..5116.95 rows=12442 width=204) (actual time=3.605..3.606 rows=41.67 loops=3)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 50kB
|
||
Buffers: shared hit=3782
|
||
Worker 0: Sort Method: top-N heapsort Memory: 47kB
|
||
Worker 1: Sort Method: top-N heapsort Memory: 47kB
|
||
-> Parallel Seq Scan on messages (cost=0.00..4670.75 rows=12442 width=204) (actual time=0.637..3.062 rows=10000.00 loops=3)
|
||
Filter: ((chat_id = '1e7be80c-6ea6-4d25-931b-218ba391b32d'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:37:07.977303+00'::timestamp with time zone, 'cd02316c-24ba-4c9a-ba33-df19122e22ef'::uuid)))
|
||
Rows Removed by Filter: 32000
|
||
Buffers: shared hit=3752
|
||
Planning:
|
||
Buffers: shared hit=130
|
||
Planning Time: 0.242 ms
|
||
Execution Time: 5.559 ms
|
||
|
||
### Без индекса ix_messages_chat_id
|
||
|
||
Limit (cost=6085.87..6091.81 rows=51 width=204) (actual time=4.089..4.924 rows=51.00 loops=1)
|
||
Buffers: shared hit=3782
|
||
-> Gather Merge (cost=6085.87..9563.67 rows=29861 width=204) (actual time=4.088..4.921 rows=51.00 loops=1)
|
||
Workers Planned: 2
|
||
Workers Launched: 2
|
||
Buffers: shared hit=3782
|
||
-> Sort (cost=5085.84..5116.95 rows=12442 width=204) (actual time=3.217..3.219 rows=40.33 loops=3)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 50kB
|
||
Buffers: shared hit=3782
|
||
Worker 0: Sort Method: top-N heapsort Memory: 47kB
|
||
Worker 1: Sort Method: top-N heapsort Memory: 50kB
|
||
-> Parallel Seq Scan on messages (cost=0.00..4670.75 rows=12442 width=204) (actual time=0.562..2.660 rows=10000.00 loops=3)
|
||
Filter: ((chat_id = '1e7be80c-6ea6-4d25-931b-218ba391b32d'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:37:07.977303+00'::timestamp with time zone, 'cd02316c-24ba-4c9a-ba33-df19122e22ef'::uuid)))
|
||
Rows Removed by Filter: 32000
|
||
Buffers: shared hit=3752
|
||
Planning:
|
||
Buffers: shared hit=17
|
||
Planning Time: 0.088 ms
|
||
Execution Time: 4.934 ms
|