выполнение лр2 на 4

This commit is contained in:
Ilya
2026-10-09 12:44:56 +03:00
parent e914e384fc
commit 13fa1852cb
30 changed files with 585 additions and 238 deletions
-7
View File
@@ -1,8 +1,4 @@
#!/bin/sh
# Seed synthetic history (20 000 messages in one chat + 50 000 in 50 other chats) and
# record EXPLAIN (ANALYZE, BUFFERS) of the exact keyset queries used by the API,
# with and without messages_chat_history_idx. Output: evidence/explain_history.txt
# Seeding is idempotent (fixed IDs, ON CONFLICT DO NOTHING); data is synthetic.
set -eu
cd "$(dirname "$0")/.."
@@ -15,7 +11,6 @@ INSERT INTO users (id, username, display_name, password_hash) VALUES
('00000000-0000-4000-8000-000000000002', 'seed_explain_b', 'Seed B', 'not-a-login')
ON CONFLICT DO NOTHING;
-- chat ...0100 is the measured one; ...0101-0150 are background noise.
INSERT INTO chats (id, title, created_at)
SELECT ('00000000-0000-4000-8000-' || lpad((100 + g)::text, 12, '0'))::uuid, 'explain ' || g, now()
FROM generate_series(0, 50) g
@@ -50,7 +45,6 @@ SQL
$PSQL -tA -c "SELECT '# messages total: ' || count(*) || ', in measured chat: ' || count(*) FILTER (WHERE chat_id = '00000000-0000-4000-8000-000000000100') FROM messages"
echo
$PSQL <<'SQL'
-- The same statements as app/storage.py (Store.list_messages), as prepared statements.
PREPARE first_page(uuid, int) AS
SELECT id, chat_id, sender_id, text, created_at FROM messages
WHERE chat_id = $1 ORDER BY created_at, id LIMIT $2;
@@ -65,7 +59,6 @@ EXPLAIN (ANALYZE, BUFFERS) EXECUTE first_page('00000000-0000-4000-8000-000000000
EXPLAIN (ANALYZE, BUFFERS) EXECUTE next_page('00000000-0000-4000-8000-000000000100',
timestamptz '2026-01-01' + interval '15000 seconds', md5('explain-0-15000')::uuid, 51);
-- Same queries without the index, inside a transaction that is rolled back.
BEGIN;
DROP INDEX messages_chat_history_idx;
\echo '=== 3. WITHOUT index: first page ==='