forked from mtusi2026/mtusi2026lab2
реализован уровень 4
This commit is contained in:
Executable
+114
@@ -0,0 +1,114 @@
|
||||
\set ON_ERROR_STOP on
|
||||
\pset pager off
|
||||
|
||||
BEGIN;
|
||||
|
||||
INSERT INTO users (
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
password_hash
|
||||
)
|
||||
VALUES (
|
||||
'00000000-0000-4000-8000-000000000041',
|
||||
'level4_explain_user',
|
||||
'Level 4 EXPLAIN user',
|
||||
'synthetic-not-a-login-hash'
|
||||
);
|
||||
|
||||
INSERT INTO chats (
|
||||
id,
|
||||
title
|
||||
)
|
||||
VALUES (
|
||||
'00000000-0000-4000-8000-000000000042',
|
||||
'Level 4 EXPLAIN chat'
|
||||
);
|
||||
|
||||
INSERT INTO chat_members (
|
||||
chat_id,
|
||||
user_id,
|
||||
position
|
||||
)
|
||||
VALUES (
|
||||
'00000000-0000-4000-8000-000000000042',
|
||||
'00000000-0000-4000-8000-000000000041',
|
||||
0
|
||||
);
|
||||
|
||||
WITH generated AS (
|
||||
SELECT
|
||||
number,
|
||||
md5('level4-message-' || number::text) AS digest
|
||||
FROM generate_series(1, 10000) AS series(number)
|
||||
)
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
)
|
||||
SELECT
|
||||
(
|
||||
substr(digest, 1, 8) || '-' ||
|
||||
substr(digest, 9, 4) || '-4' ||
|
||||
substr(digest, 14, 3) || '-a' ||
|
||||
substr(digest, 18, 3) || '-' ||
|
||||
substr(digest, 21, 12)
|
||||
)::uuid,
|
||||
'00000000-0000-4000-8000-000000000042',
|
||||
'00000000-0000-4000-8000-000000000041',
|
||||
'Synthetic history message ' || number,
|
||||
TIMESTAMPTZ '2026-01-01 00:00:00+00'
|
||||
+ number * INTERVAL '1 microsecond'
|
||||
FROM generated;
|
||||
|
||||
ANALYZE messages;
|
||||
|
||||
\echo 'Synthetic message count (must be 10000):'
|
||||
SELECT count(*) AS synthetic_message_count
|
||||
FROM messages
|
||||
WHERE chat_id = '00000000-0000-4000-8000-000000000042';
|
||||
|
||||
\echo 'First page plan:'
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE chat_id = '00000000-0000-4000-8000-000000000042'
|
||||
ORDER BY created_at, id
|
||||
LIMIT 101;
|
||||
|
||||
SELECT
|
||||
id AS cursor_id,
|
||||
created_at AS cursor_created_at
|
||||
FROM messages
|
||||
WHERE chat_id = '00000000-0000-4000-8000-000000000042'
|
||||
ORDER BY created_at, id
|
||||
OFFSET 4999
|
||||
LIMIT 1
|
||||
\gset
|
||||
|
||||
\echo 'Page after cursor plan:'
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE chat_id = '00000000-0000-4000-8000-000000000042'
|
||||
AND (created_at, id) > (
|
||||
:'cursor_created_at'::timestamptz,
|
||||
:'cursor_id'::uuid
|
||||
)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 101;
|
||||
|
||||
ROLLBACK;
|
||||
Reference in New Issue
Block a user