95 lines
5.1 KiB
Plaintext
95 lines
5.1 KiB
Plaintext
# План запроса истории: размер 10k
|
||
|
||
Commit e61a0e5
|
||
Страница истории читаемого чата; позиция — сколько сообщений
|
||
истории до страницы:
|
||
- first: после 0
|
||
- middle: после 5000
|
||
- last: после 9900
|
||
|
||
## Планы приложения
|
||
|
||
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
|
||
с которыми его выполнило приложение (prepared statement asyncpg).
|
||
|
||
### first
|
||
|
||
duration: 0.034 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 = '43653413-a4e5-4fce-8197-5e94ebffbd59', $2 = '51'
|
||
Limit (cost=0.29..5.09 rows=51 width=121) (actual time=0.016..0.022 rows=51.00 loops=1)
|
||
Buffers: shared hit=3
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..942.41 rows=10000 width=121) (actual time=0.015..0.018 rows=51.00 loops=1)
|
||
Index Cond: (chat_id = '43653413-a4e5-4fce-8197-5e94ebffbd59'::uuid)
|
||
Index Searches: 1
|
||
Buffers: shared hit=3
|
||
|
||
### middle
|
||
|
||
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 = '43653413-a4e5-4fce-8197-5e94ebffbd59', $2 = '2026-10-02 00:21:19.575202+00', $3 = '912186b6-d6ec-4eb9-85d7-c1cff28b4a98', $4 = '51'
|
||
Limit (cost=0.29..7.04 rows=51 width=121) (actual time=0.011..0.037 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..662.60 rows=5000 width=121) (actual time=0.011..0.034 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '43653413-a4e5-4fce-8197-5e94ebffbd59'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:21:19.575202+00'::timestamp with time zone, '912186b6-d6ec-4eb9-85d7-c1cff28b4a98'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=54
|
||
|
||
### last
|
||
|
||
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 = '43653413-a4e5-4fce-8197-5e94ebffbd59', $2 = '2026-10-02 00:21:24.712133+00', $3 = '77b54258-b0ae-4886-abfb-18c074ed7baf', $4 = '51'
|
||
Limit (cost=0.29..77.36 rows=51 width=121) (actual time=0.009..0.034 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..151.41 rows=100 width=121) (actual time=0.009..0.031 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '43653413-a4e5-4fce-8197-5e94ebffbd59'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:21:24.712133+00'::timestamp with time zone, '77b54258-b0ae-4886-abfb-18c074ed7baf'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=54
|
||
|
||
## Средняя страница в psql: с индексом и без
|
||
|
||
Тот же текст запроса и те же параметры, что у плана middle.
|
||
psql планирует их заново (custom plan). DROP INDEX — внутри
|
||
транзакции, затем ROLLBACK: индекс остаётся в базе.
|
||
|
||
### С индексом ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=0.29..7.04 rows=51 width=121) (actual time=0.017..0.041 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.29..662.60 rows=5000 width=121) (actual time=0.016..0.037 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '43653413-a4e5-4fce-8197-5e94ebffbd59'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:21:19.575202+00'::timestamp with time zone, '912186b6-d6ec-4eb9-85d7-c1cff28b4a98'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=54
|
||
Planning:
|
||
Buffers: shared hit=111
|
||
Planning Time: 0.203 ms
|
||
Execution Time: 0.068 ms
|
||
|
||
### Без индекса ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=544.81..544.94 rows=51 width=121) (actual time=0.832..0.836 rows=51.00 loops=1)
|
||
Buffers: shared hit=209
|
||
-> Sort (cost=544.81..557.31 rows=5000 width=121) (actual time=0.831..0.834 rows=51.00 loops=1)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 44kB
|
||
Buffers: shared hit=209
|
||
-> Seq Scan on messages (cost=0.00..378.00 rows=5000 width=121) (actual time=0.206..0.559 rows=5000.00 loops=1)
|
||
Filter: ((chat_id = '43653413-a4e5-4fce-8197-5e94ebffbd59'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:21:19.575202+00'::timestamp with time zone, '912186b6-d6ec-4eb9-85d7-c1cff28b4a98'::uuid)))
|
||
Rows Removed by Filter: 5000
|
||
Buffers: shared hit=203
|
||
Planning:
|
||
Buffers: shared hit=15
|
||
Planning Time: 0.086 ms
|
||
Execution Time: 0.848 ms
|