forked from mtusi2026/mtusi2026lab2
74 lines
3.5 KiB
PL/PgSQL
74 lines
3.5 KiB
PL/PgSQL
-- Обоснование индекса истории messages_history (migrations/001_init.sql).
|
|
-- Запуск: python student-tests/explain.py (результат -> evidence/explain-history.txt)
|
|
--
|
|
-- Всё в одной транзакции, в конце ROLLBACK: тестовые данные не остаются в базе,
|
|
-- скрипт можно запускать сколько угодно раз.
|
|
|
|
\pset footer off
|
|
BEGIN;
|
|
|
|
-- Тестовые данные: один большой чат на 15 000 сообщений и 50 маленьких по 100,
|
|
-- всего 20 000 сообщений. Чужие сообщения в таблице есть, как в настоящей базе.
|
|
CREATE TEMP TABLE seed_chats AS
|
|
SELECT gen_random_uuid() AS id, n, CASE WHEN n = 0 THEN 15000 ELSE 100 END AS size
|
|
FROM generate_series(0, 50) AS n;
|
|
CREATE TEMP TABLE seed_user AS SELECT gen_random_uuid() AS id;
|
|
|
|
INSERT INTO users (id, username, display_name, password_hash)
|
|
SELECT id, 'explain_' || substr(md5(random()::text), 1, 10), 'Explain', 'not-a-real-hash' FROM seed_user;
|
|
INSERT INTO chats (id, title) SELECT id, 'explain ' || n FROM seed_chats;
|
|
INSERT INTO chat_members (chat_id, user_id) SELECT c.id, u.id FROM seed_chats c, seed_user u;
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT gen_random_uuid(), c.id, u.id, 'message ' || g,
|
|
timestamptz '2026-09-01 00:00:00+00' + g * interval '1 second'
|
|
FROM seed_chats c, seed_user u, generate_series(1, c.size) AS g;
|
|
ANALYZE messages;
|
|
|
|
SELECT id AS big_chat FROM seed_chats WHERE n = 0 \gset
|
|
-- Курсор глубоко в истории: ключ (created_at, id) 10 000-го сообщения большого чата.
|
|
SELECT created_at AS cursor_time, id AS cursor_id FROM messages
|
|
WHERE chat_id = :'big_chat' ORDER BY created_at, id OFFSET 9999 LIMIT 1 \gset
|
|
|
|
\echo '=== Data volume'
|
|
SELECT count(*) AS messages_total,
|
|
count(*) FILTER (WHERE chat_id = :'big_chat') AS messages_in_big_chat
|
|
FROM messages;
|
|
|
|
\echo ''
|
|
\echo '=== History index'
|
|
SELECT indexdef FROM pg_indexes WHERE indexname = 'messages_history';
|
|
|
|
\echo ''
|
|
\echo '=== 1. First page: limit=50, the app asks for 51 rows to know if there is a next page'
|
|
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = :'big_chat'
|
|
ORDER BY created_at, id LIMIT 51;
|
|
|
|
\echo ''
|
|
\echo '=== 2. Page after the 10000th message by keyset cursor (this is what the app does)'
|
|
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = :'big_chat' AND (created_at, id) > (:'cursor_time'::timestamptz, :'cursor_id'::uuid)
|
|
ORDER BY created_at, id LIMIT 51;
|
|
|
|
\echo ''
|
|
\echo '=== 3. The same page with OFFSET 10000 (the app does NOT do this)'
|
|
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = :'big_chat'
|
|
ORDER BY created_at, id OFFSET 10000 LIMIT 51;
|
|
|
|
\echo ''
|
|
\echo '=== 4. Query 2 without the index (index is dropped only inside this transaction)'
|
|
DROP INDEX messages_history;
|
|
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = :'big_chat' AND (created_at, id) > (:'cursor_time'::timestamptz, :'cursor_id'::uuid)
|
|
ORDER BY created_at, id LIMIT 51;
|
|
|
|
ROLLBACK;
|
|
\echo ''
|
|
\echo '=== ROLLBACK: test data removed, index is back'
|
|
SELECT count(*) AS explain_users_left FROM users WHERE username LIKE 'explain\_%';
|