forked from mtusi2026/mtusi2026lab2
48 lines
3.5 KiB
Plaintext
48 lines
3.5 KiB
Plaintext
# Grade 4: keyset pagination uses the composite index on >=10k messages
|
|
# dataset
|
|
$ docker compose exec -T postgres psql -U messenger -d messenger -c select count(*) as messages, pg_size_pretty(pg_total_relation_size('messages')) as size from messages;
|
|
messages | size
|
|
----------+---------
|
|
10343 | 2568 kB
|
|
(1 row)
|
|
|
|
|
|
# first page (cursor before history)
|
|
$ docker compose exec -T postgres psql -U messenger -d messenger -c EXPLAIN (ANALYZE, BUFFERS) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d' AND (created_at, id) > ('1970-01-01T00:00:00Z'::timestamptz, '00000000-0000-0000-0000-000000000000'::uuid) ORDER BY created_at, id LIMIT 50;
|
|
QUERY PLAN
|
|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
Limit (cost=0.29..9.97 rows=50 width=81) (actual time=0.015..0.071 rows=50 loops=1)
|
|
Buffers: shared hit=52
|
|
-> Index Scan using messages_chat_created_id_idx on messages (cost=0.29..645.61 rows=3333 width=81) (actual time=0.015..0.067 rows=50 loops=1)
|
|
Index Cond: ((chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d'::uuid) AND (ROW(created_at, id) > ROW('1970-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
|
|
Buffers: shared hit=52
|
|
Planning:
|
|
Buffers: shared hit=106
|
|
Planning Time: 0.248 ms
|
|
Execution Time: 0.098 ms
|
|
(9 rows)
|
|
|
|
|
|
# deep page (cursor at offset 5000) - cost stays flat
|
|
$ docker compose exec -T postgres psql -U messenger -d messenger -c EXPLAIN (ANALYZE, BUFFERS) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d' AND (created_at, id) > (SELECT created_at, id FROM messages WHERE chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d' ORDER BY created_at, id OFFSET 5000 LIMIT 1) ORDER BY created_at, id LIMIT 50;
|
|
QUERY PLAN
|
|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
Limit (cost=186.95..209.67 rows=50 width=81) (actual time=0.620..0.621 rows=0 loops=1)
|
|
Buffers: shared hit=35
|
|
InitPlan 1 (returns $0,$1)
|
|
-> Limit (cost=186.61..186.67 rows=1 width=24) (actual time=0.607..0.608 rows=0 loops=1)
|
|
Buffers: shared hit=35
|
|
-> Index Only Scan using messages_chat_created_id_idx on messages messages_1 (cost=0.29..186.61 rows=3333 width=24) (actual time=0.024..0.511 rows=3333 loops=1)
|
|
Index Cond: (chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d'::uuid)
|
|
Heap Fetches: 0
|
|
Buffers: shared hit=35
|
|
-> Index Scan using messages_chat_created_id_idx on messages (cost=0.29..504.99 rows=1111 width=81) (actual time=0.619..0.619 rows=0 loops=1)
|
|
Index Cond: ((chat_id = '9157b350-faa5-4a4b-9907-c0610e01649d'::uuid) AND (ROW(created_at, id) > ROW($0, $1)))
|
|
Buffers: shared hit=35
|
|
Planning:
|
|
Buffers: shared hit=106
|
|
Planning Time: 0.384 ms
|
|
Execution Time: 0.659 ms
|
|
(16 rows)
|
|
|