# План запроса истории: класс medium, чат 6k Код: src_sha256 8a7e256c540af433, commit fee6f77c3bf1; вариант индекса: chat-only Страница истории читаемого чата (limit 50); позиция — сколько сообщений истории до страницы: - first: после 0 - middle: после 3000 - last: после 5950 ## Планы приложения auto_explain (ANALYZE, BUFFERS, TIMING): запрос, параметры и план, с которыми его выполнило приложение (prepared statement asyncpg). ### first duration: 2.159 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 = '4eb01ce4-c529-4fa1-b477-0413137a44f8', $2 = '51' Limit (cost=4266.49..4266.62 rows=51 width=204) (actual time=2.129..2.134 rows=51.00 loops=1) Buffers: shared hit=3028 -> Sort (cost=4266.49..4281.37 rows=5951 width=204) (actual time=2.128..2.130 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 38kB Buffers: shared hit=3028 -> Bitmap Heap Scan on messages (cost=82.41..4067.95 rows=5951 width=204) (actual time=0.385..1.726 rows=6000.00 loops=1) Recheck Cond: (chat_id = '4eb01ce4-c529-4fa1-b477-0413137a44f8'::uuid) Heap Blocks: exact=3019 Buffers: shared hit=3028 -> Bitmap Index Scan on ix_messages_chat_id (cost=0.00..80.92 rows=5951 width=0) (actual time=0.188..0.188 rows=6000.00 loops=1) Index Cond: (chat_id = '4eb01ce4-c529-4fa1-b477-0413137a44f8'::uuid) Index Searches: 1 Buffers: shared hit=9 ### middle duration: 1.600 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 = '4eb01ce4-c529-4fa1-b477-0413137a44f8', $2 = '2026-10-02 01:37:07.232997+00', $3 = '621a870c-4a04-4a2f-90e4-74a36b636aba', $4 = '51' Limit (cost=603.27..603.29 rows=6 width=204) (actual time=1.571..1.574 rows=51.00 loops=1) Buffers: shared hit=3028 -> Sort (cost=603.27..603.42 rows=60 width=204) (actual time=1.570..1.572 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 40kB Buffers: shared hit=3028 -> Bitmap Heap Scan on messages (cost=5.67..601.50 rows=60 width=204) (actual time=0.591..1.421 rows=3000.00 loops=1) Recheck Cond: (chat_id = $1) Filter: (ROW(created_at, id) > ROW($2, $3)) Rows Removed by Filter: 3000 Heap Blocks: exact=3019 Buffers: shared hit=3028 -> Bitmap Index Scan on ix_messages_chat_id (cost=0.00..5.65 rows=181 width=0) (actual time=0.181..0.181 rows=6000.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=9 ### last duration: 1.201 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 = '4eb01ce4-c529-4fa1-b477-0413137a44f8', $2 = '2026-10-02 01:38:03.263099+00', $3 = '46bd6798-0c14-4ed0-9a97-720472ecf8ef', $4 = '51' Limit (cost=603.27..603.29 rows=6 width=204) (actual time=1.177..1.180 rows=50.00 loops=1) Buffers: shared hit=3028 -> Sort (cost=603.27..603.42 rows=60 width=204) (actual time=1.176..1.178 rows=50.00 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 37kB Buffers: shared hit=3028 -> Bitmap Heap Scan on messages (cost=5.67..601.50 rows=60 width=204) (actual time=1.162..1.170 rows=50.00 loops=1) Recheck Cond: (chat_id = $1) Filter: (ROW(created_at, id) > ROW($2, $3)) Rows Removed by Filter: 5950 Heap Blocks: exact=3019 Buffers: shared hit=3028 -> Bitmap Index Scan on ix_messages_chat_id (cost=0.00..5.65 rows=181 width=0) (actual time=0.169..0.169 rows=6000.00 loops=1) Index Cond: (chat_id = $1) Index Searches: 1 Buffers: shared hit=9 ## Страница middle в psql: с индексом и без Тот же текст запроса и те же параметры, что у плана middle. psql планирует их заново (custom plan). DROP INDEX — внутри транзакции, затем ROLLBACK: индекс остаётся в базе. ### С индексом ix_messages_chat_id Limit (cost=4197.56..4197.69 rows=51 width=204) (actual time=4.308..4.316 rows=51.00 loops=1) Buffers: shared hit=3034 -> Sort (cost=4197.56..4205.10 rows=3015 width=204) (actual time=4.307..4.309 rows=51.00 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 40kB Buffers: shared hit=3034 -> Bitmap Heap Scan on messages (cost=81.68..4096.97 rows=3015 width=204) (actual time=1.377..4.094 rows=3000.00 loops=1) Recheck Cond: (chat_id = '4eb01ce4-c529-4fa1-b477-0413137a44f8'::uuid) Filter: (ROW(created_at, id) > ROW('2026-10-02 01:37:07.232997+00'::timestamp with time zone, '621a870c-4a04-4a2f-90e4-74a36b636aba'::uuid)) Rows Removed by Filter: 3000 Heap Blocks: exact=3019 Buffers: shared hit=3028 -> Bitmap Index Scan on ix_messages_chat_id (cost=0.00..80.92 rows=5951 width=0) (actual time=0.356..0.356 rows=6000.00 loops=1) Index Cond: (chat_id = '4eb01ce4-c529-4fa1-b477-0413137a44f8'::uuid) Index Searches: 1 Buffers: shared hit=9 Planning: Buffers: shared hit=130 Planning Time: 0.289 ms Execution Time: 4.361 ms ### Без индекса ix_messages_chat_id Limit (cost=5712.68..5718.62 rows=51 width=204) (actual time=3.646..4.634 rows=51.00 loops=1) Buffers: shared hit=3782 -> Gather Merge (cost=5712.68..6063.71 rows=3014 width=204) (actual time=3.645..4.630 rows=51.00 loops=1) Workers Planned: 2 Workers Launched: 2 Buffers: shared hit=3782 -> Sort (cost=4712.65..4715.79 rows=1256 width=204) (actual time=2.496..2.497 rows=42.00 loops=3) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 49kB Buffers: shared hit=3782 Worker 0: Sort Method: top-N heapsort Memory: 48kB Worker 1: Sort Method: top-N heapsort Memory: 45kB -> Parallel Seq Scan on messages (cost=0.00..4670.75 rows=1256 width=204) (actual time=0.515..2.417 rows=1000.00 loops=3) Filter: ((chat_id = '4eb01ce4-c529-4fa1-b477-0413137a44f8'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:37:07.232997+00'::timestamp with time zone, '621a870c-4a04-4a2f-90e4-74a36b636aba'::uuid))) Rows Removed by Filter: 41000 Buffers: shared hit=3752 Planning: Buffers: shared hit=17 Planning Time: 0.098 ms Execution Time: 4.645 ms