добавлен эксперимент плана запроса истории сообщений
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
\set ON_ERROR_STOP on
|
||||
|
||||
\echo '=== Lab 2 Grade 4: message history index experiment ==='
|
||||
|
||||
-- Fixed identifiers make the experiment repeatable.
|
||||
\set chat_id '22222222-2222-4222-8222-222222222222'
|
||||
\set user_id '33333333-3333-4333-8333-333333333333'
|
||||
|
||||
\echo '=== Preparing synthetic benchmark user ==='
|
||||
|
||||
INSERT INTO users (
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
password_hash,
|
||||
created_at
|
||||
)
|
||||
VALUES (
|
||||
:'user_id'::uuid,
|
||||
'explain_benchmark_user',
|
||||
'EXPLAIN benchmark user',
|
||||
'benchmark-only-not-a-real-password-hash',
|
||||
CURRENT_TIMESTAMP
|
||||
)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
\echo '=== Preparing test chat ==='
|
||||
|
||||
INSERT INTO chats (id, title)
|
||||
VALUES (
|
||||
:'chat_id'::uuid,
|
||||
'EXPLAIN test 20000 messages'
|
||||
)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id)
|
||||
VALUES (
|
||||
:'chat_id'::uuid,
|
||||
:'user_id'::uuid
|
||||
)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
|
||||
\echo '=== Preparing 20000 messages ==='
|
||||
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
)
|
||||
SELECT
|
||||
(
|
||||
lpad(to_hex(g), 8, '0') ||
|
||||
'-0000-4000-8000-' ||
|
||||
lpad(to_hex(g), 12, '0')
|
||||
)::uuid,
|
||||
:'chat_id'::uuid,
|
||||
:'user_id'::uuid,
|
||||
'EXPLAIN test message ' || g,
|
||||
'2026-01-01 00:00:00+00'::timestamptz
|
||||
+ g * interval '1 millisecond'
|
||||
FROM generate_series(1, 20000) AS g
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
|
||||
\echo '=== Test data size ==='
|
||||
|
||||
SELECT COUNT(*) AS message_count
|
||||
FROM messages
|
||||
WHERE chat_id = :'chat_id'::uuid;
|
||||
|
||||
|
||||
\echo '=== Refreshing PostgreSQL statistics ==='
|
||||
|
||||
ANALYZE messages;
|
||||
|
||||
|
||||
\echo '=== OFFSET pagination after 15000 messages ==='
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'chat_id'::uuid
|
||||
ORDER BY created_at, id
|
||||
OFFSET 15000
|
||||
LIMIT 100;
|
||||
|
||||
|
||||
\echo '=== KEYSET pagination after message 15000 ==='
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'chat_id'::uuid
|
||||
AND (created_at, id) >
|
||||
(
|
||||
'2026-01-01 00:00:15+00'::timestamptz,
|
||||
'00003a98-0000-4000-8000-000000003a98'::uuid
|
||||
)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 100;
|
||||
Reference in New Issue
Block a user