Files
mtusi2026lab4/evidence/12-explain.txt
T

129 lines
8.8 KiB
Plaintext

# Индекс истории: EXPLAIN (ANALYZE, BUFFERS), объём данных в первой таблице вывода
# дата: 2026-10-30T20:07Z, чистый clone
# стенд: Apple M3 Pro, 11 CPU, 18 GB RAM, macOS 26.1, Docker 29.3.0, Compose 5.1.0
$ make explain
./scripts/explain_history.sh
== Объём данных
messages_total | messages_in_chat | table_size | index_size
----------------+------------------+------------+------------
101045 | 10000 | 11 MB | 9544 kB
(1 row)
== 1. Первая страница (limit 50, запрос берёт 51 строку)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..729.05 rows=51 width=112) (actual time=0.028..0.228 rows=51 loops=1)
Buffers: shared hit=63
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..142912.03 rows=10003 width=112) (actual time=0.023..0.219 rows=51 loops=1)
Index Cond: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid)
Buffers: shared hit=63
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.004..0.004 rows=1 loops=51)
Buffers: shared hit=54
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.002..0.002 rows=0 loops=51)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=54
-> Bitmap Heap Scan on message_attachments a (cost=4.20..13.67 rows=6 width=20) (actual time=0.000..0.000 rows=0 loops=51)
Recheck Cond: (message_id = messages.id)
Buffers: shared hit=51
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=51)
Index Cond: (message_id = messages.id)
Buffers: shared hit=51
Planning:
Buffers: shared hit=76
Planning Time: 0.174 ms
Execution Time: 0.255 ms
(22 rows)
== 2. Страница из середины истории: keyset после 5000-го сообщения
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.42..738.40 rows=51 width=112) (actual time=0.013..0.198 rows=51 loops=1)
Buffers: shared hit=59
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..101986.67 rows=7048 width=112) (actual time=0.013..0.196 rows=51 loops=1)
Index Cond: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-10-30 11:47:36.896462+00'::timestamp with time zone, '0c56411c-acb3-420b-bd20-5ac5ed7ab8a2'::uuid)))
Buffers: shared hit=59
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.003..0.003 rows=1 loops=51)
Buffers: shared hit=51
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.002..0.002 rows=0 loops=51)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=51
-> Bitmap Heap Scan on message_attachments a (cost=4.20..13.67 rows=6 width=20) (actual time=0.000..0.000 rows=0 loops=51)
Recheck Cond: (message_id = messages.id)
Buffers: shared hit=51
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=51)
Index Cond: (message_id = messages.id)
Buffers: shared hit=51
Planning Time: 0.045 ms
Execution Time: 0.232 ms
(20 rows)
== 3. Та же страница через OFFSET (так делать не стали)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=71434.79..72163.43 rows=51 width=112) (actual time=5.082..5.132 rows=51 loops=1)
Buffers: shared hit=5552
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..142912.03 rows=10003 width=112) (actual time=0.012..5.035 rows=5051 loops=1)
Index Cond: (chat_id = '00000000-0000-4000-8000-000000000100'::uuid)
Buffers: shared hit=5552
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.001..0.001 rows=1 loops=5051)
Buffers: shared hit=5051
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.001..0.001 rows=0 loops=5051)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=5051
-> Bitmap Heap Scan on message_attachments a (cost=4.20..13.67 rows=6 width=20) (actual time=0.000..0.000 rows=0 loops=5051)
Recheck Cond: (message_id = messages.id)
Buffers: shared hit=5051
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=5051)
Index Cond: (message_id = messages.id)
Buffers: shared hit=5051
Planning Time: 0.032 ms
Execution Time: 5.152 ms
(20 rows)
== 4. Запрос из пункта 2 без индекса messages_chat_history_idx (DROP INDEX внутри транзакции с ROLLBACK)
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=3400.42..4104.22 rows=51 width=112) (actual time=3.126..3.183 rows=51 loops=1)
Buffers: shared hit=1454
-> Result (cost=3400.42..100662.45 rows=7048 width=112) (actual time=3.125..3.180 rows=51 loops=1)
Buffers: shared hit=1454
-> Sort (cost=3400.42..3418.04 rows=7048 width=80) (actual time=3.098..3.100 rows=51 loops=1)
Sort Key: messages.created_at, messages.id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=1403
-> Seq Scan on messages (cost=0.00..3165.29 rows=7048 width=80) (actual time=1.800..2.874 rows=5000 loops=1)
Filter: ((chat_id = '00000000-0000-4000-8000-000000000100'::uuid) AND (ROW(created_at, id) > ROW('2026-10-30 11:47:36.896462+00'::timestamp with time zone, '0c56411c-acb3-420b-bd20-5ac5ed7ab8a2'::uuid)))
Rows Removed by Filter: 96045
Buffers: shared hit=1397
SubPlan 1
-> Aggregate (cost=13.78..13.79 rows=1 width=32) (actual time=0.001..0.001 rows=1 loops=51)
Buffers: shared hit=51
-> Sort (cost=13.74..13.76 rows=6 width=20) (actual time=0.001..0.001 rows=0 loops=51)
Sort Key: a."position"
Sort Method: quicksort Memory: 25kB
Buffers: shared hit=51
-> Bitmap Heap Scan on message_attachments a (cost=4.20..13.67 rows=6 width=20) (actual time=0.000..0.000 rows=0 loops=51)
Recheck Cond: (message_id = messages.id)
Buffers: shared hit=51
-> Bitmap Index Scan on message_attachments_pkey (cost=0.00..4.20 rows=6 width=0) (actual time=0.000..0.000 rows=0 loops=51)
Index Cond: (message_id = messages.id)
Buffers: shared hit=51
Planning:
Buffers: shared hit=4
Planning Time: 0.050 ms
Execution Time: 3.202 ms
(29 rows)