добавлен эксперимент плана запроса истории сообщений

This commit is contained in:
2026-10-08 01:22:59 +03:00
parent c1200032b5
commit 3662e9ff5c
2 changed files with 155 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
=== Lab 2 Grade 4: message history index experiment ===
=== Preparing synthetic benchmark user ===
INSERT 0 0
=== Preparing test chat ===
INSERT 0 0
INSERT 0 0
=== Preparing 20000 messages ===
INSERT 0 0
=== Test data size ===
message_count
---------------
20000
(1 row)
=== Refreshing PostgreSQL statistics ===
ANALYZE
=== OFFSET pagination after 15000 messages ===
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=2129.70..2143.90 rows=100 width=82) (actual time=1.883..1.896 rows=100 loops=1)
Buffers: shared hit=435
-> Index Scan using ix_messages_chat_created_id on messages (cost=0.41..2839.47 rows=20000 width=82) (actual time=0.020..1.535 rows=15100 loops=1)
Index Cond: (chat_id = '22222222-2222-4222-8222-222222222222'::uuid)
Buffers: shared hit=435
Planning:
Buffers: shared hit=21
Planning Time: 0.094 ms
Execution Time: 1.908 ms
(9 rows)
=== KEYSET pagination after message 15000 ===
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.41..30.01 rows=100 width=82) (actual time=0.008..0.031 rows=100 loops=1)
Buffers: shared hit=7
-> Index Scan using ix_messages_chat_created_id on messages (cost=0.41..1481.99 rows=5006 width=82) (actual time=0.007..0.022 rows=100 loops=1)
Index Cond: ((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:15+00'::timestamp with time zone, '00003a98-0000-4000-8000-000000003a98'::uuid)))
Buffers: shared hit=7
Planning Time: 0.048 ms
Execution Time: 0.053 ms
(7 rows)
+113
View File
@@ -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;