agentleFS
NikoxkxNikoxkx/Agent-Skills/skills/data-analytics/sql-query-optimizer/SKILL.md

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

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
  1. Distinctive:

    Look for: Seq Scan on large tables, high cost, many rows removed by filter, Nested Loop with high row counts.

    line 41
  2. Very specific:

    Use JOINs or IN subquery / lateral join instead of looping in application code.

    line 50
  3. Distinctive:

    Run EXPLAIN ANALYZE before and after.

    line 60
  4. Distinctive:

    Run the query with realistic data volume.

    line 61
  5. Distinctive:

    Check that the new index does not slow down writes unacceptably (measure insert/update time).

    line 62
  6. Distinctive:

    No increase in write latency after adding the index (measured on staging).

    line 87

“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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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).
  • EXPLAIN support (all major relational DBs).
  • For production changes: migration tool (Alembic, Flyway, Prisma, etc.).

Steps

  1. Capture the slow query:
    • From logs, APM (pg_stat_statements, slow query log), or application tracing.
    • Get the full query text with parameters.
  2. 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.
  3. 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.
  4. Fix N+1:
    • Use JOINs or IN subquery / 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.
  5. Rewrite patterns:
    • Subquery → JOIN or CTE.
    • Correlated subquery → window function or lateral.
    • SELECT * → only needed columns.
    • ORDER BY without LIMIT on large result sets → paginate or use cursor.
  6. 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).
  7. Output:
    • Original query + EXPLAIN.
    • Diagnosis.
    • Optimized query + new EXPLAIN.
    • Index creation statement (with CONCURRENTLY for 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 RECOMPILE or plan guides when needed.
  • ORM limitations: Sometimes the best fix is a raw SQL query or a database view.

Verification

  1. New EXPLAIN shows index usage (Index Scan / Index Only Scan instead of Seq Scan).
  2. Query time improves by at least 5-10x on realistic data.
  3. No increase in write latency after adding the index (measured on staging).
  4. Application tests still pass (the optimized query returns identical results).
  5. Success: The slow path is fixed, and the query plan is stable and efficient.

References

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.