101 lines
5.6 KiB
Plaintext
101 lines
5.6 KiB
Plaintext
# План запроса истории: размер 500k
|
||
|
||
Commit e61a0e5
|
||
Страница истории читаемого чата; позиция — сколько сообщений
|
||
истории до страницы:
|
||
- first: после 0
|
||
- middle: после 250000
|
||
- last: после 499500
|
||
|
||
## Планы приложения
|
||
|
||
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
|
||
с которыми его выполнило приложение (prepared statement asyncpg).
|
||
|
||
### first
|
||
|
||
duration: 0.038 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 = '109824b5-66a4-4253-80e5-49bb45401d43', $2 = '51'
|
||
Limit (cost=0.42..5.06 rows=51 width=121) (actual time=0.017..0.025 rows=51.00 loops=1)
|
||
Buffers: shared hit=4
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..45451.11 rows=500000 width=121) (actual time=0.016..0.021 rows=51.00 loops=1)
|
||
Index Cond: (chat_id = '109824b5-66a4-4253-80e5-49bb45401d43'::uuid)
|
||
Index Searches: 1
|
||
Buffers: shared hit=4
|
||
|
||
### middle
|
||
|
||
duration: 0.048 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 = '109824b5-66a4-4253-80e5-49bb45401d43', $2 = '2026-10-02 00:37:56.094512+00', $3 = '6cc91dc6-e968-416e-b22f-7c0cfd478b00', $4 = '51'
|
||
Limit (cost=0.42..6.92 rows=51 width=121) (actual time=0.012..0.036 rows=51.00 loops=1)
|
||
Buffers: shared hit=47
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..31731.26 rows=248945 width=121) (actual time=0.012..0.033 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '109824b5-66a4-4253-80e5-49bb45401d43'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:37:56.094512+00'::timestamp with time zone, '6cc91dc6-e968-416e-b22f-7c0cfd478b00'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=47
|
||
|
||
### last
|
||
|
||
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 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 = '109824b5-66a4-4253-80e5-49bb45401d43', $2 = '2026-10-02 00:42:27.49097+00', $3 = 'e088f724-9b35-4c97-9f18-9c7895e0a326', $4 = '51'
|
||
Limit (cost=0.42..91.14 rows=51 width=121) (actual time=0.010..0.038 rows=51.00 loops=1)
|
||
Buffers: shared hit=55
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..832.84 rows=468 width=121) (actual time=0.009..0.034 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '109824b5-66a4-4253-80e5-49bb45401d43'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:42:27.49097+00'::timestamp with time zone, 'e088f724-9b35-4c97-9f18-9c7895e0a326'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=55
|
||
|
||
## Средняя страница в psql: с индексом и без
|
||
|
||
Тот же текст запроса и те же параметры, что у плана middle.
|
||
psql планирует их заново (custom plan). DROP INDEX — внутри
|
||
транзакции, затем ROLLBACK: индекс остаётся в базе.
|
||
|
||
### С индексом ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=0.42..6.92 rows=51 width=121) (actual time=0.027..0.059 rows=51.00 loops=1)
|
||
Buffers: shared hit=47
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..31731.26 rows=248945 width=121) (actual time=0.026..0.054 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '109824b5-66a4-4253-80e5-49bb45401d43'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:37:56.094512+00'::timestamp with time zone, '6cc91dc6-e968-416e-b22f-7c0cfd478b00'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=47
|
||
Planning:
|
||
Buffers: shared hit=111
|
||
Planning Time: 0.287 ms
|
||
Execution Time: 0.090 ms
|
||
|
||
### Без индекса ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=17754.41..17760.35 rows=51 width=121) (actual time=18.156..20.096 rows=51.00 loops=1)
|
||
Buffers: shared hit=9678
|
||
-> Gather Merge (cost=17754.41..46748.16 rows=248945 width=121) (actual time=18.156..20.093 rows=51.00 loops=1)
|
||
Workers Planned: 2
|
||
Workers Launched: 2
|
||
Buffers: shared hit=9678
|
||
-> Sort (cost=16754.39..17013.70 rows=103727 width=121) (actual time=16.709..16.711 rows=42.33 loops=3)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 43kB
|
||
Buffers: shared hit=9678
|
||
Worker 0: Sort Method: top-N heapsort Memory: 42kB
|
||
Worker 1: Sort Method: top-N heapsort Memory: 44kB
|
||
-> Parallel Seq Scan on messages (cost=0.00..13293.83 rows=103727 width=121) (actual time=5.006..11.514 rows=83333.33 loops=3)
|
||
Filter: ((chat_id = '109824b5-66a4-4253-80e5-49bb45401d43'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:37:56.094512+00'::timestamp with time zone, '6cc91dc6-e968-416e-b22f-7c0cfd478b00'::uuid)))
|
||
Rows Removed by Filter: 83333
|
||
Buffers: shared hit=9648
|
||
Planning:
|
||
Buffers: shared hit=15
|
||
Planning Time: 0.094 ms
|
||
Execution Time: 20.124 ms
|