SQL Query Optimizer
Skill9 starsChanged 8 months agoMITView on GitHub
Reviews SQL queries for performance issues and rewrites them with optimized execution plans.
In this repository61 other files it gives its agents.
- Accessibility Audit HelperSkill
- Agent Context File WriterSkill
- Agent Evaluation Framework BuilderSkill
- AI Feature Spec WriterSkill
- API Documentation GeneratorSkill
- REST API Endpoint DesignerSkill
- Architecture ExplainerSkill
- Article RepurposerSkill
- Auth Flow PlannerSkill
- Blog Outline GeneratorSkill
- Breaking Change DetectorSkill
- Bug Root Cause AnalyzerSkill
- Caching Strategy AdvisorSkill
- Changelog CuratorSkill
- Code Review ChecklistSkill
- Database Schema ReviewerSkill
- Docker Compose GeneratorSkill
- Documentation Gap FinderSkill
- Edge Case DiscoverySkill
- Error Log SummarizerSkill
- FAQ GeneratorSkill
- Flaky Test AnalyzerSkill
- Form UX OptimizerSkill
- Git Commit WriterSkill
- Hallucination Risk ReviewerSkill
- Hybrid Search ArchitectSkill
- Incident Postmortem WriterSkill
- Issue to PR PlannerSkill
- Landing Page Copy ImproverSkill
- LangGraph State Machine DesignerSkill
- LLM Cost OptimizerSkill
- LLM Tracing and Observability SetupSkill
- MCP Integration AssistantSkill
- MCP Server BuilderSkill
- Migration Guide WriterSkill
- Newsletter Summary WriterSkill
- Observability ChecklistSkill
- Playwright Test BuilderSkill
- PR Description WriterSkill
- Prompt Injection Defense AuditorSkill
- Prompt RefinerSkill
- Prompt Version Control WorkflowSkill
- RAG Chunking Strategy AdvisorSkill
- RAG Workflow PlannerSkill
- React Component ReviewerSkill
- README WriterSkill
- Refactor PlannerSkill
- Release Notes GeneratorSkill
- Responsive Layout ReviewerSkill
- SDK Quickstart WriterSkill
- SEO Content OptimizerSkill
- SEO Metadata GeneratorSkill
- Social Post Thread WriterSkill
- Sprint Summary GeneratorSkill
- Stack Trace AnalyzerSkill
- Technical Blog Post WriterSkill
- Tech Debt AuditorSkill
- Troubleshooting Guide BuilderSkill
- Unit Test ImproverSkill
- Unit Test WriterSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 4 are distinctive, and 3 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.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 19
Use this when a query is running slowly in production, when you're writing a complex query for the first time and want a second opinion, or …
- Distinctive: line 39
When asked to optimize a SQL query, follow these steps:
- Very specific: line 50
Implicit type coercions: Do joins compare columns of different types (e.g., INT vs VARCHAR)?
- Very specific: line 54
Write the optimized query: Use CTEs (WITH clauses) to improve readability when the logic is complex.
- Distinctive: line 85
If the query cannot be improved, say so explicitly and explain why.
- Distinctive: line 139
Always provide the database system.
- Very specific: line 140
If you have the output of EXPLAIN ANALYZE, paste it — it gives the agent exact scan types and row estimates to work with.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 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
SQL Query Optimizer
What this skill does
This skill directs the agent to analyze a SQL query for common performance anti-patterns and rewrite it to execute more efficiently. It checks indexes, join strategies, subquery usage, N+1 patterns, unnecessary full table scans, and more — then provides a side-by-side comparison of the original and optimized query with a plain-English explanation of each change.
Use this when a query is running slowly in production, when you're writing a complex query for the first time and want a second opinion, or when you're reviewing a pull request that touches database queries.
How to use
Claude Code / Cursor / Codex
Copy this file to .agents/skills/sql-query-optimizer/SKILL.md in your project root (for Claude Code), or add the instructions below to your .cursorrules (for Cursor).
Then ask:
- "Optimize this query using the SQL Query Optimizer skill."
- "This query takes 3 seconds on 500k rows. Use the SQL Query Optimizer skill to improve it."
Provide the query, and optionally:
- The database system (PostgreSQL, MySQL, SQLite, etc.)
- Relevant table schemas and row counts
- Any existing index definitions
- The
EXPLAINorEXPLAIN ANALYZEoutput if available
The Prompt / Instructions for the Agent
When asked to optimize a SQL query, follow these steps:
- Identify the database system (PostgreSQL, MySQL, SQLite, MSSQL). Syntax and optimizer behavior differ. If not specified, ask.
Analyze for these anti-patterns in order:
- Missing indexes: Are columns in
WHERE,JOIN ON,ORDER BY, orGROUP BYclauses indexed? Flag unindexed columns on large tables. - *SELECT : Does the query select all columns when only a few are needed? Unnecessary columns increase I/O and memory.
- N+1 queries: Is this query run inside a loop in application code (indicated by a comment or context)? Suggest a single JOIN or subquery instead.
- Correlated subqueries: Does the
WHEREclause contain a subquery that references the outer query? These execute once per row. Rewrite as a JOIN or CTE. - Non-sargable predicates: Are functions applied to indexed columns in
WHERE(e.g.,WHERE YEAR(created_at) = 2024)? This prevents index use. - Implicit type coercions: Do joins compare columns of different types (e.g.,
INTvsVARCHAR)? This forces a full scan. - Unnecessary DISTINCT or ORDER BY: Is
DISTINCTused where it's not needed? IsORDER BYapplied to a subquery that is later filtered? - Large offset pagination: Does the query use
LIMIT x OFFSET ywith a large offset? Suggest keyset pagination instead.
- Missing indexes: Are columns in
- Write the optimized query. Use CTEs (
WITHclauses) to improve readability when the logic is complex. - List index recommendations separately. Format as
CREATE INDEXstatements that can be run directly. - Output format:
-- ORIGINAL QUERY
[paste original here]
-- OPTIMIZED QUERY
[rewritten query here]### What changed and why
- **Change 1:** [Explanation]
- **Change 2:** [Explanation]
### Recommended indexes
\```sql
CREATE INDEX idx_orders_user_id ON orders(user_id);
\```
### Estimated impact
[Brief note on expected improvement — e.g., "This should reduce the query from a full table scan to an index seek, likely cutting execution time by 80-95% on large tables."]- If the query cannot be improved, say so explicitly and explain why.
Example
Input to Agent:
"Optimize this PostgreSQL query. The
orderstable has 2M rows.user_idis not indexed.SELECT * FROM orders WHERE user_id = 42 AND status = 'pending' AND EXTRACT(YEAR FROM created_at) = 2025 ORDER BY created_at DESC; ```"
Output from Agent:
-- ORIGINAL QUERY
SELECT *
FROM orders
WHERE user_id = 42
AND status = 'pending'
AND EXTRACT(YEAR FROM created_at) = 2025
ORDER BY created_at DESC;
-- OPTIMIZED QUERY
SELECT id, user_id, status, amount, created_at
FROM orders
WHERE user_id = 42
AND status = 'pending'
AND created_at >= '2025-01-01'
AND created_at < '2026-01-01'
ORDER BY created_at DESC;What changed and why
- *Removed SELECT : Selected only the columns likely needed. Reduces I/O and memory overhead.
- Replaced EXTRACT(YEAR FROM created_at): The function call made the query non-sargable (the index on
created_atcould not be used). Replacing it with a range predicate allows an index seek. - user_id filter first: With the index below, PostgreSQL will use
user_idto narrow rows before applying the date range filter.
Recommended indexes
CREATE INDEX idx_orders_user_id_status_created ON orders(user_id, status, created_at DESC);Estimated impact
This changes a full sequential scan of 2M rows to an index scan targeting only rows for user_id = 42. On typical data distributions, this should reduce query time from seconds to milliseconds.
Notes
- Always provide the database system. PostgreSQL's planner differs significantly from MySQL's.
- If you have the output of
EXPLAIN ANALYZE, paste it — it gives the agent exact scan types and row estimates to work with. - The skill focuses on read query optimization. For write-heavy tables, index recommendations should be weighed against insert/update overhead.
- The skill does not run queries or access your database — all analysis is based on the query text and schema information you provide.
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.

