skillsync

sql-explain

Explains what a SQL query does and why it may be slow, then suggests an index and a faster rewrite. Use when the user pastes a SQL query or an EXPLAIN plan and asks why it's slow.

Ask your agent

What it does

What the query returns in plain words, the likely slow part, one CREATE INDEX and a rewrite.

SKILL.md
---
name: sql-explain
description: "Explains what a SQL query does and why it may be slow, then suggests an index and a faster rewrite. Use when the user pastes a SQL query or an EXPLAIN plan and asks why it's slow."
---

# SQL explain

Explain first, then speed it up. patterns.md lists the usual causes; examples/slow-join.sql shows a
worked example.

## Steps

1. **Say in plain words which rows the query returns** ("every order from the last 30 days with the
   customer's name, newest first"). If the query looks wrong for what the user wants, say so now.
2. **Find the slow part.** If there's an `EXPLAIN (ANALYZE)` plan, read it bottom-up and point at the
   node with the most actual time or the biggest gap between estimated and actual rows. Without a plan,
   look for the patterns in patterns.md: full scans, functions on indexed columns, leading-wildcard
   `LIKE`, `OR` across columns, joins with no index on either side, deep `OFFSET`.
3. **Suggest the one index that would help most**, as a `CREATE INDEX` statement, with columns in the
   right order (equality filters first, then range, then sort). Mention `CONCURRENTLY` for production.
4. **Rewrite the query** when that's clearer or faster, and say exactly what changed and why.
5. **Say how to confirm it**: the `EXPLAIN (ANALYZE, BUFFERS)` to run before and after.

Don't suggest more than two indexes: each one slows down writes.
3 files
  • SKILL.md1.4 KB
  • examples/slow-join.sql0.2 KB
  • patterns.md0.4 KB