SQL Optimization Skill · Databases

SQL Optimization: Master Database Query Performance

Debug slow SQL queries with EXPLAIN analysis, design effective database indexes, fix N+1 problems, and apply proven query optimization patterns for PostgreSQL, MySQL, and more.

  • SQL
  • Database
  • PostgreSQL
  • Performance
  • Query Optimization

About This Skill

Debug slow queries, design strategic indexes, and apply proven optimization patterns. Master EXPLAIN analysis, fix N+1 problems, and achieve 99% performance improvements.

Optimize Your Queries in 4 Steps

  1. 1Run EXPLAIN ANALYZE on your slow query
  2. 2Identify bottlenecks (Seq Scan, Nested Loop, etc.)
  3. 3Apply the right pattern (index, rewrite, batch)
  4. 4Validate with before/after metrics

Core Capabilities

EXPLAIN Analysis

Interpret execution plans to identify Seq Scans, inefficient joins, and missing indexes that cause slow queries.

Index Design

Create composite, partial, covering, and expression indexes with optimal column ordering for maximum performance.

Query Rewriting

Transform slow queries using 5 proven patterns: subquery→JOIN, IN→EXISTS, function elimination, and more.

N+1 Resolution

Fix ORM-generated query storms with JOIN or batch query strategies to reduce database round trips.

Pagination Optimization

Replace slow OFFSET pagination with keyset pagination for O(1) performance on large datasets.

Performance Validation

Compare before/after execution times, verify correctness, and monitor performance after deployment.

SKILL.md

---
name: sql-optimization
description: Use when debugging slow SQL queries, designing database indexes, or rewriting queries for better performance. For EXPLAIN analysis, index strategy, N+1 problem resolution, and query optimization patterns.
---

# 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:
1. **Diagnose**: Identify bottlenecks with EXPLAIN analysis
2. **Optimize**: Design effective indexes and rewrite inefficient queries
3. **Validate**: Measure improvements and monitor performance

## Core Methodology

### Step 1: Analyze with EXPLAIN
Run EXPLAIN ANALYZE to see execution plan and actual timings.

### Step 2: Identify Bottlenecks
- Seq Scan = Missing index
- High rows removed = Non-selective filter
- Nested Loop on large tables = Inefficient join

### Step 3: Apply Patterns
- Index Strategies: composite, partial, covering, expression
- Query Rewrites: subquery→JOIN, IN→EXISTS, keyset pagination
- N+1 Solutions: JOIN or batch queries

Frequently Asked Questions

FAQ

Which databases does this skill support?
The optimization patterns work with PostgreSQL, MySQL, SQLite, and most SQL databases. EXPLAIN syntax may vary slightly between systems.
How do I find slow queries in production?
Enable slow query logging in your database (e.g., log_min_duration_statement in PostgreSQL), use pg_stat_statements for query statistics, or check your APM tool.
When should I use a composite index vs multiple single-column indexes?
Use composite indexes when queries filter on multiple columns together. Order matters: put equality columns first, then range, then sort. Single-column indexes are better when columns are queried independently.

Discussion

Discussion

0 comments
U

Trigger Phrases

Use these phrases to activate this skill in your AI coding assistant:

My query is slowHow to optimize SQLWhat index should I createFix N+1 queriesEXPLAIN ANALYZE helpDatabase performanceSlow paginationQuery optimization
SQL Optimization - Query Performance Tuning & Index Design