ЛР 2: EXPLAIN (ANALYZE, BUFFERS) для истории на 10 000 сообщений
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
BEGIN
|
||||
INSERT 0 0
|
||||
INSERT 0 0
|
||||
INSERT 0 0
|
||||
INSERT 0 0
|
||||
COMMIT
|
||||
ANALYZE
|
||||
1. Первая страница (limit 50)
|
||||
QUERY PLAN
|
||||
---------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=0.29..5.33 rows=51 width=64) (actual time=0.015..0.020 rows=51 loops=1)
|
||||
Buffers: shared hit=3
|
||||
-> Index Scan using messages_history_idx on messages (cost=0.29..989.57 rows=10000 width=64) (actual time=0.015..0.017 rows=51 loops=1)
|
||||
Index Cond: (chat_id = '00000000-0000-4000-8000-0000000000c1'::uuid)
|
||||
Buffers: shared hit=3
|
||||
Planning:
|
||||
Buffers: shared hit=23
|
||||
Planning Time: 0.108 ms
|
||||
Execution Time: 0.037 ms
|
||||
(9 rows)
|
||||
|
||||
2. Страница из середины истории (keyset cursor)
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=0.29..6.63 rows=51 width=64) (actual time=0.007..0.011 rows=51 loops=1)
|
||||
Buffers: shared hit=3
|
||||
-> Index Scan using messages_history_idx on messages (cost=0.29..620.78 rows=4991 width=64) (actual time=0.006..0.009 rows=51 loops=1)
|
||||
Index Cond: ((chat_id = '00000000-0000-4000-8000-0000000000c1'::uuid) AND (ROW(created_at, id) > ROW('2026-11-03 06:25:42.350396+00'::timestamp with time zone, 'e5c90185-a58b-4164-b1b3-cb15e30b97e1'::uuid)))
|
||||
Buffers: shared hit=3
|
||||
Planning Time: 0.027 ms
|
||||
Execution Time: 0.034 ms
|
||||
(7 rows)
|
||||
|
||||
3. Для сравнения: без индекса messages_history_idx
|
||||
BEGIN
|
||||
DROP INDEX
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=581.83..581.96 rows=51 width=64) (actual time=0.915..0.919 rows=51 loops=1)
|
||||
Buffers: shared hit=123
|
||||
-> Sort (cost=581.83..606.83 rows=10000 width=64) (actual time=0.915..0.916 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 31kB
|
||||
Buffers: shared hit=123
|
||||
-> Seq Scan on messages (cost=0.00..248.21 rows=10000 width=64) (actual time=0.004..0.485 rows=10000 loops=1)
|
||||
Filter: (chat_id = '00000000-0000-4000-8000-0000000000c1'::uuid)
|
||||
Rows Removed by Filter: 17
|
||||
Buffers: shared hit=123
|
||||
Planning:
|
||||
Buffers: shared hit=4
|
||||
Planning Time: 0.037 ms
|
||||
Execution Time: 0.941 ms
|
||||
(14 rows)
|
||||
|
||||
ROLLBACK
|
||||
@@ -0,0 +1,45 @@
|
||||
-- Нагрузочные данные для 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;
|
||||
Reference in New Issue
Block a user