sql-optimization-patterns

Analyze EXPLAIN query plans and apply indexing strategies to optimize SQL performance.

2|Updated Feb 6, 2026
One-click install
npx skills add https://github.com/bcastelino/agent-skills-kit --skill sql-optimization-patterns-bcastelino
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/bcastelino/agent-skills-kit/tree/main/skills/sql-optimization-patterns
Command: npx skills add https://github.com/bcastelino/agent-skills-kit --skill sql-optimization-patterns-bcastelino

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve?

This Skill helps users identify and resolve slow-running SQL queries, leading to significantly improved database performance, reduced load, and lower costs.

Core Features & Use Cases

  • Query Analysis: Understand and interpret EXPLAIN query plans.
  • Indexing Strategies: Learn to create and apply effective indexes (B-Tree, Hash, GIN, etc.).
  • Optimization Patterns: Implement techniques to avoid N+1 queries, optimize pagination, and aggregate data efficiently.
  • Use Case: A user is experiencing slow load times on their e-commerce dashboard due to inefficient product search queries. This Skill can analyze the problematic queries, suggest appropriate indexes, and refactor the SQL for faster retrieval.

Quick Start

Use the sql-optimization-patterns skill to analyze the EXPLAIN output for the following slow query: SELECT * FROM orders WHERE order_date < '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 optimize SQL queries using EXPLAIN plan analysis?

To optimize SQL queries, analyze the EXPLAIN query plan to identify execution bottlenecks, apply advanced indexing strategies like B-Tree or GIN, and refactor inefficient retrieval patterns to resolve slow database performance and reduce system load.

What is the best way to fix the N+1 query problem in database performance tuning?

Fixing the N+1 query problem involves implementing specific SQL optimization patterns that batch data retrieval instead of executing individual queries per row. This query tuning technique significantly reduces database load and improves application response times.

How do I create effective indexing strategies to improve database performance?

Create effective indexing strategies by selecting appropriate index types such as B-Tree, Hash, or GIN based on query conditions. Proper indexing accelerates data retrieval and directly resolves bottlenecks found during EXPLAIN plan analysis.

How can I optimize SQL pagination for large datasets?

Optimize SQL pagination by replacing inefficient offset-based retrieval with optimization patterns like keyset pagination. This query tuning method avoids scanning unnecessary rows and significantly improves database performance on large tables.

Do I need to understand execution plans for SQL query tuning?

Yes, understanding SQL execution plans is required to effectively debug and enhance query performance. Interpreting EXPLAIN output is necessary to identify performance bottlenecks and apply the correct indexing strategies and optimization patterns.

Why does inefficient SQL aggregation slow down database performance?

Inefficient SQL aggregation slows down database performance by causing the engine to process excessive data without proper indexing support. Query tuning resolves this by refactoring the aggregation logic and applying appropriate indexes to minimize computation overhead.