docker compose exec -T postgres psql -X -q -U messenger -d messenger -v ON_ERROR_STOP=1 < infra/sql/explain_history.sql === Объём данных messages_total | messages_in_big_chat ----------------+---------------------- 21618 | 12000 === Индекс истории (migrations/001_core.sql) indexdef ------------------------------------------------------------------------------------------------- CREATE INDEX messages_chat_history_idx ON public.messages USING btree (chat_id, created_at, id) === 1. Первая страница (limit=50, приложение просит 51 строку, чтобы понять, есть ли следующая) QUERY PLAN --------------------------------------------------------------------------------------------------------- Limit (actual time=0.018..0.041 rows=51 loops=1) Buffers: shared hit=5 -> Index Scan using messages_chat_history_idx on messages (actual time=0.017..0.033 rows=51 loops=1) Index Cond: (chat_id = '4981747a-a263-417e-80a3-fb7063eb8be5'::uuid) Buffers: shared hit=5 Planning: Buffers: shared hit=6 Planning Time: 0.139 ms Execution Time: 0.066 ms === 2. Страница из середины истории по keyset-курсору (после 6000-го сообщения) QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (actual time=0.009..0.020 rows=51 loops=1) Buffers: shared hit=4 -> Index Scan using messages_chat_history_idx on messages (actual time=0.009..0.016 rows=51 loops=1) Index Cond: ((chat_id = '4981747a-a263-417e-80a3-fb7063eb8be5'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '41b24ea4-c9d1-467e-a5cc-6b4a5413a8be'::uuid))) Buffers: shared hit=4 Planning Time: 0.068 ms Execution Time: 0.076 ms === 3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает) QUERY PLAN ----------------------------------------------------------------------------------------------------------- Limit (actual time=1.377..1.386 rows=51 loops=1) Buffers: shared hit=161 -> Index Scan using messages_chat_history_idx on messages (actual time=0.023..1.131 rows=6051 loops=1) Index Cond: (chat_id = '4981747a-a263-417e-80a3-fb7063eb8be5'::uuid) Buffers: shared hit=161 Planning Time: 0.099 ms Execution Time: 1.406 ms === 4. Для сравнения: запрос 2 без индекса (индекс удалён только внутри этой транзакции) QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (actual time=2.522..2.528 rows=51 loops=1) Buffers: shared hit=296 -> Sort (actual time=2.522..2.524 rows=51 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 31kB Buffers: shared hit=296 -> Seq Scan on messages (actual time=0.631..2.057 rows=6000 loops=1) Filter: ((chat_id = '4981747a-a263-417e-80a3-fb7063eb8be5'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '41b24ea4-c9d1-467e-a5cc-6b4a5413a8be'::uuid))) Rows Removed by Filter: 15618 Buffers: shared hit=296 Planning: Buffers: shared hit=4 Planning Time: 0.128 ms Execution Time: 2.554 ms === ROLLBACK: синтетические данные удалены, индекс на месте