* повторным запуском обновила цифры в отчетах
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -1,139 +1,139 @@
|
||||
# Трек 2. EXPLAIN (ANALYZE, BUFFERS): 3 варианта индекса
|
||||
|
||||
Данные: 160 001 сообщение, неравномерные чаты (61818 / 54545 / 18182 / 9777 / … / 1).
|
||||
Запрос: `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`.
|
||||
|
||||
```
|
||||
Limit (cost=4887.94..4893.75 rows=51 width=69) (actual time=36.242..39.932 rows=51.00 loops=1)
|
||||
Buffers: shared hit=2028
|
||||
-> Gather Merge (cost=4887.94..6211.25 rows=11611 width=69) (actual time=36.240..39.926 rows=51.00 loops=1)
|
||||
Workers Planned: 1
|
||||
Workers Launched: 1
|
||||
Buffers: shared hit=2028
|
||||
-> Sort (cost=3887.93..3905.01 rows=6830 width=69) (actual time=7.197..7.202 rows=25.50 loops=2)
|
||||
Limit (cost=6593.26..6599.20 rows=51 width=70) (actual time=10.966..12.790 rows=51.00 loops=1)
|
||||
Buffers: shared hit=3210
|
||||
-> Gather Merge (cost=6593.26..11539.13 rows=42466 width=70) (actual time=10.964..12.782 rows=51.00 loops=1)
|
||||
Workers Planned: 2
|
||||
Workers Launched: 2
|
||||
Buffers: shared hit=3210
|
||||
-> Sort (cost=5593.24..5637.48 rows=17694 width=70) (actual time=7.715..7.718 rows=39.00 loops=3)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 35kB
|
||||
Buffers: shared hit=2028
|
||||
Worker 0: Sort Method: quicksort Memory: 25kB
|
||||
-> Parallel Seq Scan on messages (cost=0.00..3660.07 rows=6830 width=69) (actual time=3.983..6.075 rows=20000.00 loops=2)
|
||||
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)))
|
||||
Rows Removed by Filter: 60000
|
||||
Buffers: shared hit=2013
|
||||
Sort Method: top-N heapsort Memory: 36kB
|
||||
Buffers: shared hit=3210
|
||||
Worker 0: Sort Method: top-N heapsort Memory: 36kB
|
||||
Worker 1: Sort Method: top-N heapsort Memory: 36kB
|
||||
-> Parallel Seq Scan on messages (cost=0.00..5002.93 rows=17694 width=70) (actual time=3.301..5.795 rows=26666.67 loops=3)
|
||||
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`.
|
||||
Тот же `Parallel Seq Scan` по всей таблице.
|
||||
|
||||
```
|
||||
Limit (cost=4660.63..4660.64 rows=5 width=69) (actual time=36.246..41.037 rows=1.00 loops=1)
|
||||
Buffers: shared hit=2019
|
||||
-> Sort (cost=4660.63..4660.64 rows=5 width=69) (actual time=36.242..41.032 rows=1.00 loops=1)
|
||||
Limit (cost=6003.04..6003.05 rows=1 width=70) (actual time=7.881..10.103 rows=1.00 loops=1)
|
||||
Buffers: shared hit=3186
|
||||
-> Sort (cost=6003.04..6003.05 rows=1 width=70) (actual time=7.879..10.099 rows=1.00 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: quicksort Memory: 25kB
|
||||
Buffers: shared hit=2019
|
||||
-> Gather (cost=1000.00..4660.57 rows=5 width=69) (actual time=0.522..41.001 rows=1.00 loops=1)
|
||||
Workers Planned: 1
|
||||
Workers Launched: 1
|
||||
Buffers: shared hit=2013
|
||||
-> Parallel Seq Scan on messages (cost=0.00..3660.07 rows=3 width=69) (actual time=0.052..5.988 rows=0.50 loops=2)
|
||||
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)))
|
||||
Rows Removed by Filter: 80000
|
||||
Buffers: shared hit=2013
|
||||
Buffers: shared hit=3186
|
||||
-> Gather (cost=1000.00..6003.03 rows=1 width=70) (actual time=7.667..10.055 rows=1.00 loops=1)
|
||||
Workers Planned: 2
|
||||
Workers Launched: 2
|
||||
Buffers: shared hit=3180
|
||||
-> Parallel Seq Scan on messages (cost=0.00..5002.93 rows=1 width=70) (actual time=3.431..4.199 rows=0.33 loops=3)
|
||||
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`.
|
||||
|
||||
```
|
||||
Limit (cost=4135.73..4135.86 rows=51 width=69) (actual time=9.172..9.177 rows=51.00 loops=1)
|
||||
Buffers: shared hit=863
|
||||
-> Sort (cost=4135.73..4164.10 rows=11346 width=69) (actual time=9.169..9.172 rows=51.00 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 35kB
|
||||
Buffers: shared hit=863
|
||||
-> Bitmap Heap Scan on messages (cost=676.65..3757.21 rows=11346 width=69) (actual time=2.808..6.995 rows=40000.00 loops=1)
|
||||
Recheck Cond: (chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid)
|
||||
Filter: (ROW(created_at, id) > ROW('2026-10-02 15:04:18.650063+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))
|
||||
Rows Removed by Filter: 21818
|
||||
Heap Blocks: exact=803
|
||||
Buffers: shared hit=857
|
||||
-> Bitmap Index Scan on idx_messages_chat_only (cost=0.00..673.82 rows=61003 width=0) (actual time=0.873..0.873 rows=61818.00 loops=1)
|
||||
Index Cond: (chat_id = 'b54ecd23-caa8-4861-b0ad-ffd4651a5e7f'::uuid)
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=54
|
||||
Limit (cost=6594.20..6600.14 rows=51 width=70) (actual time=9.705..11.642 rows=51.00 loops=1)
|
||||
Buffers: shared hit=3210
|
||||
-> Gather Merge (cost=6594.20..11547.87 rows=42533 width=70) (actual time=9.688..11.619 rows=51.00 loops=1)
|
||||
Workers Planned: 2
|
||||
Workers Launched: 2
|
||||
Buffers: shared hit=3210
|
||||
-> Sort (cost=5594.17..5638.48 rows=17722 width=70) (actual time=6.872..6.874 rows=37.33 loops=3)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 36kB
|
||||
Buffers: shared hit=3210
|
||||
Worker 0: Sort Method: top-N heapsort Memory: 36kB
|
||||
Worker 1: Sort Method: top-N heapsort Memory: 37kB
|
||||
-> Parallel Seq Scan on messages (cost=0.00..5002.93 rows=17722 width=70) (actual time=2.667..5.224 rows=26666.67 loops=3)
|
||||
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`.
|
||||
|
||||
```
|
||||
Limit (cost=28.62..28.64 rows=11 width=69) (actual time=0.040..0.041 rows=1.00 loops=1)
|
||||
Buffers: shared hit=9
|
||||
-> Sort (cost=28.62..28.64 rows=11 width=69) (actual time=0.038..0.038 rows=1.00 loops=1)
|
||||
Limit (cost=32.15..32.16 rows=4 width=70) (actual time=0.055..0.056 rows=1.00 loops=1)
|
||||
Buffers: shared hit=10
|
||||
-> Sort (cost=32.15..32.16 rows=4 width=70) (actual time=0.054..0.055 rows=1.00 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: quicksort Memory: 25kB
|
||||
Buffers: shared hit=9
|
||||
-> Index Scan using idx_messages_chat_only on messages (cost=0.29..28.43 rows=11 width=69) (actual time=0.017..0.017 rows=1.00 loops=1)
|
||||
Index Cond: (chat_id = '730e474b-405e-4558-8cf3-ac6a17c2a7d1'::uuid)
|
||||
Filter: (ROW(created_at, id) > ROW('2026-10-02 01:23:13.456347+03'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))
|
||||
Buffers: shared hit=10
|
||||
-> Index Scan using idx_messages_chat_only on messages (cost=0.42..32.11 rows=4 width=70) (actual time=0.022..0.023 rows=1.00 loops=1)
|
||||
Index Cond: (chat_id = '61004e52-701f-4f72-ab45-62c0a1ab97ec'::uuid)
|
||||
Filter: (ROW(created_at, id) > ROW('2026-10-02 14:24:06.811443+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=3
|
||||
Buffers: shared hit=4
|
||||
Planning:
|
||||
Buffers: shared hit=100
|
||||
Planning Time: 1.450 ms
|
||||
Execution Time: 0.069 ms
|
||||
Planning Time: 0.642 ms
|
||||
Execution Time: 0.112 ms
|
||||
```
|
||||
|
||||
## 5. Полный индекс (chat_id, created_at, id), большой чат
|
||||
|
||||
`Index Scan using idx_messages_chat_created`, **без `Sort`**. ~52 буфера. `Execution Time = 0.214 ms`.
|
||||
`Index Scan using idx_messages_chat_created`, **без `Sort`**.
|
||||
|
||||
```
|
||||
Limit (cost=0.42..28.88 rows=51 width=69) (actual time=0.072..0.187 rows=51.00 loops=1)
|
||||
Buffers: shared hit=51 read=1
|
||||
-> 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)))
|
||||
Limit (cost=0.42..17.05 rows=51 width=70) (actual time=0.093..0.150 rows=51.00 loops=1)
|
||||
Buffers: shared hit=53 read=1
|
||||
-> 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` без `Sort`.
|
||||
|
||||
```
|
||||
Limit (cost=0.42..28.71 rows=8 width=69) (actual time=0.029..0.030 rows=1.00 loops=1)
|
||||
Limit (cost=0.42..8.44 rows=1 width=70) (actual time=0.028..0.029 rows=1.00 loops=1)
|
||||
Buffers: shared hit=4
|
||||
-> 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)))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=4
|
||||
Planning:
|
||||
Buffers: shared hit=111
|
||||
Planning Time: 2.160 ms
|
||||
Execution Time: 0.072 ms
|
||||
Planning Time: 0.846 ms
|
||||
Execution Time: 0.105 ms
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user