sql-optimization-patterns

Analyze EXPLAIN output and recommend indexes and query rewrites for PostgreSQL and MySQL.

90|4|Updated Feb 22, 2026
One-click install
npx skills add https://github.com/aisa-group/skill-inject --skill sql-optimization-patterns-aisa-group
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/aisa-group/skill-inject/tree/main/data/skills/sql-optimization-patterns
Command: npx skills add https://github.com/aisa-group/skill-inject --skill sql-optimization-patterns-aisa-group

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers and DBAs identify and fix slow database queries, reduce resource consumption and latency, and eliminate common anti-patterns like N+1 queries that degrade application performance.

Core Features & Use Cases

  • EXPLAIN analysis: Interpret query execution plans to reveal sequential scans, index usage, join methods, cost estimates, and actual vs estimated row counts.
  • Indexing strategies: Recommend appropriate index types (B-Tree, GIN, GiST, BRIN), composite and functional indexes, partial and covering indexes, and guidance on when to add or remove indexes.
  • Query refactors & patterns: Provide rewrites to avoid correlated subqueries, optimize joins, switch to cursor-based pagination, batch operations, and advise on materialized views and partitioning.
  • Use Case: Diagnose a high-latency API endpoint by analyzing its EXPLAIN output, producing index and query rewrite recommendations, and outlining maintenance steps (ANALYZE, VACUUM, reindex).

Quick Start

Analyze the provided slow query and its EXPLAIN output, then recommend concrete index, rewrite, and maintenance steps to reduce execution time.

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 EXPLAIN output to fix slow PostgreSQL queries?

To analyze slow PostgreSQL queries, read EXPLAIN output to identify sequential scans, evaluate join methods, and compare estimated versus actual row counts. This reveals execution bottlenecks and guides targeted query rewrites to reduce latency.

What is the best way to eliminate N+1 query patterns in relational databases?

The best way to eliminate N+1 query patterns is by refactoring correlated subqueries into batch operations or optimized joins. This minimizes redundant database round-trips and significantly improves application throughput and resource consumption.

When should I use composite, partial, or covering indexes for SQL optimization?

Use composite, partial, or covering indexes during SQL optimization when query patterns require specific column filtering, frequent subset access, or index-only scans. Selecting the correct index type directly reduces execution time and resource consumption.

Does this SQL optimization approach work with both PostgreSQL and MySQL?

Yes, this SQL optimization approach applies to relational databases such as PostgreSQL and MySQL. It diagnoses slow queries, analyzes execution plans, and recommends indexing strategies and query rewrites compatible with both platforms.

How do I implement cursor-based pagination to improve query performance?

To improve query performance, implement cursor-based pagination by replacing offset-based queries with cursor predicates. This prevents full-table scans of offset rows, maintaining consistent latency as your dataset grows during pagination operations.

What maintenance steps are required after applying index recommendations and query rewrites?

After applying index recommendations and query rewrites, required maintenance steps include running ANALYZE to update table statistics, executing VACUUM to reclaim storage, and performing reindex operations to maintain ongoing throughput and latency improvements.