sql-optimization-patterns

Analyze SQL query execution plans and apply indexing strategies to reduce latency.

1|Updated Aug 13, 2022
One-click install
npx skills add https://github.com/sskim91/dotfiles --skill sql-optimization-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/sskim91/dotfiles/tree/main/claude/skills/sql-optimization-patterns
Command: npx skills add https://github.com/sskim91/dotfiles --skill sql-optimization-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) and assets (resource) and scripts (resource) components.

What problem does it solve?

This Skill addresses the critical issue of slow database queries and inefficient schema design, which can cripple application performance and increase operational costs. It provides systematic approaches to identify and resolve performance bottlenecks.

Core Features & Use Cases

  • EXPLAIN Analysis: Learn to interpret query execution plans to pinpoint performance issues.
  • Indexing Strategies: Implement B-Tree, Composite, Partial, and Covering indexes for faster data retrieval.
  • N+1 Query Elimination: Transform inefficient data fetching into optimized JOINs or batch operations.
  • Use Case: You've identified a report that takes minutes to load due to a slow SQL query. Use this Skill to analyze the query's EXPLAIN plan, suggest appropriate indexes, and rewrite the query for sub-second response times.

Quick Start

Analyze the following SQL query using EXPLAIN and suggest optimization strategies, including indexing: SELECT * FROM orders WHERE customer_id = 123 AND created_at > '2023-01-01';

Frequently Asked Questions about sql-optimization-patterns

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 are identified using EXPLAIN or EXPLAIN ANALYZE to inspect execution plans and locate performance bottlenecks. Fix them by removing SELECT *, adding appropriate indexes, rewriting inefficient JOINs, and applying optimization patterns like covering indexes or cursor-based pagination to reduce execution cost and latency.

What indexing strategies improve database query performance?

Indexing strategies include B-Tree indexes for general lookups, composite indexes for multi-column filters, partial indexes to narrow scope, and covering indexes that store all required columns. Selecting the right index type based on your query patterns eliminates full table scans and accelerates data retrieval.

How do I optimize JOIN operations and eliminate N+1 queries?

Optimize JOINs by replacing inefficient multiple queries with single batched or joined operations. Eliminate N+1 query patterns—where one query triggers many dependent queries—by restructuring data fetching into optimized JOINS or batch operations, reducing database roundtrips and total execution time.

When should I use partial and expression indexes?

Partial indexes filter rows at index build time, reducing size and improving performance for queries with WHERE conditions. Expression indexes index computed columns or function results directly. Use them when queries consistently filter on specific subsets or computed values to avoid full index scans.

What does EXPLAIN output tell me about query performance?

EXPLAIN output shows the query execution plan, including operation sequence, row counts, and cost estimates. EXPLAIN ANALYZE adds actual runtime statistics. Interpreting this plan reveals full table scans, missing indexes, and join inefficiencies—the root causes of slow queries you can then eliminate.

How does cursor-based pagination improve performance on large datasets?

Cursor-based pagination uses indexed column values as pointers rather than OFFSET, which scans discarded rows. This approach scales efficiently across growing datasets by fetching only necessary rows, reducing memory consumption and query latency compared to offset-based pagination.