forked from mtusi2026/mtusi2026lab5
76 lines
2.7 KiB
PL/PgSQL
76 lines
2.7 KiB
PL/PgSQL
-- EXPLAIN evidence for the message history query (make explain).
|
|
--
|
|
-- Everything runs in one transaction that is rolled back: it seeds 12 000
|
|
-- messages in one chat and 90 000 in 300 other chats, so the plan is measured
|
|
-- on a realistic volume without leaving data on the stand. The PREPARE text
|
|
-- is the statement of Messages.ListByChat in internal/chat/store/postgres.go;
|
|
-- keep them identical.
|
|
\set ON_ERROR_STOP on
|
|
\pset pager off
|
|
BEGIN;
|
|
|
|
SELECT uuidv7() AS author \gset
|
|
SELECT uuidv7() AS big_chat \gset
|
|
|
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
|
VALUES (:'author', 'explain_' || substr(md5(random()::text), 1, 12), 'EXPLAIN', 'not-a-password-hash',
|
|
uuid_extract_timestamp(:'author'));
|
|
|
|
CREATE TEMP TABLE noise_chats ON COMMIT DROP AS
|
|
SELECT uuidv7() AS id FROM generate_series(1, 300);
|
|
|
|
INSERT INTO chats (id, title, created_at)
|
|
SELECT :'big_chat'::uuid, 'explain: big chat', uuid_extract_timestamp(:'big_chat')
|
|
UNION ALL
|
|
SELECT id, 'explain: other chat', uuid_extract_timestamp(id) FROM noise_chats;
|
|
|
|
INSERT INTO chat_members (chat_id, user_id)
|
|
SELECT id, :'author' FROM chats WHERE title LIKE 'explain:%';
|
|
|
|
-- Rows are inserted ordered by n, so the messages of all chats interleave in
|
|
-- the heap as they would under real traffic; ids stay increasing per chat.
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT id, chat_id, :'author', 'message ' || n, uuid_extract_timestamp(id)
|
|
FROM (
|
|
SELECT uuidv7() AS id, chat_id, n
|
|
FROM (
|
|
SELECT :'big_chat'::uuid AS chat_id, n FROM generate_series(1, 12000) AS n
|
|
UNION ALL
|
|
SELECT c.id, n FROM noise_chats AS c, generate_series(1, 300) AS n
|
|
) AS planned
|
|
ORDER BY n
|
|
) AS generated;
|
|
|
|
ANALYZE users, chats, chat_members, messages;
|
|
|
|
SELECT (SELECT count(*) FROM messages WHERE chat_id = :'big_chat') AS big_chat_messages,
|
|
(SELECT count(*) FROM messages) AS all_messages;
|
|
|
|
PREPARE history(uuid, uuid, int) AS
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = $1
|
|
AND id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
|
|
ORDER BY id
|
|
LIMIT $3;
|
|
|
|
\echo
|
|
\echo == First page: no cursor, limit 50 (+1 row that tells whether a next page exists)
|
|
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', NULL, 51);
|
|
|
|
SELECT id AS deep_cursor FROM messages WHERE chat_id = :'big_chat' ORDER BY id OFFSET 10000 LIMIT 1 \gset
|
|
\echo
|
|
\echo == Deep page: cursor after the 10 001st message of the chat
|
|
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', :'deep_cursor', 51);
|
|
|
|
\echo
|
|
\echo == For contrast, OFFSET pagination of the same page reads every skipped row
|
|
EXPLAIN (ANALYZE, BUFFERS)
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = :'big_chat'
|
|
ORDER BY id
|
|
OFFSET 10001 LIMIT 51;
|
|
|
|
ROLLBACK;
|