BEGIN INSERT 0 1 INSERT 0 10 INSERT 0 10 INSERT 0 60000 ANALYZE messages_in_target_chat ------------------------- 12000 (1 row) === 1. Первая страница (limit 50 + 1) === QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.41..14.11 rows=51 width=65) (actual time=0.010..0.018 rows=51 loops=1) Buffers: shared hit=4 -> Index Scan using messages_chat_created_id_idx on messages (cost=0.41..3252.33 rows=12114 width=65) (actual time=0.010..0.014 rows=51 loops=1) Index Cond: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid) Buffers: shared hit=4 Planning: Buffers: shared hit=18 Planning Time: 0.107 ms Execution Time: 0.032 ms (9 rows) === 2. Страница из середины истории (keyset-курсор после ~6000-го сообщения) === QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.42..15.40 rows=51 width=65) (actual time=0.012..0.035 rows=51 loops=1) Buffers: shared hit=5 -> Index Scan using messages_chat_created_id_idx on messages (cost=0.42..3199.66 rows=10891 width=65) (actual time=0.011..0.030 rows=51 loops=1) Index Cond: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW(((now() - '1 day'::interval) + '00:00:06'::interval), '00000000-0000-0000-0000-000000000000'::uuid))) Buffers: shared hit=5 Planning Time: 0.079 ms Execution Time: 0.094 ms (7 rows) === 3. Для сравнения: та же страница, если запретить индекс === SET SET QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=2603.40..2603.52 rows=51 width=65) (actual time=6.820..6.826 rows=51 loops=1) Buffers: shared hit=740 -> Sort (cost=2603.40..2630.62 rows=10891 width=65) (actual time=6.819..6.821 rows=51 loops=1) Sort Key: created_at, id Sort Method: top-N heapsort Memory: 31kB Buffers: shared hit=740 -> Seq Scan on messages (cost=0.00..2240.05 rows=10891 width=65) (actual time=1.638..6.339 rows=6001 loops=1) Filter: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW(((now() - '1 day'::interval) + '00:00:06'::interval), '00000000-0000-0000-0000-000000000000'::uuid))) Rows Removed by Filter: 54001 Buffers: shared hit=740 Planning Time: 0.072 ms Execution Time: 6.841 ms (12 rows) ROLLBACK