sql-optimization-patterns

Analyze EXPLAIN plans and optimize SQL queries with indexing strategies.

1|Updated Jun 18, 2025
One-click install
npx skills add https://github.com/knopki/dotfiles --skill sql-optimization-patterns-knopki
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/knopki/dotfiles/tree/main/home/private_dot_config/opencode/skills/sql-optimization-patterns
Command: npx skills add https://github.com/knopki/dotfiles --skill sql-optimization-patterns-knopki

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve?

This Skill addresses slow database queries and inefficient database performance by providing strategies for optimization, indexing, and query plan analysis.

Core Features & Use Cases

  • Query Plan Analysis: Understand EXPLAIN output to identify bottlenecks.
  • Indexing Strategies: Learn to create effective B-Tree, Hash, GIN, GiST, BRIN, composite, partial, expression, and covering indexes.
  • Query Optimization Patterns: Techniques to avoid SELECT *, optimize WHERE clauses, improve JOIN performance, eliminate N+1 queries, and optimize pagination.
  • Efficient Aggregation & Subqueries: Strategies for faster COUNT, GROUP BY, and subquery transformations.
  • Batch Operations: Optimize INSERT and UPDATE statements.
  • Advanced Techniques: Materialized views, partitioning, and understanding query hints.
  • Use Case: When faced with a slow-running report that queries multiple large tables, use this Skill to analyze the EXPLAIN plan, identify missing indexes, and refactor the query for significant performance gains.

Quick Start

Analyze the execution plan of the following SQL query: SELECT * FROM users WHERE email = '[email protected]';

Frequently Asked Questions about sql-optimization-patterns

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

FAQPage Schema
How do I analyze an EXPLAIN plan to fix slow SQL queries?

Analyze an EXPLAIN plan to fix slow SQL queries by identifying bottlenecks like sequential scans or nested loops. This Skill guides you through reading execution plans to pinpoint missing indexes and refactor inefficient query structures for significant performance gains.

What is the best way to resolve the N+1 query problem in databases?

The best way to resolve the N+1 query problem is by refactoring query logic to use efficient JOINs or batch loading. This Skill provides specific patterns to eliminate N+1 queries and optimize pagination for faster database response times.

When should I use partial or composite indexes for database optimization?

Use partial or composite indexes for database optimization when specific WHERE clauses or multi-column queries are slow. This Skill explains how to create effective B-Tree, GIN, GiST, and covering indexes tailored to your query patterns.

Can I optimize SQL aggregation and pagination for large tables?

Yes, you can optimize SQL aggregation and pagination for large tables by transforming subqueries and using efficient pagination techniques. This Skill covers strategies for faster COUNT, GROUP BY, and refactoring queries to avoid scanning entire datasets.

Does SQL query tuning work with materialized views and partitioning?

SQL query tuning works with materialized views and partitioning as advanced techniques to enhance database performance. This Skill provides guidance on implementing these methods alongside query hints to manage large data volumes and reduce query execution times.

Why does avoiding SELECT * improve database performance?

Avoiding SELECT * improves database performance by reducing data transfer and allowing the database to use covering indexes more effectively. This Skill teaches efficient query writing patterns to optimize WHERE clauses and minimize unnecessary I/O operations.