
Sql Optimization Patterns
Systematically speed up slow SQL, design indexes, and read EXPLAIN plans when your app’s database becomes the bottleneck.
Overview
SQL Optimization Patterns is an agent skill most often used in Build backend (also Operate errors, Ship perf) that masters EXPLAIN analysis, indexing, and query rewrites to eliminate slow SQL.
Install
npx skills add https://github.com/wshobson/agents --skill sql-optimization-patternsWhat is this skill?
- EXPLAIN, EXPLAIN ANALYZE, and verbose BUFFERS workflows for PostgreSQL plan inspection
- Interpretation guide for Seq Scan vs Index Scan vs Index Only Scan
- Indexing strategies and slow-query debugging playbooks
- N+1 query detection and remediation patterns
- Cost and scalability framing for growing datasets
Adoption & trust: 14k installs on skills.sh; 36.5k GitHub stars; 3/3 security scanners passed (skills.sh audits); trending (+100% hot-view momentum).
What problem does it solve?
Endpoints feel fast in dev but crawl in production because queries full-scan large tables, miss indexes, or multiply through N+1 ORM access.
Who is it for?
Indie SaaS builders on PostgreSQL or similar SQL stores who profile slow endpoints and want agent-guided DBA patterns without hiring a specialist.
Skip if: Greenfield projects with no database yet, or teams that only need NoSQL document modeling with no SQL layer.
When should I use this skill?
Debugging slow-running queries, designing performant database schemas, or optimizing application response times.
What do I get? / Deliverables
You get an optimization plan with concrete EXPLAIN-driven changes—indexes, rewrites, and schema tweaks—that measurably cuts query time and database load.
- Annotated EXPLAIN / EXPLAIN ANALYZE interpretation
- Recommended indexes and query rewrites
- N+1 remediation checklist for application code
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Primary shelf is Build → backend because schema choices, queries, and indexes are authored with the data layer during product construction. Backend subphase is where ORMs, migrations, and repository queries live—the natural home for EXPLAIN analysis and N+1 remediation.
Where it fits
Design indexes and join patterns while implementing the orders and users tables so new endpoints stay sub-100ms.
Run EXPLAIN ANALYZE on staging load tests before launch week to catch sequential scans on hot paths.
Respond to timeout alerts by walking plan output and adding partial indexes without a full rewrite sprint.
How it compares
Procedural optimization methodology for your repo’s queries—not a hosted APM or managed database autopilot.
Common Questions / FAQ
Who is sql-optimization-patterns for?
Solo builders shipping SaaS or APIs who write or review SQL and need structured help reading plans and fixing performance regressions.
When should I use sql-optimization-patterns?
While building backend data access, before Ship perf hardening, and during Operate when monitoring flags slow queries or rising DB cost.
Is sql-optimization-patterns safe to install?
The skill describes read-mostly diagnostics and schema changes you apply manually—review Security Audits on this page and never let an agent run destructive SQL without your review.
SKILL.md
READMESKILL.md - Sql Optimization Patterns
# SQL Optimization Patterns Transform slow database queries into lightning-fast operations through systematic optimization, proper indexing, and query plan analysis. ## When to Use This Skill - Debugging slow-running queries - Designing performant database schemas - Optimizing application response times - Reducing database load and costs - Improving scalability for growing datasets - Analyzing EXPLAIN query plans - Implementing efficient indexes - Resolving N+1 query problems ## Core Concepts ### 1. Query Execution Plans (EXPLAIN) Understanding EXPLAIN output is fundamental to optimization. **PostgreSQL EXPLAIN:** ```sql -- Basic explain EXPLAIN SELECT * FROM users WHERE email = 'user@example.com'; -- With actual execution stats EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'user@example.com'; -- Verbose output with more details EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT u.*, o.order_total FROM users u JOIN orders o ON u.id = o.user_id WHERE u.created_at > NOW() - INTERVAL '30 days'; ``` **Key Metrics to Watch:** - **Seq Scan**: Full table scan (usually slow for large tables) - **Index Scan**: Using index (good) - **Index Only Scan**: Using index without touching table (best) - **Nested Loop**: Join method (okay for small datasets) - **Hash Join**: Join method (good for larger datasets) - **Merge Join**: Join method (good for sorted data) - **Cost**: Estimated query cost (lower is better) - **Rows**: Estimated rows returned - **Actual Time**: Real execution time ### 2. Index Strategies Indexes are the most powerful optimization tool. **Index Types:** - **B-Tree**: Default, good for equality and range queries - **Hash**: Only for equality (=) comparisons - **GIN**: Full-text search, array queries, JSONB - **GiST**: Geometric data, full-text search - **BRIN**: Block Range INdex for very large tables with correlation ```sql -- Standard B-Tree index CREATE INDEX idx_users_email ON users(email); -- Composite index (order matters!) CREATE INDEX idx_orders_user_status ON orders(user_id, status); -- Partial index (index subset of rows) CREATE INDEX idx_active_users ON users(email) WHERE status = 'active'; -- Expression index CREATE INDEX idx_users_lower_email ON users(LOWER(email)); -- Covering index (include additional columns) CREATE INDEX idx_users_email_covering ON users(email) INCLUDE (name, created_at); -- Full-text search index CREATE INDEX idx_posts_search ON posts USING GIN(to_tsvector('english', title || ' ' || body)); -- JSONB index CREATE INDEX idx_metadata ON events USING GIN(metadata); ``` ### 3. Query Optimization Patterns **Avoid SELECT \*:** ```sql -- Bad: Fetches unnecessary columns SELECT * FROM users WHERE id = 123; -- Good: Fetch only what you need SELECT id, email, name FROM users WHERE id = 123; ``` **Use WHERE Clause Efficiently:** ```sql -- Bad: Function prevents index usage SELECT * FROM users WHERE LOWER(email) = 'user@example.com'; -- Good: Create functional index or use exact match CREATE INDEX idx_users_email_lower ON users(LOWER(email)); -- Then: SELECT * FROM users WHERE LOWER(email) = 'user@example.com'; -- Or store normalized data SELECT * FROM users WHERE email = 'user@example.com'; ``` **Optimize JOINs:** ```sql -- Bad: Cartesian product then filter SELECT u.name, o.total FROM users u, orders o WHERE u.id = o.user_id AND u.created_at > '2024-01-01'; -- Good: Filter before join SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id WHERE u.created_at > '2024-01-01'; -- Better: Filter both tables SELECT u.name, o.total FROM (SELECT * FROM users WHERE created_at > '2024-01-01') u JOIN orders o ON u.id = o.user_id; ``` ## Optimization Patterns ### Patter