Files

80 lines
4.1 KiB
Plaintext

=== Data volume
messages_total | messages_in_big_chat
----------------+----------------------
48658 | 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.011 rows=51 loops=1)
Buffers: shared hit=4
-> Index Scan using messages_history on messages (actual time=0.006..0.009 rows=51 loops=1)
Index Cond: (chat_id = 'b246d1e6-d41a-4111-aad6-c0474ab30641'::uuid)
Buffers: shared hit=4
Planning:
Buffers: shared hit=7
Planning Time: 0.073 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.007..0.011 rows=51 loops=1)
Buffers: shared hit=4
-> Index Scan using messages_history on messages (actual time=0.006..0.009 rows=51 loops=1)
Index Cond: ((chat_id = 'b246d1e6-d41a-4111-aad6-c0474ab30641'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, 'c1c5eccc-7e7c-4a03-a48e-3a8fb237e518'::uuid)))
Buffers: shared hit=4
Planning Time: 0.044 ms
Execution Time: 0.030 ms
=== 3. The same page with OFFSET 10000 (the app does NOT do this)
QUERY PLAN
--------------------------------------------------------------------------------
Limit (actual time=3.445..3.448 rows=51 loops=1)
Buffers: shared hit=860
-> Sort (actual time=3.038..3.253 rows=10051 loops=1)
Sort Key: created_at, id
Sort Method: quicksort Memory: 2377kB
Buffers: shared hit=860
-> Seq Scan on messages (actual time=0.007..1.686 rows=15000 loops=1)
Filter: (chat_id = 'b246d1e6-d41a-4111-aad6-c0474ab30641'::uuid)
Rows Removed by Filter: 33658
Buffers: shared hit=860
Planning Time: 0.037 ms
Execution Time: 3.511 ms
=== 4. Query 2 without the index (index is dropped only inside this transaction)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (actual time=1.864..1.868 rows=51 loops=1)
Buffers: shared hit=860
-> Sort (actual time=1.864..1.865 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=860
-> Seq Scan on messages (actual time=1.115..1.524 rows=5000 loops=1)
Filter: ((chat_id = 'b246d1e6-d41a-4111-aad6-c0474ab30641'::uuid) AND (ROW(created_at, id) > ROW('2026-09-01 02:46:40+00'::timestamp with time zone, 'c1c5eccc-7e7c-4a03-a48e-3a8fb237e518'::uuid)))
Rows Removed by Filter: 43658
Buffers: shared hit=860
Planning:
Buffers: shared hit=4
Planning Time: 0.051 ms
Execution Time: 1.882 ms
=== ROLLBACK: test data removed, index is back
explain_users_left
--------------------
0