Files
mtusi2026lab5/evidence/explain-history.txt
T

100 lines
5.6 KiB
Plaintext

=== Data volume
messages_total | messages_in_big_chat
----------------+----------------------
29169 | 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.005..0.012 rows=51 loops=1)
Buffers: shared hit=14
-> Index Scan using messages_history on messages (actual time=0.005..0.009 rows=51 loops=1)
Index Cond: (chat_id = 'ee34a051-241a-4a88-a3cd-c6d8cc5d2152'::uuid)
Buffers: shared hit=14
Planning:
Buffers: shared hit=6
Planning Time: 0.058 ms
Execution Time: 0.020 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=5
-> Index Scan using messages_history on messages (actual time=0.005..0.010 rows=51 loops=1)
Index Cond: ((chat_id = 'ee34a051-241a-4a88-a3cd-c6d8cc5d2152'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, 'b3d18836-9770-4887-8b0d-12b3e59970c1'::uuid)))
Buffers: shared hit=5
Planning Time: 0.043 ms
Execution Time: 0.029 ms
=== 2b. The same page exactly as the app sends it since lab4: with attachment_ids of every message
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=0.020..0.155 rows=51 loops=1)
Buffers: shared hit=59
-> Index Scan using messages_history on messages m (actual time=0.019..0.152 rows=51 loops=1)
Index Cond: ((chat_id = 'ee34a051-241a-4a88-a3cd-c6d8cc5d2152'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, 'b3d18836-9770-4887-8b0d-12b3e59970c1'::uuid)))
Buffers: shared hit=59
SubPlan 1
-> Sort (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 (actual time=0.000..0.000 rows=0 loops=51)
Recheck Cond: (message_id = m.id)
Buffers: shared hit=51
-> Bitmap Index Scan on message_attachments_message_id_attachment_id_key (actual time=0.000..0.000 rows=0 loops=51)
Index Cond: (message_id = m.id)
Buffers: shared hit=51
Planning:
Buffers: shared hit=63
Planning Time: 0.205 ms
Execution Time: 0.172 ms
=== 3. The same page with OFFSET 10000 (the app does NOT do this)
QUERY PLAN
---------------------------------------------------------------------------------------------------
Limit (actual time=0.832..0.838 rows=51 loops=1)
Buffers: shared hit=266
-> Index Scan using messages_history on messages (actual time=0.007..0.645 rows=10051 loops=1)
Index Cond: (chat_id = 'ee34a051-241a-4a88-a3cd-c6d8cc5d2152'::uuid)
Buffers: shared hit=266
Planning Time: 0.041 ms
Execution Time: 0.849 ms
=== 4. Query 2 without the index (index is dropped only inside this transaction)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=1.257..1.261 rows=51 loops=1)
Buffers: shared hit=450
-> Sort (actual time=1.257..1.258 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=450
-> Seq Scan on messages (actual time=0.530..0.937 rows=5000 loops=1)
Filter: ((chat_id = 'ee34a051-241a-4a88-a3cd-c6d8cc5d2152'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, 'b3d18836-9770-4887-8b0d-12b3e59970c1'::uuid)))
Rows Removed by Filter: 24169
Buffers: shared hit=450
Planning:
Buffers: shared hit=4
Planning Time: 0.082 ms
Execution Time: 1.274 ms
=== ROLLBACK: test data removed, index is back
explain_users_left
--------------------
0