rows total=100000, rows in target chat=20000, page=50 PostgreSQL 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, 64-bit ==================================================================================================== A. Первая страница, keyset, С индексом (chat_id, created_at, id) ==================================================================================================== Limit (cost=0.42..14.81 rows=51 width=69) (actual time=0.010..0.021 rows=51 loops=1) Buffers: shared hit=4 -> Index Scan using messages_chat_created_id_idx on messages (cost=0.42..5670.87 rows=20093 width=69) (actual time=0.009..0.016 rows=51 loops=1) Index Cond: (chat_id = '010619e6-7ff5-46b1-877c-03368e85f10b'::uuid) Buffers: shared hit=4 Planning: Buffers: shared hit=6 Planning Time: 0.063 ms Execution Time: 0.032 ms ==================================================================================================== B. Страница на глубине ~15 000, keyset, С индексом ==================================================================================================== Limit (cost=0.42..48.66 rows=51 width=69) (actual time=0.011..0.021 rows=51 loops=1) Buffers: shared hit=4 -> Index Scan using messages_chat_created_id_idx on messages (cost=0.42..4756.71 rows=5028 width=69) (actual time=0.011..0.016 rows=51 loops=1) Index Cond: ((chat_id = '010619e6-7ff5-46b1-877c-03368e85f10b'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 20:50:05.000522+00'::timestamp with time zone, 'f47f8dd1-aaae-4701-a00d-6e20a06648e3'::uuid))) Buffers: shared hit=4 Planning Time: 0.059 ms Execution Time: 0.044 ms ==================================================================================================== C. Та же страница, keyset, БЕЗ индекса (индекс удалён в откатываемой транзакции) ==================================================================================================== Limit (cost=3152.74..3152.87 rows=51 width=69) (actual time=9.057..9.066 rows=51 loops=1) Buffers: shared hit=1235 -> Sort (cost=3152.74..3165.31 rows=5028 width=69) (actual time=9.056..9.060 rows=51 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 31kB Buffers: shared hit=1235 -> Seq Scan on messages (cost=0.00..2985.00 rows=5028 width=69) (actual time=1.098..8.625 rows=4999 loops=1) Filter: ((chat_id = '010619e6-7ff5-46b1-877c-03368e85f10b'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 20:50:05.000522+00'::timestamp with time zone, 'f47f8dd1-aaae-4701-a00d-6e20a06648e3'::uuid))) Rows Removed by Filter: 95001 Buffers: shared hit=1235 Planning: Buffers: shared hit=4 Planning Time: 0.065 ms Execution Time: 9.081 ms ==================================================================================================== D. Та же глубина через OFFSET 15000, С индексом (антипример) ==================================================================================================== Limit (cost=3958.59..3958.71 rows=51 width=69) (actual time=10.512..10.519 rows=51 loops=1) Buffers: shared hit=1235 -> Sort (cost=3921.09..3971.32 rows=20093 width=69) (actual time=9.103..9.920 rows=15051 loops=1) Sort Key: created_at, id Sort Method: quicksort Memory: 3271kB Buffers: shared hit=1235 -> Seq Scan on messages (cost=0.00..2485.00 rows=20093 width=69) (actual time=0.003..6.416 rows=20000 loops=1) Filter: (chat_id = '010619e6-7ff5-46b1-877c-03368e85f10b'::uuid) Rows Removed by Filter: 80000 Buffers: shared hit=1235 Planning: Buffers: shared hit=22 Planning Time: 0.101 ms Execution Time: 10.643 ms ==================================================================================================== E. Первая страница, БЕЗ индекса ==================================================================================================== Limit (cost=3155.35..3155.47 rows=51 width=69) (actual time=7.702..7.709 rows=51 loops=1) Buffers: shared hit=1235 -> Sort (cost=3155.35..3205.58 rows=20093 width=69) (actual time=7.701..7.704 rows=51 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 31kB Buffers: shared hit=1235 -> Seq Scan on messages (cost=0.00..2485.00 rows=20093 width=69) (actual time=0.004..6.070 rows=20000 loops=1) Filter: (chat_id = '010619e6-7ff5-46b1-877c-03368e85f10b'::uuid) Rows Removed by Filter: 80000 Buffers: shared hit=1235 Planning: Buffers: shared hit=4 Planning Time: 0.060 ms Execution Time: 7.721 ms Индексы messages: messages_chat_created_id_idx -> CREATE INDEX messages_chat_created_id_idx ON public.messages USING btree (chat_id, created_at, id) messages_pkey -> CREATE UNIQUE INDEX messages_pkey ON public.messages USING btree (id) Размер индекса: 8536 kB ; таблицы: 9880 kB