sql-optimization-patterns

Diagnose and optimize slow SQL queries using EXPLAIN analysis and indexing strategies.

1|Updated May 10, 2026
One-click install
npx skills add https://github.com/Tgoldi/claude-skills --skill sql-optimization-patterns-tgoldi
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/Tgoldi/claude-skills/tree/main/sql-optimization-patterns
Command: npx skills add https://github.com/Tgoldi/claude-skills --skill sql-optimization-patterns-tgoldi

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Slow database queries degrade application performance and increase infrastructure costs, and developers often lack a systematic approach to diagnosing and fixing them. ## Core Features & Use Cases - Query Plan Analysis: Interpret EXPLAIN and EXPLAIN ANALYZE output to identify sequential scans, inefficient joins, and costly operations. - Index Strategy Design: Create B-Tree, GIN, partial, covering, and composite indexes matched to actual query patterns. - Anti-Pattern Fixes: Eliminate N+1 queries, replace OFFSET pagination with cursor-based pagination, and rewrite correlated subqueries as joins or CTEs. - Use Case: Your API endpoint takes 3 seconds because it runs one query per user record. Use this Skill to rewrite it as a single JOIN with a supporting composite index, cutting response time to milliseconds. ## Quick Start Analyze this slow PostgreSQL query and recommend indexes and rewrites to make it faster.

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 a slow SQL query?

Start by running EXPLAIN ANALYZE on the query to see the actual execution plan. Look for sequential scans on large tables, then add indexes on filtered or joined columns and rewrite the query to avoid functions in WHERE clauses that block index usage.

How do I read PostgreSQL EXPLAIN ANALYZE output?

Focus on the scan type, cost, rows, and actual time fields. Sequential scans on large tables signal missing indexes, while large gaps between estimated and actual rows indicate stale statistics that ANALYZE can fix.

How do I fix N+1 query problems in my application?

Replace per-record queries with a single JOIN or a batch query using WHERE id IN (...). In application code, load the parent records first, then fetch all related records in one query and group them by foreign key.

When should I use a composite index vs multiple single-column indexes?

Use a composite index when queries filter or join on multiple columns together, with the most selective or equality column first. Column order matters because the index only supports leftmost-prefix lookups.

Why is OFFSET pagination slow on large tables?

OFFSET forces the database to scan and discard all skipped rows, so page 5000 reads 100,000 rows. Cursor-based pagination using WHERE created_at < last_seen_value with a matching index stays fast at any depth.

Can too many indexes hurt database performance?

Yes. Every index must be updated on INSERT, UPDATE, and DELETE, slowing writes and consuming storage. Monitor pg_stat_user_indexes for indexes with zero scans and drop unused ones.