debug-slow-queries
Skill9 starsChanged 10 months agoMITView on GitHub
Diagnoses and fixes slow database queries using explain plans, statistics, and targeted indexes or rewrites. Use when an endpoint is slow, a query regresses, cpu spikes, or timeouts appear.
In this repository3 other files it gives its agents.
- data-quality-auditSkill
- query-database-schemaSkill
- write-safe-migrationsSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 4 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 61
add index to match order by, or paginate differently
- Distinctive: line 132
avoid wrapping indexed columns in functions in where clauses
- Distinctive: line 136
use keyset pagination when possible
- Distinctive: line 156
consider materialized views / summary tables for heavy aggregates
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Debug Slow Queries
Quick Start
Goal: go from "slow" to a verified fix with minimal risk.
When to use this skill
- p95 latency spikes, timeouts, or sudden cpu growth tied to database load
- a deploy introduces query regression
- tables grow and plans flip (index → seq scan, join strategy changes)
inputs to collect (before changing anything)
- database engine + version
- query text + typical parameters (not placeholders only)
- runtime evidence: p50/p95, rows returned, timeouts, error logs
- table sizes / row counts (approx)
- relevant indexes + constraints
- environment: prod vs staging, read replica vs primary
Workflow (default)
- reproduce or capture
- reproduce in staging with production-like data, or capture the exact query + params from logs
- baseline
- record current p50/p95 and the explain plan
- read the plan
- identify where time is spent (scan, join, sort, aggregate)
- pick the cheapest safe fix
- rewrite query → add/adjust index → update stats → change schema
- verify
- re-run explain, compare timing, validate correctness
- roll out safely
- ship behind a flag if needed, monitor, keep rollback path
Explain plan patterns (what to look for)
scans
- sequential scan on large table
- likely missing selective predicate index, or predicate is not sargable
- index scan returning many rows
- index exists but selectivity is poor; may need different leading columns or a partial index
joins
- nested loop with huge inner iterations
- inner side needs an index on join key, or join order needs improvement
- hash join spilling
- work_mem/memory pressure or too many rows; reduce join input with earlier filters
sorts / aggregates
- sort on large result
- add index to match order by, or paginate differently
- group by on many rows
- pre-filter, pre-aggregate, or add covering index for grouping columns
Engine-specific commands
PostgreSQL
get plan + timing:
EXPLAIN (ANALYZE, BUFFERS, VERBOSE) <query>;check table stats freshness:
SELECT relname, n_live_tup, last_analyze, last_autoanalyze
FROM pg_stat_user_tables
ORDER BY n_live_tup DESC;see indexes:
SELECT tablename, indexname, indexdef
FROM pg_indexes
WHERE schemaname = 'public' AND tablename = 'your_table';safe index creation:
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_name ON your_table (col1, col2);MySQL
get plan:
EXPLAIN <query>;get runtime plan details (mysql 8):
EXPLAIN ANALYZE <query>;see indexes:
SHOW INDEX FROM your_table;SQLite
get plan:
EXPLAIN QUERY PLAN <query>;Fix toolbox (ordered by typical safety)
1) query rewrites (often best first)
- limit early
- move filters into subqueries/ctes only if they reduce rows before joins
- avoid non-sargable predicates
- avoid wrapping indexed columns in functions in where clauses
- replace
select *- reduce io and sort payload
- avoid
offsetpagination at scale- use keyset pagination when possible
2) index changes
Use this checklist:
- does the where clause filter on a selective column?
- does the join predicate have an index on the inner side?
- does order by match an index prefix?
- is a composite index needed (leading columns matter)?
- can a partial index reduce size (postgres)?
3) statistics / maintenance
- analyze / vacuum (postgres) or optimize/analyze table (mysql) can fix bad estimates
- confirm cardinality estimates vs actual rows in explain analyze
4) schema-level changes (highest cost)
- denormalize only after proving query/index fixes are insufficient
- consider materialized views / summary tables for heavy aggregates
Output format (copy/paste)
## slow query report
### symptom
- endpoint/job:
- p50/p95:
- query:
### evidence
- explain plan notes:
- row counts:
- indexes involved:
### root cause
- primary bottleneck:
- why it happens:
### fix
- change:
- risk:
- rollback:
### verification
- before:
- after:
- correctness checks:Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

