# Slow Query Optimizer: Plan-Proven SQL Fixes

> Reads the real query plan, fixes N+1s and unusable filters before adding indexes, and proves the speedup with identical rows.

Slow Query Optimizer: Plan-Proven SQL Fixes is a free AI agent skill on Agensi by HEMAPRIYAN RK, in Data & Databases. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [HEMAPRIYAN RK](https://www.agensi.io/creators/hemapriyan-rk)
- Category: [Data & Databases](https://www.agensi.io/skills/data-databases)
- Version: 1.0
- Last updated: 2026-12-03
- Security: scan passed (2026-10-19); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/slow-query-optimizer-plan-proven-sql-fixes
- Tags: database, sql, performance

## About this skill

### What it does

Speeds up slow SQL queries and ORM code with evidence. It runs the real query plan, finds the actual cause (N+1 queries, a missing or unusable index, non-sargable filters, OFFSET pagination), fixes it, and re-runs the same measurement. It also checks that the rewritten query returns exactly the same rows.

### Why it exists

Adding an index because a query "looks slow" is a guess, and guessed indexes slow down every write for nothing. Many slow endpoints aren't missing an index at all: they run one query per row, or wrap the indexed column in a function. The plan tells you which.

### How it works

1. Scopes to the slow query or endpoint you name, and agrees on the target time and data size.
2. Measures with EXPLAIN ANALYZE (Postgres, MySQL 8) or EXPLAIN QUERY PLAN plus timing (SQLite), and counts queries per request to catch N+1.
3. Reads the plan with a bundled guide: the slowest node, estimate vs. actual rows, full scans, spilled sorts, large OFFSETs.
4. Fixes in leverage order: query shape first, then a matching index, then statistics, then configuration. A bundled catalog covers ORM N+1 fixes (Django, SQLAlchemy, Rails, Prisma), non-sargable predicates, index design and keyset pagination.
5. Verifies with the same measurement, checks results are identical, and reports index write cost and a lock-free rollout.

### What you get

Before/after time on the agreed data, the plan evidence for the cause, the SQL or ORM change and index DDL, how results were confirmed identical, and the index's write cost and rollout plan.

### When not to use it

Database server tuning without a specific slow query, schema design from scratch (use a database-design skill), or NoSQL stores.

### See it in action

**Request:**

> Use the slow-query-optimizer skill on dashboard.py. The top-customers dashboard endpoint is too slow against our database (shop.db, about 50,000 orders and growing). Make it fast.

**Result:** 4.99 s → 4.3 ms on 50,000 orders, with identical results for every window and list size tested. Removing the N+1 alone took it to 17 ms; the covering index did the rest. The complete, unedited output, the inputs, and the before/after files are in [examples/slow-query-optimizer](https://github.com/hemapriyan-rk/claude-rigor-skills/tree/main/examples/slow-query-optimizer).

### Known limitations

- Needs to run queries (or have you run them) for real measurements; without that it gives commands, not results.
- Timing on a laptop or staging copy can differ from production; use production-sized data where possible.
- SQLite has no EXPLAIN ANALYZE, so plans there show the access path but not per-node time.
- Index size estimates may be approximate when the database doesn't expose size statistics.

### FAQ

**Will it add indexes everywhere?**
No. It fixes the query shape first and adds only an index that serves the specific slow query, and it reports the write cost of that index.

**How do I know the faster query is still correct?**
It compares old and new results with an order-independent check (EXCEPT both ways or sorted comparison). In the demo, results matched for every window and list size tested.

**Does it work with my ORM?**
Yes. It counts queries per request to find N+1 patterns and uses the bundled fixes for Django, SQLAlchemy, Rails and Prisma, or plain SQL.

### Compatibility

Works with any agent that supports the SKILL.md standard. The demo was run with Claude Code (claude-sonnet-5-5) on SQLite. Best with shell access to run the plan and timing; without it, it gives you the exact commands to run and works from the output you paste.

### Open source

Part of [claude-rigor-skills](https://github.com/hemapriyan-rk/claude-rigor-skills): 33 MIT-licensed skills that make AI show its work. Documentation: https://github.com/hemapriyan-rk/claude-rigor-skills/tree/main/skills/slow-query-optimizer

## 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 Slow Query Optimizer: Plan-Proven SQL Fixes skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### What does Slow Query Optimizer: Plan-Proven SQL Fixes do?

Reads the real query plan, fixes N+1s and unusable filters before adding indexes, and proves the speedup with identical rows.

### Which AI agents support Slow Query Optimizer: Plan-Proven SQL Fixes?

This skill uses the open SKILL.md standard. Use it through an MCP-compatible agent connected to Agensi, and check the compatibility information on this page for confirmed agents and required capabilities.

### How do I use Slow Query Optimizer: Plan-Proven SQL Fixes?

Connect your AI agent to Agensi through MCP, then ask it to use Slow Query Optimizer: Plan-Proven SQL Fixes on your task. Your agent loads the latest approved version each time. Paid skills require a purchase or active subscription. Skill files are not available for download; your licence does not allow you to save, copy or export any part of a skill.

---

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.
