Diagnose slow PostgreSQL, MySQL, and SQLite queries with EXPLAIN ANALYZE plan audits, composite index design, and N+1 elimination.
First skill download free (Google or GitHub sign-in) · Extra packs from $4.90, never expire.
Drop straight into .agents/skills/ or .claude/skills/ — 3 verified files ready to run, 8-point safety checked.
---
name: sql-optimization
description: "Use when debugging slow SQL queries, optimizing indexes, or rewriting inefficient database operations for better performance."
---
# SQL Optimization
Master SQL query performance through systematic analysis, strategic indexing, and proven optimization patterns.
## Overview
This skill helps you diagnose and fix slow database queries. It covers:
1. **Diagnose**: Identify bottlenecks with EXPLAIN analysis
2. **Optimize**: Design effective indexes and rewrite inefficient queries
3. **Validate**: Measure improvements and monitor performance
## When to Use This Skill
- **Slow Query Debugging**: Query taking too long, need to find the bottleneck
- **Index Design**: Creating indexes to speed up queries
- **Query Rewriting**: Transforming inefficient SQL into optimized versions
- **N+1 Problem**: ORM generating too many queries
- **Pagination Optimization**: Large offset queries slowing down
### Example Triggers
- "My query is slow, how do I debug it?"
- "What indexes should I create for this table?"
- "How do I fix N+1 queries in my ORM?"
- "What's wrong with my EXPLAIN output?"
- "How do I optimize pagination for large datasets?"
---
## Core Methodology
### Step 1: Analyze Query Performance
1. Identify slow queries from logs or monitoring
2. Run `EXPLAIN ANALYZE` to see execution plan
3. Measure actual query execution time
4. Check resource utilization (CPU, I/O, memory)
### Step 2: Understand Execution Plans
| Warning Sign | Meaning | Action |
|--------------|---------|--------|
| `Seq Scan` | Full table scan | Add appropriate index |
| `Nested Loop` on large tables | Inefficient join | Check join conditions |
| High `rows removed by filter` | Index not selective | Use more specific index |
| `Sort` with high cost | Sorting in memory | Add index with ORDER BY columns |
### Step 3: Apply Optimizations
1. Design appropriate indexes (see Index Strategies)
…
Cursor & Verify: follow bundled INSTALL.md, then run bash scripts/verify.sh inside the skill folder.
SQL
Database
PostgreSQL
Performance
Query Optimization
4-Platform Quick Install
1 Download the 3-file ZIP above (1st skill free after signing in with Google or GitHub) and unzip into .agents/skills/ (Antigravity / Codex) or .claude/skills/ (Claude Code) so SKILL.md lands at .agents/skills/sql-optimization/SKILL.md without double-nesting, or follow INSTALL.md for Cursor.
2 Run bash scripts/verify.sh from inside that skill folder to confirm every packaged file is intact.
3 Paste your slow SQL query alongside its EXPLAIN ANALYZE output and table schema so the agent diagnoses scans, join order, and missing indexes.
Example Command
Analyze this PostgreSQL EXPLAIN ANALYZE output, eliminate the sequential scan, and propose the minimal composite index.
Core Capabilities
EXPLAIN ANALYZE Execution Plan Diagnosis
Reads sequential scans, sort spills, and join costs directly from real query plans.
Composite & Covering Index Design
Orders equality-first and range-last columns to cut heap fetches without bloating writes.
N+1 ORM Query Elimination
Rewrites chatty ORM loops into batched joins and selective projections cleanly.
Keyset Pagination & Query Rewriting
Replaces slow OFFSET pagination and correlated subqueries with predictable cursor patterns.
About This Skill
SQL Optimization is a portable database-tuning skill for debugging slow PostgreSQL, MySQL, and SQLite queries inside Antigravity, Claude Code, Codex, and Cursor. Packaged as an 8-point safety-checked 3-file ZIP (SKILL.md, INSTALL.md, and scripts/verify.sh), it directs your agent through EXPLAIN ANALYZE plan reading, composite index ordering, N+1 query elimination, and keyset pagination before touching production schemas. Best for backend engineers resolving slow queries and high database CPU; skip this skill if you need NoSQL document modeling or database cluster hardware administration.
v2.0.0 (2026-12-03): Refined SKILL.md workflow, frontmatter triggers, and completion checks, plus added 4-platform INSTALL.md and read-only scripts/verify.sh (3 files total, 8-point safety checked).
SKILL.md
---
name: sql-optimization
description: "Use when debugging slow SQL queries, optimizing indexes, or rewriting inefficient database operations for better performance."
---
# SQL Optimization
Master SQL query performance through systematic analysis, strategic indexing, and proven optimization patterns.
## Overview
This skill helps you diagnose and fix slow database queries. It covers:
1. **Diagnose**: Identify bottlenecks with EXPLAIN analysis
2. **Optimize**: Design effective indexes and rewrite inefficient queries
3. **Validate**: Measure improvements and monitor performance
## When to Use This Skill
- **Slow Query Debugging**: Query taking too long, need to find the bottleneck
- **Index Design**: Creating indexes to speed up queries
- **Query Rewriting**: Transforming inefficient SQL into optimized versions
- **N+1 Problem**: ORM generating too many queries
- **Pagination Optimization**: Large offset queries slowing down
### Example Triggers
- "My query is slow, how do I debug it?"
- "What indexes should I create for this table?"
- "How do I fix N+1 queries in my ORM?"
- "What's wrong with my EXPLAIN output?"
- "How do I optimize pagination for large datasets?"
---
## Core Methodology
### Step 1: Analyze Query Performance
1. Identify slow queries from logs or monitoring
2. Run `EXPLAIN ANALYZE` to see execution plan
3. Measure actual query execution time
4. Check resource utilization (CPU, I/O, memory)
### Step 2: Understand Execution Plans
| Warning Sign | Meaning | Action |
|--------------|---------|--------|
| `Seq Scan` | Full table scan | Add appropriate index |
| `Nested Loop` on large tables | Inefficient join | Check join conditions |
| High `rows removed by filter` | Index not selective | Use more specific index |
| `Sort` with high cost | Sorting in memory | Add index with ORDER BY columns |
### Step 3: Apply Optimizations
1. Design appropriate indexes (see Index Strategies)
…
Showing the preview — download the complete 3-file ZIP above for the full SKILL.md, INSTALL.md, and scripts/verify.sh check (1st skill free after sign-in).
INSTALL.md (1.8 KB)
SKILL.md (7.2 KB)
scripts/verify.sh (2.4 KB)
Frequently Asked Questions
How do I install SQL Optimization in Antigravity, Claude Code, Codex, or Cursor?
Unzip the 3-file ZIP into .agents/skills/ for Antigravity or Codex, .claude/skills/ for Claude Code, or follow INSTALL.md for Cursor, then run bash scripts/verify.sh from inside that skill folder.
What files are included in the SQL Optimization ZIP?
The 3-file ZIP includes the complete SKILL.md query-tuning playbook, a 4-IDE INSTALL.md guide, and a read-only scripts/verify.sh installation check.
When should I use SQL Optimization—and when should I skip it?
Use it when you have slow relational queries, ORM N+1 waterfalls, offset-pagination timeouts, or unclear EXPLAIN plans in PostgreSQL, MySQL, SQL Server, or SQLite. Skip it if you are tuning MongoDB/DynamoDB or administering low-level storage replication.
How does it decide between a composite index and separate single-column indexes?
The skill instructs your agent to inspect equality-first vs range-last predicate selectivity and sort order from EXPLAIN ANALYZE, avoiding redundant single-column indexes that slow down writes without eliminating heap fetches.
Is SQL Optimization safe to run, and how does pricing work?
Yes—it is a pure instruction skill that passes our 8-point static safety audit and never runs migrations automatically without your review. Your 1st skill download is free after signing in with Google or GitHub; Starter is $4.90 (10 never-expiring credits) and Pro is $15/mo or $89/yr for all 125 skills.