forked from mtusi2026/mtusi2026lab2
45 lines
2.0 KiB
PL/PgSQL
45 lines
2.0 KiB
PL/PgSQL
-- Нагрузочные данные для EXPLAIN: 10 000 сообщений в одном чате (синтетика).
|
|
\set ON_ERROR_STOP on
|
|
BEGIN;
|
|
INSERT INTO users (id, username, display_name, password_hash)
|
|
VALUES ('00000000-0000-4000-8000-000000000001', 'explain_user', 'Explain', 'x')
|
|
ON CONFLICT DO NOTHING;
|
|
INSERT INTO chats (id, title)
|
|
VALUES ('00000000-0000-4000-8000-0000000000c1', 'explain-chat')
|
|
ON CONFLICT DO NOTHING;
|
|
INSERT INTO chat_members (chat_id, user_id, position)
|
|
VALUES ('00000000-0000-4000-8000-0000000000c1', '00000000-0000-4000-8000-000000000001', 0)
|
|
ON CONFLICT DO NOTHING;
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT gen_random_uuid(), '00000000-0000-4000-8000-0000000000c1',
|
|
'00000000-0000-4000-8000-000000000001', 'msg ' || g,
|
|
now() - (10001 - g) * interval '1 second'
|
|
FROM generate_series(1, 10000) g
|
|
WHERE NOT EXISTS (SELECT 1 FROM messages WHERE chat_id = '00000000-0000-4000-8000-0000000000c1');
|
|
COMMIT;
|
|
ANALYZE messages;
|
|
|
|
\echo '1. Первая страница (limit 50)'
|
|
EXPLAIN (ANALYZE, BUFFERS)
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = '00000000-0000-4000-8000-0000000000c1'
|
|
ORDER BY created_at, id LIMIT 51;
|
|
|
|
\echo '2. Страница из середины истории (keyset cursor)'
|
|
SELECT created_at AS c_ts, id AS c_id FROM messages
|
|
WHERE chat_id = '00000000-0000-4000-8000-0000000000c1'
|
|
ORDER BY created_at, id OFFSET 5000 LIMIT 1 \gset
|
|
EXPLAIN (ANALYZE, BUFFERS)
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = '00000000-0000-4000-8000-0000000000c1'
|
|
AND (created_at, id) > (:'c_ts'::timestamptz, :'c_id'::uuid)
|
|
ORDER BY created_at, id LIMIT 51;
|
|
|
|
\echo '3. Для сравнения: без индекса messages_history_idx'
|
|
BEGIN;
|
|
DROP INDEX messages_history_idx;
|
|
EXPLAIN (ANALYZE, BUFFERS)
|
|
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
|
WHERE chat_id = '00000000-0000-4000-8000-0000000000c1'
|
|
ORDER BY created_at, id LIMIT 51;
|
|
ROLLBACK; |