sql-optimization-patterns

Diagnose PostgreSQL execution plans and apply index and query rewrite patterns.

Updated Jul 2, 2025
One-click install
npx skills add https://github.com/janpeterd/dotfiles --skill sql-optimization-patterns-janpeterd
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/janpeterd/dotfiles/tree/main/dot_agents/skills/sql-optimization-patterns
Command: npx skills add https://github.com/janpeterd/dotfiles --skill sql-optimization-patterns-janpeterd

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve?

This Skill helps you diagnose and fix slow database queries by guiding you through query-plan reading, index design, and common SQL performance anti-patterns.

Core Features & Use Cases

  • EXPLAIN-first tuning: Learn to interpret PostgreSQL EXPLAIN / EXPLAIN ANALYZE metrics to pinpoint where time is spent.
  • Index strategy recipes: Choose appropriate index types (B-Tree, GIN, GiST, BRIN), craft composite and partial indexes, and use expression/covering indexes for real speedups.
  • Optimization patterns: Apply proven fixes for N+1 query issues, inefficient pagination (OFFSET vs cursor), expensive COUNT/GROUP BY patterns, and correlated subquery rewrites, including batching and aggregation techniques.
  • Advanced performance levers: Improve performance with materialized views and partitioning, plus selective tuning techniques like join-method guidance and maintenance practices.

Quick Start

Use sql-optimization-patterns to analyze the slow query and generate the most likely index and rewrite changes based on its EXPLAIN ANALYZE output.

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 slow PostgreSQL queries using EXPLAIN ANALYZE?

To optimize slow PostgreSQL queries, use EXPLAIN ANALYZE to read execution metrics, pinpoint time bottlenecks, and apply pattern-based rewrites or new indexes to reduce runtime.

What is the best way to fix N+1 query patterns and slow pagination in SQL?

Fixing N+1 query patterns and slow pagination involves applying query rewrite patterns, using cursor pagination over OFFSET, and implementing batching or aggregation techniques to minimize database roundtrips.

How do I choose the right PostgreSQL index types for query performance tuning?

Choosing the right PostgreSQL index types requires analyzing execution plans to select B-Tree, GIN, GiST, or BRIN indexes, and crafting composite, partial, or covering indexes for targeted speedups.

Does this SQL optimization approach work for expensive COUNT and GROUP BY operations?

Yes, this SQL optimization approach works for expensive COUNT and GROUP BY operations by applying proven query rewrite patterns, leveraging materialized views, and using partitioning to reduce computation overhead.

When should I use materialized views instead of query rewrites for SQL optimization?

Use materialized views for SQL optimization when query rewrites cannot sufficiently reduce runtime for complex aggregations, allowing you to precompute and store expensive query results for faster read access.