sql-query-optimizer
Skill1 starsChanged 5 months agoApache-2.0View on GitHub
Analyzes and optimizes slow SQL queries for PostgreSQL, MySQL, or SQLite. Use when queries are slow, or when building efficient queries from scratch.
6 other projects use a version of this.
Adopted by
Most recently taken up by
- nek1987/auto-agent-harnesstheir own version, first seen Oct 11, 2026
- CreativeActtech/llm-skillstheir own version, first seen Oct 8, 2026
- SkillMedev/data-engineering-pipelinetheir own version, first seen Oct 5, 2026
- myclaude-sh/sql-query-optimizertheir own version, first seen Aug 26, 2026
- mDevsLabs/Skillstheir own version, first seen Jul 29, 2026
- ATOM00blue/agent-skills-protheir own version, first seen May 22, 2026
Other versions6 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 6
- nek1987/auto-agent-harness★ 4same name
- CreativeActtech/llm-skills★ 2same name
- mDevsLabs/Skills★ 2same name
- ATOM00blue/agent-skills-pro★ 0same name
- myclaude-sh/sql-query-optimizer★ 0same name
- SkillMedev/data-engineering-pipeline★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository58 other files it gives its agents.
- agent-chain-designerSkill
- api-wrapper-builderSkill
- embeddings-builderSkill
- evaluation-harnessSkill
- fine-tuning-preparerSkill
- llm-router-builderSkill
- ml-model-trainerSkill
- model-benchmarkerSkill
- prompt-engineerSkill
- rag-pipeline-builderSkill
- synthetic-data-generatorSkill
- vector-db-integratorSkill
- csv-processorSkill
- database-schema-designerSkill
- data-pipeline-builderSkill
- etl-workflow-designerSkill
- json-transformerSkill
- pandas-data-cleanerSkill
- report-generatorSkill
- time-series-analyzerSkill
- visualization-generatorSkill
- backup-strategySkill
- ci-cd-optimizerSkill
- docker-compose-generatorSkill
- github-actions-pipelineSkill
- kubernetes-deploymentSkill
- load-balancer-configSkill
- log-aggregator-setupSkill
- monitoring-dashboardSkill
- nginx-config-generatorSkill
- security-hardeningSkill
- ssl-certificate-managerSkill
- terraform-module-builderSkill
- api-security-auditorSkill
- compliance-checkerSkill
- dependency-checkerSkill
- incident-responderSkill
- jwt-security-auditorSkill
- owasp-checkerSkill
- penetration-test-plannerSkill
- secret-detectorSkill
- threat-model-designerSkill
- vulnerability-scannerSkill
- accessibility-auditorSkill
- api-route-builderSkill
- auth-flow-builderSkill
- css-animation-crafterSkill
- form-validatorSkill
- graphql-schema-designerSkill
- html-component-builderSkill
- nextjs-app-scaffoldSkill
- performance-auditorSkill
- pwa-setupSkill
- react-component-generatorSkill
- responsive-layout-systemSkill
- seo-optimizerSkill
- tailwind-design-systemSkill
- websocket-integrationSkill
The file
We split this skill into the rules it gives an agent
6 rules. 0 are common, 5 are distinctive, and 1 is very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
5
Distinctive
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 41
Look for: Seq Scan on large tables, high cost, many rows removed by filter, Nested Loop with high row counts.
- Very specific: line 50
Use JOINs or IN subquery / lateral join instead of looping in application code.
- Distinctive: line 60
Run EXPLAIN ANALYZE before and after.
- Distinctive: line 61
Run the query with realistic data volume.
- Distinctive: line 62
Check that the new index does not slow down writes unacceptably (measure insert/update time).
- Distinctive: line 87
No increase in write latency after adding the index (measured on staging).
“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Overview
Analyzes slow SQL queries using EXPLAIN / EXPLAIN ANALYZE, recommends and implements index strategies (B-tree, partial, composite, covering), detects and fixes N+1 problems, optimizes JOINs, subqueries vs CTEs vs window functions, and provides concrete before/after query rewrites with performance numbers.
When to Use This Skill
- A query is slow in production or during development.
- Building new reports or features that query the database.
- Reviewing ORM-generated queries that are causing problems.
- Preparing for a database migration or scaling event.
Prerequisites
- Access to the database (read-only for analysis, or a staging copy for testing).
- The slow query (or the code that generates it).
EXPLAINsupport (all major relational DBs).- For production changes: migration tool (Alembic, Flyway, Prisma, etc.).
Steps
- Capture the slow query:
- From logs, APM (pg_stat_statements, slow query log), or application tracing.
- Get the full query text with parameters.
- Run EXPLAIN ANALYZE (or EXPLAIN on SQLite):
EXPLAIN ANALYZE SELECT ...;- Look for: Seq Scan on large tables, high cost, many rows removed by filter, Nested Loop with high row counts.
- Index strategy:
- B-tree for equality and range.
- Composite indexes in column order of the WHERE/JOIN/ORDER BY.
- Partial indexes for common filters (e.g.,
WHERE status = 'active'). - Covering indexes (INCLUDE in Postgres) to avoid table lookups.
- Fix N+1:
- Use JOINs or
INsubquery / lateral join instead of looping in application code. - For ORMs: use
select_related/prefetch_related(Django),includes/eager_load(ActiveRecord), or write a single query.
- Use JOINs or
- Rewrite patterns:
- Subquery → JOIN or CTE.
- Correlated subquery → window function or lateral.
SELECT *→ only needed columns.ORDER BYwithout LIMIT on large result sets → paginate or use cursor.
- Test the change:
- Run EXPLAIN ANALYZE before and after.
- Run the query with realistic data volume.
- Check that the new index does not slow down writes unacceptably (measure insert/update time).
- Output:
- Original query + EXPLAIN.
- Diagnosis.
- Optimized query + new EXPLAIN.
- Index creation statement (with
CONCURRENTLYfor Postgres production). - Application code change (if N+1).
- Monitoring recommendation (add to pg_stat_statements watch list).
Examples
A classic slow "get recent orders with customer and items" N+1 query, its EXPLAIN, the diagnosis, the rewritten JOIN + covering index version, and the before/after timing (e.g., 2.3s → 12ms) are included, along with similar examples for MySQL and SQLite.
Edge Cases & Error Handling
- Write-heavy tables: Partial or filtered indexes to reduce write overhead.
- Very large tables: Partitioning + query that can use partition pruning.
- Parameter sniffing (SQL Server) or plan cache issues: use
RECOMPILEor plan guides when needed. - ORM limitations: Sometimes the best fix is a raw SQL query or a database view.
Verification
- New EXPLAIN shows index usage (Index Scan / Index Only Scan instead of Seq Scan).
- Query time improves by at least 5-10x on realistic data.
- No increase in write latency after adding the index (measured on staging).
- Application tests still pass (the optimized query returns identical results).
- Success: The slow path is fixed, and the query plan is stable and efficient.
References
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.

