forked from mtusi2026/mtusi2026lab2
72 lines
4.4 KiB
Plaintext
72 lines
4.4 KiB
Plaintext
docker compose exec -T postgres psql -X -q -U messenger -d messenger -v ON_ERROR_STOP=1 < infra/sql/explain_history.sql
|
|
=== Объём данных
|
|
messages_total | messages_in_big_chat
|
|
----------------+----------------------
|
|
21650 | 12000
|
|
|
|
|
|
=== Индекс истории (migrations/001_core.sql)
|
|
indexdef
|
|
-------------------------------------------------------------------------------------------------
|
|
CREATE INDEX messages_chat_history_idx ON public.messages USING btree (chat_id, created_at, id)
|
|
|
|
|
|
=== 1. Первая страница (limit=50, приложение просит 51 строку, чтобы понять, есть ли следующая)
|
|
QUERY PLAN
|
|
---------------------------------------------------------------------------------------------------------
|
|
Limit (actual time=0.023..0.043 rows=51 loops=1)
|
|
Buffers: shared hit=6
|
|
-> Index Scan using messages_chat_history_idx on messages (actual time=0.022..0.036 rows=51 loops=1)
|
|
Index Cond: (chat_id = 'b0b6cd43-57d9-4deb-99f3-e04443a88e4c'::uuid)
|
|
Buffers: shared hit=6
|
|
Planning:
|
|
Buffers: shared hit=6
|
|
Planning Time: 0.160 ms
|
|
Execution Time: 0.077 ms
|
|
|
|
|
|
=== 2. Страница из середины истории по keyset-курсору (после 6000-го сообщения)
|
|
QUERY PLAN
|
|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
Limit (actual time=0.019..0.055 rows=51 loops=1)
|
|
Buffers: shared hit=6
|
|
-> Index Scan using messages_chat_history_idx on messages (actual time=0.018..0.044 rows=51 loops=1)
|
|
Index Cond: ((chat_id = 'b0b6cd43-57d9-4deb-99f3-e04443a88e4c'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, 'bc979085-5d84-44a6-9df2-3b1fff1bdaf1'::uuid)))
|
|
Buffers: shared hit=6
|
|
Planning Time: 0.157 ms
|
|
Execution Time: 0.122 ms
|
|
|
|
|
|
=== 3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает)
|
|
QUERY PLAN
|
|
-----------------------------------------------------------------------------------------------------------
|
|
Limit (actual time=1.586..1.603 rows=51 loops=1)
|
|
Buffers: shared hit=163
|
|
-> Index Scan using messages_chat_history_idx on messages (actual time=0.020..1.305 rows=6051 loops=1)
|
|
Index Cond: (chat_id = 'b0b6cd43-57d9-4deb-99f3-e04443a88e4c'::uuid)
|
|
Buffers: shared hit=163
|
|
Planning Time: 0.095 ms
|
|
Execution Time: 1.625 ms
|
|
|
|
|
|
=== 4. Для сравнения: запрос 2 без индекса (индекс удалён только внутри этой транзакции)
|
|
QUERY PLAN
|
|
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
Limit (actual time=4.238..4.247 rows=51 loops=1)
|
|
Buffers: shared hit=289
|
|
-> Sort (actual time=4.237..4.241 rows=51 loops=1)
|
|
Sort Key: created_at, id
|
|
Sort Method: top-N heapsort Memory: 31kB
|
|
Buffers: shared hit=289
|
|
-> Seq Scan on messages (actual time=0.967..3.376 rows=6000 loops=1)
|
|
Filter: ((chat_id = 'b0b6cd43-57d9-4deb-99f3-e04443a88e4c'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:40:00+00'::timestamp with time zone, 'bc979085-5d84-44a6-9df2-3b1fff1bdaf1'::uuid)))
|
|
Rows Removed by Filter: 15650
|
|
Buffers: shared hit=289
|
|
Planning:
|
|
Buffers: shared hit=4
|
|
Planning Time: 0.119 ms
|
|
Execution Time: 4.268 ms
|
|
|
|
|
|
=== ROLLBACK: синтетические данные удалены, индекс на месте
|