Query Optimization

Analyze EXPLAIN plans and recommend indexing strategies for SQL queries.

2|Updated Oct 15, 2025
One-click install
npx skills add https://github.com/cyperx84/claude-code-plugin-examples --skill query-optimization-cyperx84
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Query Optimization
Source: https://github.com/cyperx84/claude-code-plugin-examples/tree/main/examples/04-real-world/database-plugin/skills/query-optimization
Command: npx skills add https://github.com/cyperx84/claude-code-plugin-examples --skill query-optimization-cyperx84

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps teams improve the speed and efficiency of SQL queries by analyzing execution plans, tuning indexes, and rewriting queries for better performance.

Core Features & Use Cases

  • Query plan analysis: Interpret EXPLAIN and EXPLAIN ANALYZE outputs to identify bottlenecks (Seq Scan, Index Scan, Sort) and guide optimization.
  • Index optimization: Recommend composite, partial, and covering indexes, and advise on when to create or adjust indexes.
  • Query rewriting & patterns: Propose rewrites to avoid N+1 queries, remove SELECT * and optimize JOIN orders for smaller result sets.
  • Use Case: A production database with slow reporting queries can use this Skill to reduce latency by applying indexing and query refinements, resulting in faster response times.

Quick Start

Optimize the following query for speed: SELECT customer_id, SUM(total) FROM orders WHERE created_at > '2024-01-01' GROUP BY customer_id;

Frequently Asked Questions about Query Optimization

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

FAQPage Schema
How do I analyze an SQL execution plan to identify query bottlenecks?

To analyze an SQL execution plan, interpret EXPLAIN and EXPLAIN ANALYZE outputs to pinpoint bottlenecks like Seq Scan, Index Scan, or Sort operations. This guides query optimization by highlighting exact performance issues within the relational database.

What is the best way to optimize slow SQL reporting queries in production?

The best way to optimize slow SQL reporting queries is by applying indexing strategies and query rewriting. Analyzing execution plans allows you to refine SELECT statements and JOIN orders, reducing latency and resulting in faster response times.

How do I choose between composite and partial indexes for PostgreSQL?

Choose between composite and partial indexes by analyzing execution plans to assess query patterns. The Skill recommends composite, partial, and covering indexes based on filtered columns and WHERE clauses, advising exactly when to create or adjust indexes for optimal performance.

How can I rewrite SQL queries to avoid N+1 patterns and SELECT *?

To rewrite SQL queries and avoid N+1 patterns, propose rewrites that remove SELECT * and optimize JOIN orders for smaller result sets. This reduces unnecessary data retrieval and minimizes redundant database calls in production environments.

Does this query optimization approach support database partitioning and caching?

Yes, this query optimization approach supports relational databases in production environments and explicitly discusses partitioning and caching. It evaluates these advanced strategies alongside index selection to resolve complex performance bottlenecks.