---
name: sql-query-wizard
description: "Write, explain, and optimize Postgres/MySQL queries with indexes and EXPLAIN analysis. Pastes in a schema, writes a query, explains the plan, suggests indexes, and benchmarks before/after."
license: MIT
---

# SQL Query Wizard

SQL Query Wizard helps you write, understand, and speed up SQL against Postgres and MySQL. Paste in a schema and a slow query, and it walks through the execution plan, proposes targeted indexes, and measures the before/after timing so you can see whether the change actually helped. It's aimed at backend engineers and data analysts who can read SQL but don't want to memorize every planner quirk.

## When to use it

- A query that was fast in dev is timing out in production and you need to know why.
- You're staring at an `EXPLAIN` plan full of `Seq Scan`, `Nested Loop`, or `Using filesort` and want it translated.
- You're designing a new table and want index advice before the data grows.
- You need to rewrite a correlated subquery, a `DISTINCT`, or an `OR` chain into something the planner handles better.

## How it works

Give the skill your DDL (`CREATE TABLE` / `CREATE INDEX` statements) plus the query. It will:

1. Run `EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)` on Postgres or `EXPLAIN ANALYZE` / `EXPLAIN FORMAT=JSON` on MySQL and read the plan node by node — flagging row-estimate misses, spilled sorts, and scans that should be index lookups.
2. Suggest concrete index DDL, including composite column order, partial indexes (`WHERE` clauses), covering indexes (`INCLUDE` on Postgres), and expression indexes.
3. Rewrite the query where the shape, not the index, is the bottleneck.
4. Re-run the plan after applying changes and report timing and row deltas side by side.

It distinguishes Postgres and MySQL semantics — e.g. Postgres `INCLUDE` columns vs MySQL covering via the secondary index, and `pg_stat_statements` vs the MySQL `performance_schema` for finding slow queries.

## Example

```
curl -fsSL --create-dirs -o ~/.claude/skills/sql-query-wizard/SKILL.md https://claudeskil.com/skills/sql-query-wizard/SKILL.md

> Schema:
>   CREATE TABLE orders (id bigserial PK, user_id bigint,
>     status text, created_at timestamptz);
> Query:
>   SELECT * FROM orders
>   WHERE user_id = 42 AND status = 'paid'
>   ORDER BY created_at DESC LIMIT 20;
> Engine: Postgres 16. It does a Seq Scan + Sort. Help.

→ Suggests:
  CREATE INDEX idx_orders_user_status_created
    ON orders (user_id, status, created_at DESC);
  Plan changes from Seq Scan + Sort to Index Scan (no sort node).
```

## Tips & gotchas

- Always run `EXPLAIN ANALYZE` against realistic data volumes — plans flip between seq scan and index scan based on table statistics, so a 100-row dev table lies.
- Run `ANALYZE` (or `ANALYZE TABLE` on MySQL) after large loads; bad row estimates cause most "why isn't my index used" cases.
- Composite index column order matters: equality predicates first, range/sort columns last.
- `EXPLAIN ANALYZE` actually executes the query — wrap writes in a transaction you roll back.

## FAQ

**Does it connect to my database?** Only if you let it run queries; otherwise paste the plan text and it analyzes statically.

**Postgres and MySQL both?** Yes — it adapts syntax and planner advice per engine.

**Can it explain an existing plan I paste?** Yes, paste raw `EXPLAIN` output and it annotates each node.
