Files
mtusi2026lab2/evidence/explain-history.txt

75 lines
3.9 KiB
Plaintext

=== Data volume
messages_total | messages_in_big_chat
----------------+----------------------
20116 | 15000
=== History index
indexdef
----------------------------------------------------------------------------------------
CREATE INDEX messages_history ON public.messages USING btree (chat_id, created_at, id)
=== 1. First page: limit=50, the app asks for 51 rows to know if there is a next page
QUERY PLAN
------------------------------------------------------------------------------------------------
Limit (actual time=0.007..0.012 rows=51 loops=1)
Buffers: shared hit=4
-> Index Scan using messages_history on messages (actual time=0.007..0.009 rows=51 loops=1)
Index Cond: (chat_id = '6a75871f-4837-4b3c-b7c8-1c36de87da98'::uuid)
Buffers: shared hit=4
Planning:
Buffers: shared hit=8
Planning Time: 0.155 ms
Execution Time: 0.028 ms
=== 2. Page after the 10000th message by keyset cursor (this is what the app does)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.006..0.012 rows=51 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_history on messages (actual time=0.006..0.010 rows=51 loops=1)
Index Cond: ((chat_id = '6a75871f-4837-4b3c-b7c8-1c36de87da98'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, 'd867a7e3-2b56-475a-81c9-19729e8336c2'::uuid)))
Buffers: shared hit=6
Planning Time: 0.038 ms
Execution Time: 0.031 ms
=== 3. The same page with OFFSET 10000 (the app does NOT do this)
QUERY PLAN
---------------------------------------------------------------------------------------------------
Limit (actual time=0.716..0.721 rows=51 loops=1)
Buffers: shared hit=257
-> Index Scan using messages_history on messages (actual time=0.008..0.521 rows=10051 loops=1)
Index Cond: (chat_id = '6a75871f-4837-4b3c-b7c8-1c36de87da98'::uuid)
Buffers: shared hit=257
Planning Time: 0.028 ms
Execution Time: 0.765 ms
=== 4. Query 2 without the index (index is dropped only inside this transaction)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.862..0.865 rows=51 loops=1)
Buffers: shared hit=255
-> Sort (actual time=0.861..0.863 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=255
-> Seq Scan on messages (actual time=0.289..0.686 rows=5000 loops=1)
Filter: ((chat_id = '6a75871f-4837-4b3c-b7c8-1c36de87da98'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, 'd867a7e3-2b56-475a-81c9-19729e8336c2'::uuid)))
Rows Removed by Filter: 15116
Buffers: shared hit=255
Planning:
Buffers: shared hit=4
Planning Time: 0.051 ms
Execution Time: 0.884 ms
=== ROLLBACK: test data removed, index is back
explain_users_left
--------------------
0