sql-optimization

Analyze and optimize SQL queries using EXPLAIN/ANALYZE outputs and index design.

53|1|Updated Dec 18, 2025
One-click install
npx skills add https://github.com/cosmix/claude-code-setup --skill sql-optimization
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization
Source: https://github.com/cosmix/claude-code-setup/tree/main/skills/sql-optimization
Command: npx skills add https://github.com/cosmix/claude-code-setup --skill sql-optimization

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill focuses on analyzing slow queries, indexing, and rewriting SQL to improve performance.

Core Features & Use Cases

  • Explain & Analyze: EXPLAIN ANALYZE for bottleneck detection.
  • Index Design: Composite and partial indexes for common queries.
  • Query Rewriting: Rewriting joins, subqueries, and pagination.

Quick Start

Optimize a slow order history query by adding a composite index and rewriting a join.

Frequently Asked Questions about sql-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I identify and fix slow SQL queries?

Slow SQL queries can be identified and fixed by running EXPLAIN ANALYZE to examine execution plans, then optimizing through index design, query rewriting, and join order improvements. This reveals bottlenecks like full table scans and suboptimal cardinality, allowing you to add composite indexes or rewrite joins to reduce I/O and improve performance across production, staging, and development environments.

What's the best way to design indexes for query optimization?

Index design for query optimization involves creating composite and partial indexes aligned with your most frequent queries. Composite indexes combine multiple columns to cover query filters and joins, while partial indexes target specific row subsets. Use EXPLAIN ANALYZE output to validate that indexes are actually used and reduce the number of rows scanned.

How do I read and interpret EXPLAIN ANALYZE output?

EXPLAIN ANALYZE output shows the execution plan for a query, revealing which operations consume the most time and rows. Key metrics include row counts, I/O cost estimates, and actual execution time. Understanding these metrics lets you spot full table scans, inefficient joins, and missing indexes that can be corrected through reindexing or query rewriting.

When should I rewrite a query versus adding an index?

Rewrite queries when joins are poorly ordered, subqueries cause unnecessary rescans, or pagination is inefficient—changes that reduce cardinality or eliminate redundant operations. Add or modify indexes when queries perform full table scans or use inefficient join paths. Often both are needed: rewriting reduces the work scope, and indexes accelerate the revised plan.

Can I optimize queries across production, staging, and development?

Yes, this Skill applies query optimization to production, staging, and development environments. Validate optimizations with representative data before deployment to production, then monitor performance after deployment to confirm improvements and catch regressions. Use EXPLAIN ANALYZE in each environment to account for data volume differences.

What precautions should I take when deploying SQL optimizations?

Test optimizations on representative data in staging before production deployment to prevent query plan regression. Validate that added indexes and rewritten queries improve performance measurably. Monitor query execution after deployment to confirm gains hold under real load and catch unexpected plan changes or performance regressions.