исправление

This commit is contained in:
2026-10-15 19:44:46 +03:00
parent 825f6fdbf5
commit f7f689c7c2
26 changed files with 482 additions and 399 deletions
+36 -36
View File
@@ -2,7 +2,7 @@ docker compose exec -T postgres psql -X -q -U messenger -d messenger -v ON_ERROR
=== Объём данных
messages_total | messages_in_big_chat
----------------+----------------------
21635 | 12000
39193 | 12000
=== Индекс истории (migrations/001_core.sql)
@@ -14,63 +14,63 @@ docker compose exec -T postgres psql -X -q -U messenger -d messenger -v ON_ERROR
=== 1. Первая страница (limit=50, приложение просит 51 строку, чтобы понять, есть ли следующая)
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Limit (actual time=0.016..0.030 rows=51 loops=1)
Buffers: shared hit=4
Limit (actual time=0.016..0.031 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (actual time=0.015..0.026 rows=51 loops=1)
Index Cond: (chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid)
Buffers: shared hit=4
Index Cond: (chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid)
Buffers: shared hit=5
Planning:
Buffers: shared hit=7
Planning Time: 0.139 ms
Execution Time: 0.050 ms
Buffers: shared hit=6
Planning Time: 0.186 ms
Execution Time: 0.054 ms
=== 2. Страница из середины истории по keyset-курсору (после 6000-го сообщения)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.009..0.029 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_history_idx on messages (actual time=0.009..0.025 rows=51 loops=1)
Index Cond: ((chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '509a5d7d-2d2f-4322-8b64-938949155ce0'::uuid)))
Buffers: shared hit=5
Planning Time: 0.087 ms
Execution Time: 0.059 ms
Limit (actual time=0.012..0.031 rows=51 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_history_idx on messages (actual time=0.011..0.026 rows=51 loops=1)
Index Cond: ((chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '4cf0e1fe-ddbf-4da7-b5d3-e610ab7325b9'::uuid)))
Buffers: shared hit=6
Planning Time: 0.105 ms
Execution Time: 0.115 ms
=== 3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает)
QUERY PLAN
-----------------------------------------------------------------------------------------------------------
Limit (actual time=1.583..1.600 rows=51 loops=1)
Buffers: shared hit=127
-> Index Scan using messages_chat_history_idx on messages (actual time=0.021..1.393 rows=6051 loops=1)
Index Cond: (chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid)
Buffers: shared hit=127
Planning Time: 0.089 ms
Execution Time: 1.621 ms
Limit (actual time=1.711..1.729 rows=51 loops=1)
Buffers: shared hit=169
-> Index Scan using messages_chat_history_idx on messages (actual time=0.025..1.504 rows=6051 loops=1)
Index Cond: (chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid)
Buffers: shared hit=169
Planning Time: 0.112 ms
Execution Time: 1.751 ms
=== 4. Для сравнения: запрос 2 без индекса (индекс удалён только внутри этой транзакции)
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=2.654..2.660 rows=51 loops=1)
Buffers: shared hit=172
-> Sort (actual time=2.653..2.655 rows=51 loops=1)
Limit (actual time=4.045..4.053 rows=51 loops=1)
Buffers: shared hit=180
-> Sort (actual time=4.044..4.047 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=172
-> Bitmap Heap Scan on messages (actual time=0.782..1.755 rows=6000 loops=1)
Recheck Cond: (chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid)
Filter: (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '509a5d7d-2d2f-4322-8b64-938949155ce0'::uuid))
Buffers: shared hit=180
-> Bitmap Heap Scan on messages (actual time=0.996..2.398 rows=6000 loops=1)
Recheck Cond: (chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid)
Filter: (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, '4cf0e1fe-ddbf-4da7-b5d3-e610ab7325b9'::uuid))
Rows Removed by Filter: 6000
Heap Blocks: exact=161
Buffers: shared hit=172
-> Bitmap Index Scan on messages_idempotency_unique (actual time=0.201..0.202 rows=12000 loops=1)
Index Cond: (chat_id = 'e4e1b497-87b7-4320-9a40-27873d99fc1a'::uuid)
Buffers: shared hit=11
Heap Blocks: exact=168
Buffers: shared hit=180
-> Bitmap Index Scan on messages_idempotency_unique (actual time=0.253..0.253 rows=12000 loops=1)
Index Cond: (chat_id = 'c4170f65-25c8-486b-a880-dcf11a354bca'::uuid)
Buffers: shared hit=12
Planning:
Buffers: shared hit=5
Planning Time: 0.097 ms
Execution Time: 2.681 ms
Planning Time: 0.126 ms
Execution Time: 4.078 ms
=== ROLLBACK: синтетические данные удалены, индекс на месте