agentleFS
codebygarvcodebygarv/Ai-skills/skills/development/sql-query-optimizer/SKILL.md

sql-query-optimizer

Skill26 starsChanged 4 months agoMITView on GitHub

Analyzes SQL queries, execution plans, index usage, and lock contention to optimize database operations.

4 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Purpose

Analyze slow or resource-intensive SQL queries, interpret EXPLAIN / ANALYZE execution plans, and rewrite queries or recommend precise indexes to eliminate sequential scans and lock contention.

When to Use

  • A query causes high CPU, memory pressure, or connection saturation on the database.
  • Designing complex joins, aggregations, window functions, or subqueries.
  • Diagnosing deadlocks or table lock contention under concurrent writes.

What to Analyze

  1. Execution Plan: Inspect cost nodes, Seq Scans, Index Scans, Bitmap Index Scans, Nested Loops vs Hash Joins.
  2. Predicate SARGability: Identify functions on indexed columns (e.g. WHERE DATE(created_at) = ...) preventing index hits.
  3. Join & Subquery Efficiency: Convert correlated subqueries to CTEs, Window Functions, or Hash Joins.
  4. Indexing Strategy: Single-column, composite (ordering column matches query pattern), partial, or covering indexes.
  5. Pagination Mechanics: Replace large offset pagination (OFFSET 100000) with keyset/cursor pagination.

Output Format

  • Diagnosis: Why the query is slow (missing index, scan type, cardinality misestimate).
  • Optimized SQL: Clean rewritten query with explanation.
  • DDL Changes: Exact CREATE INDEX CONCURRENTLY statements needed.
  • Before vs After Metrics: Expected scan cost reduction and latency impact.

Avoid

  • Adding indexes on every column without considering write throughput degradation.
  • Omitting CONCURRENTLY on production index creation statements.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.