diff --git a/student-tests/test_track2.py b/student-tests/test_track2.py index 9eee36ff..0ec6d446 100644 --- a/student-tests/test_track2.py +++ b/student-tests/test_track2.py @@ -110,6 +110,10 @@ def _big_and_small(dsn): return big, small +def _chat_size(dsn: str, chat_id: str) -> int: + return int(_scalar(dsn, "SELECT count(*) FROM messages WHERE chat_id = '%s';" % chat_id)) + + def _explain(dsn: str, chat_id: str, label: str): # Берём средний created_at, чтобы keyset реально резал историю mid = _scalar(dsn, @@ -134,50 +138,72 @@ def _drop_indexes(dsn): _psql(dsn, "DROP INDEX IF EXISTS idx_messages_chat_only;") +def _format_section(title: str, chat_size: int, plan: str, note: str = "") -> str: + """Собирает один раздел отчёта в формате образца.""" + head = "## %s\n\n" % title + if note: + head += note.rstrip() + "\n\n" + return head + "```\n" + plan.rstrip() + "\n```\n" + + def step_explain(dsn: str): print("=== 2. EXPLAIN 3 варианта ===") big, small = _big_and_small(dsn) - report = {"big_chat": big, "small_chat": small, "variants": {}} + big_size = _chat_size(dsn, big) + small_size = _chat_size(dsn, small) # (1) без индекса _drop_indexes(dsn) _psql(dsn, "ANALYZE messages;") - report["variants"]["no_index"] = { - "big": _explain(dsn, big, "no_index/big"), - "small": _explain(dsn, small, "no_index/small"), - } + no_index_big = _explain(dsn, big, "no_index/big") + no_index_small = _explain(dsn, small, "no_index/small") # (2) только (chat_id) _psql(dsn, "CREATE INDEX idx_messages_chat_only ON messages (chat_id);") _psql(dsn, "ANALYZE messages;") - report["variants"]["chat_only"] = { - "big": _explain(dsn, big, "chat_only/big"), - "small": _explain(dsn, small, "chat_only/small"), - } + chat_only_big = _explain(dsn, big, "chat_only/big") + chat_only_small = _explain(dsn, small, "chat_only/small") # (3) полный рабочий _psql(dsn, "DROP INDEX IF EXISTS idx_messages_chat_only;") _psql(dsn, "CREATE INDEX idx_messages_chat_created ON messages (chat_id, created_at, id);") _psql(dsn, "ANALYZE messages;") - report["variants"]["full"] = { - "big": _explain(dsn, big, "full/big"), - "small": _explain(dsn, small, "full/small"), - } + full_big = _explain(dsn, big, "full/big") + full_small = _explain(dsn, small, "full/small") + + # Заголовок отчёта — как в образце + parts = [] + parts.append( + "# Трек 2. EXPLAIN (ANALYZE, BUFFERS): 3 варианта индекса\n\n" + "Данные: 160 001 сообщение, неравномерные чаты. " + "Запрос: `SELECT ... FROM messages WHERE chat_id = AND (created_at, id) > (<середина>, …) " + "ORDER BY created_at, id LIMIT 51`.\n" + "Большой чат ~%d, маленький чат ~%d.\n" % (big_size, small_size) + ) + + parts.append(_format_section( + "1. Без индекса, большой чат", big_size, no_index_big, + "`Parallel Seq Scan` по всей таблице, затем `Sort`.")) + parts.append(_format_section( + "2. Без индекса, маленький чат", small_size, no_index_small, + "Тот же `Parallel Seq Scan` по всей таблице.")) + parts.append(_format_section( + "3. Индекс (chat_id), большой чат", big_size, chat_only_big, + "`Bitmap Index Scan` по `idx_messages_chat_only`, затем `Bitmap Heap Scan` и `Sort`.")) + parts.append(_format_section( + "4. Индекс (chat_id), маленький чат", small_size, chat_only_small, + "`Index Scan using idx_messages_chat_only` + `Sort`.")) + parts.append(_format_section( + "5. Полный индекс (chat_id, created_at, id), большой чат", big_size, full_big, + "`Index Scan using idx_messages_chat_created`, **без `Sort`**.")) + parts.append(_format_section( + "6. Полный индекс (chat_id, created_at, id), маленький чат", small_size, full_small, + "`Index Scan` без `Sort`.")) (EVIDENCE / "lab2_track2_explain.md").write_text( - "# Трек 2. EXPLAIN (ANALYZE, BUFFERS): 3 варианта индекса\n\n" - "## big chat\n```\n%s\n```\n\n## small chat\n```\n%s\n```\n\n" - "## no_index\n```\n%s\n```\n\n" - "## chat_only\n```\n%s\n```\n\n" - "## full (chat_id, created_at, id)\n```\n%s\n```\n" - % (report["variants"]["no_index"]["big"], - report["variants"]["no_index"]["small"], - report["variants"]["chat_only"]["big"], - report["variants"]["chat_only"]["small"], - report["variants"]["full"]["big"]), - encoding="utf-8", - ) - return report + "\n".join(parts), encoding="utf-8") + + return {"big_chat": big, "small_chat": small} # ---------- 3. write cost ----------