"""EXPLAIN (ANALYZE, BUFFERS) запроса истории на синтетических данных. Данные создаются в одной транзакции и откатываются в конце: база не меняется. """ import asyncio from uuid import uuid4 from sqlalchemy import text from sqlalchemy.ext.asyncio import AsyncConnection from app.core.config import Settings from app.core.database import create_db_engine TARGET_MESSAGES = 10_000 NOISE_MESSAGES = 40_000 PAGE = 51 HISTORY_FIRST_PAGE = """ SELECT * FROM messages WHERE chat_id = :chat_id ORDER BY created_at, id LIMIT :page """ HISTORY_AFTER_CURSOR = """ SELECT * FROM messages WHERE chat_id = :chat_id AND (created_at, id) > (:after_created_at, :after_id) ORDER BY created_at, id LIMIT :page """ async def seed(connection: AsyncConnection) -> dict[str, object]: user_id, target_id, noise_id = uuid4(), uuid4(), uuid4() await connection.execute( text( "INSERT INTO users (id, username, display_name, password_hash) " "VALUES (:id, :username, 'Explain', 'x')" ), {"id": user_id, "username": "explain_" + uuid4().hex[:8]}, ) await connection.execute( text("INSERT INTO chats (id, title) VALUES (:a, 'target'), (:b, 'noise')"), {"a": target_id, "b": noise_id}, ) await connection.execute( text("INSERT INTO chat_members (chat_id, user_id) VALUES (:a, :u), (:b, :u)"), {"a": target_id, "b": noise_id, "u": user_id}, ) for chat_id, count in ((target_id, TARGET_MESSAGES), (noise_id, NOISE_MESSAGES)): await connection.execute( text( "INSERT INTO messages (id, chat_id, sender_id, text, created_at) " "SELECT gen_random_uuid(), :chat_id, :user_id, 'message ' || g, " "now() - make_interval(secs => :count - g) " "FROM generate_series(1, :count) AS g" ), {"chat_id": chat_id, "user_id": user_id, "count": count}, ) await connection.execute(text("ANALYZE messages")) middle = ( await connection.execute( text( "SELECT created_at, id FROM messages WHERE chat_id = :c " "ORDER BY created_at, id OFFSET :o LIMIT 1" ), {"c": target_id, "o": TARGET_MESSAGES // 2}, ) ).one() return { "chat_id": target_id, "after_created_at": middle.created_at, "after_id": middle.id, "page": PAGE, } async def explain( connection: AsyncConnection, title: str, query: str, params: dict[str, object] ) -> None: rows = await connection.execute( text(f"EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING ON) {query}"), params ) print(f"== {title}") print(query.strip()) print() for row in rows: print(row[0]) print() async def main() -> None: engine = create_db_engine(Settings()) # type: ignore[call-arg] async with engine.connect() as connection: transaction = await connection.begin() params = await seed(connection) total = ( await connection.execute(text("SELECT count(*) FROM messages")) ).scalar_one() print( f"messages in table: {total}; target chat: {TARGET_MESSAGES}; noise chat: {NOISE_MESSAGES}" ) print() first_page = {"chat_id": params["chat_id"], "page": PAGE} await explain( connection, "first page (limit+1 = 51)", HISTORY_FIRST_PAGE, first_page ) await explain( connection, "page after cursor in the middle of history", HISTORY_AFTER_CURSOR, params, ) await connection.execute(text("SET LOCAL enable_indexscan = off")) await connection.execute(text("SET LOCAL enable_bitmapscan = off")) await explain( connection, "same page with index scans disabled (for comparison)", HISTORY_AFTER_CURSOR, params, ) await transaction.rollback() await engine.dispose() if __name__ == "__main__": asyncio.run(main())