106 lines
3.8 KiB
PL/PgSQL
106 lines
3.8 KiB
PL/PgSQL
-- Grade-5 track 2: query-plan comparison on >=100k messages with highly
|
|
-- uneven chat sizes. The deterministic fixture is isolated by username/title
|
|
-- prefixes and can safely be re-run on the same persistent volume.
|
|
\set ON_ERROR_STOP on
|
|
\timing on
|
|
|
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
|
VALUES ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', 'research_seed_user',
|
|
'Research seed user', 'not-a-usable-hash', now())
|
|
ON CONFLICT (id) DO NOTHING;
|
|
|
|
INSERT INTO chats (id, title, created_at)
|
|
VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'research-hot-chat', now())
|
|
ON CONFLICT (id) DO NOTHING;
|
|
|
|
INSERT INTO chat_members (chat_id, user_id, joined_at)
|
|
VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',
|
|
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', now())
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
-- One hot chat with 90k messages and 100 cold chats with 100 messages each.
|
|
-- md5() yields stable 32 hex characters and PostgreSQL accepts it as UUID.
|
|
INSERT INTO chats (id, title, created_at)
|
|
SELECT md5('research-cold-chat-' || n)::uuid, 'research-cold-chat-' || n, now()
|
|
FROM generate_series(1, 100) AS n
|
|
ON CONFLICT (id) DO NOTHING;
|
|
|
|
INSERT INTO chat_members (chat_id, user_id, joined_at)
|
|
SELECT md5('research-cold-chat-' || n)::uuid,
|
|
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', now()
|
|
FROM generate_series(1, 100) AS n
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT md5('research-hot-message-' || n)::uuid,
|
|
'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',
|
|
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
|
|
'hot message ' || n,
|
|
timestamptz '2026-02-01 00:00:00+00' + make_interval(secs => n * 0.001)
|
|
FROM generate_series(1, 90000) AS n
|
|
ON CONFLICT (id) DO NOTHING;
|
|
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT md5('research-cold-message-' || chat_n || '-' || message_n)::uuid,
|
|
md5('research-cold-chat-' || chat_n)::uuid,
|
|
'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
|
|
'cold message ' || chat_n || '-' || message_n,
|
|
timestamptz '2026-02-02 00:00:00+00'
|
|
+ make_interval(secs => (chat_n * 100 + message_n) * 0.001)
|
|
FROM generate_series(1, 100) AS chat_n,
|
|
generate_series(1, 100) AS message_n
|
|
ON CONFLICT (id) DO NOTHING;
|
|
|
|
ANALYZE messages;
|
|
|
|
\echo '=== dataset shape ==='
|
|
SELECT count(*) AS research_messages,
|
|
count(DISTINCT chat_id) AS research_chats,
|
|
max(per_chat.count) AS largest_chat_messages,
|
|
min(per_chat.count) AS smallest_chat_messages
|
|
FROM messages
|
|
JOIN (
|
|
SELECT chat_id, count(*)
|
|
FROM messages
|
|
WHERE sender_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
|
|
GROUP BY chat_id
|
|
) AS per_chat USING (chat_id)
|
|
WHERE messages.sender_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
|
|
|
|
\echo '=== source integrity summary ==='
|
|
SELECT count(*) AS count,
|
|
min(created_at) AS first_message,
|
|
max(created_at) AS last_message,
|
|
sum(length(text)) AS text_bytes
|
|
FROM messages
|
|
WHERE sender_id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
|
|
|
|
\echo '=== index A: production composite (chat_id, created_at, id) ==='
|
|
EXPLAIN (ANALYZE, BUFFERS)
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
|
|
AND (created_at, id) > (
|
|
timestamptz '2026-02-01 00:01:20+00',
|
|
md5('research-hot-message-80000')::uuid
|
|
)
|
|
ORDER BY created_at, id
|
|
LIMIT 50;
|
|
|
|
\echo '=== index B: chat_id-only alternative (temporary, rolled back) ==='
|
|
BEGIN;
|
|
DROP INDEX messages_chat_order_idx;
|
|
CREATE INDEX messages_research_chat_only_idx ON messages (chat_id);
|
|
ANALYZE messages;
|
|
EXPLAIN (ANALYZE, BUFFERS)
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
|
|
AND (created_at, id) > (
|
|
timestamptz '2026-02-01 00:01:20+00',
|
|
md5('research-hot-message-80000')::uuid
|
|
)
|
|
ORDER BY created_at, id
|
|
LIMIT 50;
|
|
ROLLBACK;
|