# sql explain plan doctor

> Paste a PostgreSQL or MySQL EXPLAIN plan; get the slow node, ranked fixes, schema-checked CREATE INDEX and before/after proof.

sql explain plan doctor is a paid AI agent skill on Agensi by Big t, in Data & Databases. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: $5 (one-time purchase)
- Creator: [Big t](https://www.agensi.io/creators/t-b)
- Category: [Data & Databases](https://www.agensi.io/skills/data-databases)
- Version: 1.0
- Last updated: 2026-10-18
- Security: scan passed (2026-10-18); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/sql-explain-plan-doctor
- Tags: database, api, devops, sql, postgresql, mysql, explain, query-optimization

## About this skill

**Find out why the query is slow and which index fixes it, then prove the fix.**

Reads PostgreSQL `EXPLAIN (ANALYZE, BUFFERS)` text or `FORMAT JSON`, and MySQL 8 `EXPLAIN ANALYZE` tree or `EXPLAIN FORMAT=JSON`. It computes **exclusive time per node**, so you see where the milliseconds actually go, then ranks findings:

- full scans that throw away most rows
- 10x+ row misestimates
- sorts spilling to disk, multi-batch hashes, lossy bitmaps
- nested loops with a scanning inner side, join-filter waste
- functions and casts on indexed columns, leading-wildcard LIKE
- filesort and temporary tables
- stale visibility maps (heap fetches), JIT overhead, cold reads

You get ready-to-review statements:
- `CREATE INDEX CONCURRENTLY` or MySQL online `ALTER TABLE ... ALGORITHM=INPLACE, LOCK=NONE` (column order: equality first, then ORDER BY with LIMIT)
- expression and pg_trgm indexes
- `CREATE STATISTICS`
- session `work_mem`

Each is **checked against your existing indexes** (from schema DDL) so it never suggests one you already have, and redundant prefix indexes are flagged. Query lint catches OFFSET paging, NOT IN, SELECT * and OR. `--after` compares runtime, buffers, seq scans and result size to prove the fix.

### What's included
SKILL.md, `scripts/sqlplan.py`, 4 sample plans (PG text before/after, PG JSON, MySQL tree, MySQL JSON) with query and schema files, sample reports, check reference, sources (PostgreSQL and MySQL 8 official docs), tests.

## Known limitations

- Heuristic advice, not the planner itself. Always verify with a new EXPLAIN ANALYZE on staging.
- Parses PostgreSQL 12+ and MySQL 8 output. MariaDB, SQL Server, Oracle and SQLite plans aren't supported.
- Suggests B-tree, expression and pg_trgm indexes. Partitioning, BRIN and JSONB/array GIN design are out of scope.
- The schema parser reads CREATE TABLE / CREATE INDEX and inline KEY definitions, not ALTER TABLE ADD INDEX.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the sql explain plan doctor skill from Agensi" and describe your task.
3. Buy it on the listing or let your agent ask you first; your agent then loads it from Agensi when it needs it. Every paid skill has a money-back guarantee: https://www.agensi.io/docs/money-back-guarantee

## Frequently asked questions

### Does it connect to my database?

No. You run EXPLAIN and save the output; the skill only reads files.

### Is EXPLAIN ANALYZE safe to run?

It executes the query. For UPDATE/DELETE wrap it in `BEGIN; ... ROLLBACK;`. The SKILL.md gives the exact commands.

### Why give it the schema?

So it skips indexes you already have, spots redundant prefix indexes and can extend an existing index instead of adding a new one.

### MySQL without timings?

`EXPLAIN FORMAT=JSON` works (estimates only: type=ALL, filtered, filesort). `EXPLAIN ANALYZE` (8.0.18+) adds real times.

### Will it run the CREATE INDEX for me?

No. Statements go to `suggested.sql` for you to review and run on staging first.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
