sql-optimization-patterns skill
✓ VerifiedOptimize database performance through query plan analysis, indexing strategies, and efficient SQL pattern implementation.
- GitHub stars
- 42,413
- Files
- 2
- Last commit
- Nov 9, 2026
- License
- MIT
- Allowed tools
- —
What does the sql-optimization-patterns skill do?
sql-optimization-patterns is an agent skill. Optimize database performance through query plan analysis, indexing strategies, and efficient SQL pattern implementation.
Summary generated from the skill's SKILL.md.
About the sql-optimization-patterns skill
Improving database performance and eliminating slow queries is the primary function of this skill. Developers use it to debug sluggish operations, design efficient database schemas, and reduce overall database load or costs. It is particularly useful when an agent needs to analyze execution plans or resolve N+1 query problems to improve application response times.
An agent should load this skill when tasked with interpreting PostgreSQL EXPLAIN or EXPLAIN ANALYZE outputs to identify issues like Seq Scans or inefficient join methods. It provides specific strategies for implementing various index types, including B-Tree, Hash, GIN, GiST, and BRIN. The skill also covers practical query patterns, such as avoiding SELECT * and optimizing JOIN operations by filtering data before joining tables.
Beyond query writing, the skill assists with database maintenance tasks like running ANALYZE or VACUUM commands. It helps identify common pitfalls such as over-indexing or implicit type conversion that prevents index usage. By monitoring statistics through system views, it enables the identification of missing or unused indexes to maintain scalability as datasets grow.
What an agent can do with it
- 1Analyze query execution plansExamine PostgreSQL EXPLAIN output to identify slow full table scans and estimate query costs.
- 2Implement specialized indexing strategiesCreate specific index types like GIN for JSONB data or B-Tree for range queries to speed up data retrieval.
- 3Refactor inefficient SQL patternsRewrite queries to avoid unnecessary column fetching and optimize join logic for better performance.
- 4Identify database maintenance needsFind unused indexes or missing indexes by querying system statistics to reduce write overhead and improve read speeds.
Written from the skill's SKILL.md. Every use case is checked against a quote from it.
How to install the sql-optimization-patterns skill
Each command copies the skill folder into the client's skills directory. Restart the client or start a new session to load it.Install the sql-optimization-patterns skill in Plugin
/plugin marketplace add wshobson/agents /plugin install developer-essentials@claude-code-workflows
Install the sql-optimization-patterns skill in Claude Code
~/.claude/skills/sql-optimization-patterns/SKILL.md
$ npx degit wshobson/agents/plugins/developer-essentials/skills/sql-optimization-patterns ~/.claude/skills/sql-optimization-patternsInstall the sql-optimization-patterns skill in Claude Code (project)
.claude/skills/sql-optimization-patterns/SKILL.md
$ npx degit wshobson/agents/plugins/developer-essentials/skills/sql-optimization-patterns .claude/skills/sql-optimization-patternsInstall the sql-optimization-patterns skill in Codex
~/.agents/skills/sql-optimization-patterns/SKILL.md
$ npx degit wshobson/agents/plugins/developer-essentials/skills/sql-optimization-patterns ~/.agents/skills/sql-optimization-patternsInstall the sql-optimization-patterns skill in Gemini CLI
~/.gemini/skills/sql-optimization-patterns/SKILL.md
$ npx degit wshobson/agents/plugins/developer-essentials/skills/sql-optimization-patterns ~/.gemini/skills/sql-optimization-patternsInstall the sql-optimization-patterns skill in Claude.ai
npx degit wshobson/agents/plugins/developer-essentials/skills/sql-optimization-patterns sql-optimization-patterns && zip -r sql-optimization-patterns.zip sql-optimization-patterns # claude.ai → Customize → Skills → + → Create skill → Upload a skill → sql-optimization-patterns.zip
Automated checks
Ran Nov 13, 2026Full report →Passed our automated checks. That is not a security audit: read the scripts before you let an agent run them. How checks work
- Source repository linked and reachablewshobson/agents/plugins/developer-essentials/skills/sql-optimization-patterns is public and reachablegithub.com/wshobson/agents/tree/HEAD/plugins/developer-essen ↗Pass
- Open source license (OSI)MITspdx.org/licenses/MIT.html ↗Pass
- Commit or release in the last yearLast commit 2026-11-09github.com/wshobson/agents/commits ↗Pass
- No download-and-run or credential theft in scriptsNo scripts in the skill folderPass
- No hidden instructions in SKILL.mdNo override phrases, safeguard switches, credential requests or hidden charactersgithub.com/wshobson/agents/blob/HEAD/plugins/developer-essen ↗Pass
- Pre-approved tools are scopedNo pre-approved tools; the agent asks before using onegithub.com/wshobson/agents/blob/HEAD/plugins/developer-essen ↗Pass
SKILL.md
View on GitHubSQL 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:
-- 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
-- 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 *:
-- 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:
-- 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:
-- 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;
Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices
- Index Selectively: Too many indexes slow down writes
- Monitor Query Performance: Use slow query logs
- Keep Statistics Updated: Run ANALYZE regularly
- Use Appropriate Data Types: Smaller types = better performance
- Normalize Thoughtfully: Balance normalization vs performance
- Cache Frequently Accessed Data: Use application-level caching
- Connection Pooling: Reuse database connections
- Regular Maintenance: VACUUM, ANALYZE, rebuild indexes
-- Update statistics
ANALYZE users;
ANALYZE VERBOSE orders;
-- Vacuum (PostgreSQL)
VACUUM ANALYZE users;
VACUUM FULL users; -- Reclaim space (locks table)
-- Reindex
REINDEX INDEX idx_users_email;
REINDEX TABLE users;
Common Pitfalls
- Over-Indexing: Each index slows down INSERT/UPDATE/DELETE
- Unused Indexes: Waste space and slow writes
- Missing Indexes: Slow queries, full table scans
- Implicit Type Conversion: Prevents index usage
- OR Conditions: Can't use indexes efficiently
- LIKE with Leading Wildcard:
LIKE '%abc'can't use index - Function in WHERE: Prevents index usage unless functional index exists
Monitoring Queries
-- Find slow queries (PostgreSQL)
SELECT query, calls, total_time, mean_time
FROM pg_stat_statements
ORDER BY mean_time DESC
LIMIT 10;
-- Find missing indexes (PostgreSQL)
SELECT
schemaname,
tablename,
seq_scan,
seq_tup_read,
idx_scan,
seq_tup_read / seq_scan AS avg_seq_tup_read
FROM pg_stat_user_tables
WHERE seq_scan > 0
ORDER BY seq_tup_read DESC
LIMIT 10;
-- Find unused indexes (PostgreSQL)
SELECT
schemaname,
tablename,
indexname,
idx_scan,
idx_tup_read,
idx_tup_fetch
FROM pg_stat_user_indexes
WHERE idx_scan = 0
ORDER BY pg_relation_size(indexrelid) DESC;
Files in this skill
2sql-optimization-patterns skill questions
How do I install the sql-optimization-patterns skill in Claude Code?
Run npx degit wshobson/agents/plugins/developer-essentials/skills/sql-optimization-patterns ~/.claude/skills/sql-optimization-patterns. Claude Code loads skills from ~/.claude/skills (all projects) or .claude/skills (one project) and uses a skill when its description matches the task.
Which clients can use the sql-optimization-patterns skill?
Any client that reads the Agent Skills format: Claude Code, Codex, Gemini CLI and claude.ai (upload as a ZIP). Scripts in the skill run only where the client allows code execution.
How can I analyze a query execution plan in PostgreSQL?
You can use the EXPLAIN command, specifically EXPLAIN ANALYZE for actual execution statistics or EXPLAIN (ANALYZE, BUFFERS, VERBOSE) for more detailed output.