Review a supplied SQL query, schema and execution plan to propose rewrite and index experiments with equal-result checks, tradeoffs and no promised speedup.
Act as a database reviewer. Analyze the supplied query, schema and execution plan. This review is about how the database executes one query; redesigning the application's query pattern (for example N+1 calls) is a separate application review. ## Inputs Database engine and version: [DATABASE] SQL query: [QUERY] Schema and existing indexes: [SCHEMA] Execution plan output (EXPLAIN, or EXPLAIN ANALYZE where it is safe to execute): [PLAN] Row counts and value distribution of the filtered or joined columns: [DATA_CHARACTERISTICS] Write rate on these tables and other queries that depend on them: [WORKLOAD] ## Instructions 1. Summarize the plan: list each expensive node, its access method (scan, index scan, join type, sort) and, if actual rows are shown, where estimates and actuals diverge. 2. Identify causes that are visible in the plan or query text: predicates that wrap an indexed column in a function, implicit casts, missing or unused indexes, join order, large sorts or hashes that spill, and stale statistics. 3. For each candidate change (a query rewrite, a new or altered index, refreshed statistics), give: - the change, as SQL (CREATE INDEX statements included); - the plan evidence that motivates it; - tradeoffs, such as extra write cost, storage, lock behavior while building the index (for example PostgreSQL's CREATE INDEX CONCURRENTLY), or a changed sort order; - an experiment: compare the plan before and after on representative non-production data; - an equal-results check: both versions must return identical rows for the same parameters. 4. Remember that EXPLAIN ANALYZE in PostgreSQL executes the statement; wrap data-changing statements in BEGIN ... ROLLBACK. 5. If no plan was supplied, list exactly which plan output and statistics you need, and keep any suggestions labelled as hypotheses. ## Return 1. Plan summary table: node, access method, estimated vs actual rows, notes 2. Findings, ordered by likely impact, each tied to plan evidence 3. Candidate changes with SQL, tradeoffs, experiment and equal-results check 4. What not to change and why (for example, an index that would duplicate an existing one) 5. Open questions and missing evidence ## Acceptance checks - No timing or percentage improvement is claimed without supplied measurements. - Every rewrite has an equal-results check; semantics such as NULL handling, time zones and duplicates are preserved. - Index suggestions account for existing indexes and write workload. - Production changes are preceded by a non-production experiment. ## Compact version Optimize this SQL query: ```sql [QUERY_HERE] ``` Database: [MYSQL/POSTGRES/SQLSERVER] Table sizes: [SIZES] Provide: 1. Optimized query 2. Explanation of changes 3. Index recommendations 4. Execution plan analysis 5. Alternative approaches
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Database: PostgreSQL 18 Query: SELECT id FROM events WHERE created_at::date = '2026-10-01' ORDER BY id; Schema: events(id bigint primary key, account_id bigint, created_at timestamptz); index events_created_at ON events(created_at). Plan (excerpt): Sort -> Seq Scan on events, Filter: ((created_at)::date = '2026-10-01'::date), Rows Removed by Filter: 4.9M Data: about 5M rows spread evenly over 60 days; about 2,000 inserts per minute.
Illustrative properties of a correct review; this is not a recorded model run. The review says the cast on created_at stops the planner from using events_created_at, and proposes a half-open range: created_at >= '2026-10-01' AND created_at < '2026-10-02', with the session time zone stated because created_at is timestamptz. It asks for a before-and-after EXPLAIN and an identical-rows check, and does not promise a speedup. It treats a new index as unnecessary because one already exists on created_at. A local SQLite fixture demonstrated the same pattern on 5,000 synthetic rows: both forms returned the same 95 rows, and the plan changed from 'SCAN events' to 'SEARCH events USING COVERING INDEX events_created_at'. Only the plan shape was compared, not timing.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Write one SQL query from a plain-language request and your schema, with explicit NULL, join and date-range handling plus a small fixture with expected rows.
Design a relational schema from your entities and query patterns, with keys, constraints, indexes justified by queries, and a migration plan.
Review application query patterns, repeated database calls and correctness before optimizing SQL.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
IDEs
Models