Запрос: `SELECT ... FROM messages WHERE chat_id = <id> AND (created_at, id) > (<середина>, …) ORDER BY created_at, id LIMIT 51`.
Большой чат ~62k, маленький чат ~1.
Данные: 250002 сообщений, неравномерные чаты. Запрос: `SELECT ... FROM messages WHERE chat_id = <id> AND (created_at, id) > (<середина>, …) ORDER BY created_at, id LIMIT 51`.
Большой чат ~151818, маленький чат ~1.
## 1. Без индекса, большой чат
`Parallel Seq Scan` по всей таблице, затем `Sort (top-N heapsort)`. Прочитано ~2013 буферов. `Execution Time = 39.988 ms`.
`Parallel Seq Scan` по всей таблице, затем `Sort`.
Filter: ((chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Filter: ((chat_id = '362231ce-de25-4394-a32c-9dcddc4fabe0'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:00:32.39108+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Rows Removed by Filter: 56667
Buffers: shared hit=3180
Planning:
Buffers: shared hit=88
Planning Time: 1.550 ms
Execution Time: 39.988 ms
Planning Time: 0.571 ms
Execution Time: 12.922 ms
```
## 2. Без индекса, маленький чат
Тот же `Parallel Seq Scan` по всей таблице, но условий мало — `Sort (quicksort)`. `Execution Time = 41.093 ms`.
Filter: ((chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Filter: ((chat_id = '61004e52-701f-4f72-ab45-62c0a1ab97ec'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 14:24:06.811443+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Rows Removed by Filter: 83334
Buffers: shared hit=3180
Planning:
Buffers: shared hit=88
Planning Time: 1.837 ms
Execution Time: 41.093 ms
Planning Time: 0.662 ms
Execution Time: 10.192 ms
```
## 3. Индекс (chat_id), большой чат
`Bitmap Index Scan` по `idx_messages_chat_only`, затем `Bitmap Heap Scan` и `Sort`. ~863 буфера. `Execution Time = 9.220 ms`.
`Bitmap Index Scan` по `idx_messages_chat_only`, затем `Bitmap Heap Scan` и `Sort`.
Filter: ((chat_id = '362231ce-de25-4394-a32c-9dcddc4fabe0'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:00:32.39108+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Rows Removed by Filter: 56667
Buffers: shared hit=3180
Planning:
Buffers: shared hit=100
Planning Time: 1.562 ms
Execution Time: 9.220 ms
Planning Time: 0.552 ms
Execution Time: 11.745 ms
```
## 4. Индекс (chat_id), маленький чат
`Index Scan using idx_messages_chat_only` + `Sort (quicksort)`. ~9 буферов. `Execution Time = 0.069 ms`.
`Index Scan using idx_messages_chat_only` + `Sort`.
-> Index Scan using idx_messages_chat_created on messages (cost=0.42..6391.22 rows=11452 width=69) (actual time=0.070..0.182 rows=51.00 loops=1)
Index Cond: ((chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
-> Index Scan using idx_messages_chat_created on messages (cost=0.42..13689.96 rows=41992 width=70) (actual time=0.092..0.146 rows=51.00 loops=1)
Index Cond: ((chat_id = '362231ce-de25-4394-a32c-9dcddc4fabe0'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 15:00:32.39108+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Index Searches: 1
Buffers: shared hit=51 read=1
Buffers: shared hit=53 read=1
Planning:
Buffers: shared hit=111
Planning Time: 2.685 ms
Execution Time: 0.214 ms
Planning Time: 0.751 ms
Execution Time: 0.208 ms
```
## 6. Полный индекс (chat_id, created_at, id), маленький чат
`Index Scan` без `Sort`. ~4 буфера. `Execution Time = 0.072 ms`.
-> Index Scan using idx_messages_chat_created on messages (cost=0.42..28.71 rows=8 width=69) (actual time=0.026..0.027 rows=1.00 loops=1)
Index Cond: ((chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
-> Index Scan using idx_messages_chat_created on messages (cost=0.42..8.44 rows=1 width=70) (actual time=0.027..0.028 rows=1.00 loops=1)
Index Cond: ((chat_id = '61004e52-701f-4f72-ab45-62c0a1ab97ec'::uuid) AND (ROW(created_at, id) > ROW('2026-10-02 14:24:06.811443+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.