100 lines
5.5 KiB
Plaintext
100 lines
5.5 KiB
Plaintext
# План запроса истории: размер 100k
|
||
|
||
Commit e61a0e5
|
||
Страница истории читаемого чата; позиция — сколько сообщений
|
||
истории до страницы:
|
||
- first: после 0
|
||
- middle: после 50000
|
||
- last: после 99900
|
||
|
||
## Планы приложения
|
||
|
||
auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план,
|
||
с которыми его выполнило приложение (prepared statement asyncpg).
|
||
|
||
### first
|
||
|
||
duration: 0.031 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 = '547a9c1d-1b3e-4901-8aee-460715b75b45', $2 = '51'
|
||
Limit (cost=0.42..5.06 rows=51 width=121) (actual time=0.016..0.021 rows=51.00 loops=1)
|
||
Buffers: shared hit=4
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..9110.17 rows=100000 width=121) (actual time=0.016..0.018 rows=51.00 loops=1)
|
||
Index Cond: (chat_id = '547a9c1d-1b3e-4901-8aee-460715b75b45'::uuid)
|
||
Index Searches: 1
|
||
Buffers: shared hit=4
|
||
|
||
### middle
|
||
|
||
duration: 0.042 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 = '547a9c1d-1b3e-4901-8aee-460715b75b45', $2 = '2026-10-02 00:27:22.663427+00', $3 = '438a10bc-cc63-4c01-9f81-62ffadcbc63b', $4 = '51'
|
||
Limit (cost=0.42..6.88 rows=51 width=121) (actual time=0.015..0.031 rows=51.00 loops=1)
|
||
Buffers: shared hit=50
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..6410.36 rows=50569 width=121) (actual time=0.015..0.029 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '547a9c1d-1b3e-4901-8aee-460715b75b45'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:27:22.663427+00'::timestamp with time zone, '438a10bc-cc63-4c01-9f81-62ffadcbc63b'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=50
|
||
|
||
### last
|
||
|
||
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 = '547a9c1d-1b3e-4901-8aee-460715b75b45', $2 = '2026-10-02 00:28:16.874516+00', $3 = '2ecf99f9-79eb-4983-a427-a5845bf8f655', $4 = '51'
|
||
Limit (cost=0.42..93.66 rows=51 width=121) (actual time=0.016..0.040 rows=51.00 loops=1)
|
||
Buffers: shared hit=54
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..159.47 rows=87 width=121) (actual time=0.016..0.037 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '547a9c1d-1b3e-4901-8aee-460715b75b45'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:28:16.874516+00'::timestamp with time zone, '2ecf99f9-79eb-4983-a427-a5845bf8f655'::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.42..6.88 rows=51 width=121) (actual time=0.031..0.055 rows=51.00 loops=1)
|
||
Buffers: shared hit=50
|
||
-> Index Scan using ix_messages_chat_id_created_at_id on messages (cost=0.42..6410.36 rows=50569 width=121) (actual time=0.030..0.051 rows=51.00 loops=1)
|
||
Index Cond: ((chat_id = '547a9c1d-1b3e-4901-8aee-460715b75b45'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:27:22.663427+00'::timestamp with time zone, '438a10bc-cc63-4c01-9f81-62ffadcbc63b'::uuid)))
|
||
Index Searches: 1
|
||
Buffers: shared hit=50
|
||
Planning:
|
||
Buffers: shared hit=111
|
||
Planning Time: 0.252 ms
|
||
Execution Time: 0.091 ms
|
||
|
||
### Без индекса ix_messages_chat_id_created_at_id
|
||
|
||
Limit (cost=4954.81..4960.62 rows=51 width=121) (actual time=5.630..6.910 rows=51.00 loops=1)
|
||
Buffers: shared hit=1948
|
||
-> Gather Merge (cost=4954.81..10718.08 rows=50568 width=121) (actual time=5.629..6.895 rows=51.00 loops=1)
|
||
Workers Planned: 1
|
||
Workers Launched: 1
|
||
Buffers: shared hit=1948
|
||
-> Sort (cost=3954.80..4029.17 rows=29746 width=121) (actual time=4.649..4.651 rows=34.00 loops=2)
|
||
Sort Key: created_at, id
|
||
Sort Method: top-N heapsort Memory: 42kB
|
||
Buffers: shared hit=1948
|
||
Worker 0: Sort Method: top-N heapsort Memory: 51kB
|
||
-> Parallel Seq Scan on messages (cost=0.00..2962.41 rows=29746 width=121) (actual time=0.894..3.243 rows=25000.00 loops=2)
|
||
Filter: ((chat_id = '547a9c1d-1b3e-4901-8aee-460715b75b45'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 00:27:22.663427+00'::timestamp with time zone, '438a10bc-cc63-4c01-9f81-62ffadcbc63b'::uuid)))
|
||
Rows Removed by Filter: 25000
|
||
Buffers: shared hit=1933
|
||
Planning:
|
||
Buffers: shared hit=15
|
||
Planning Time: 0.088 ms
|
||
Execution Time: 6.933 ms
|