optimizing-sql
FreeNo executable scriptsNot checkedOptimize SQL query performance through EXPLAIN analysis, indexing strategies, and query rewriting for PostgreSQL, MySQL, and SQL Server. Use when debugging…
by ancoleman★ 535 GitHub starsAbout this skill
SQL Optimization
Provide tactical guidance for optimizing SQL query performance across PostgreSQL, MySQL, and SQL Server through execution plan analysis, strategic indexing, and query rewriting.
When to Use This Skill
Trigger this skill when encountering:
- Slow query performance or database timeouts
- Analyzing EXPLAIN plans or execution plans
- Determining index requirements
- Rewriting inefficient queries
- Identifying query anti-patterns (N+1, SELECT *, correlated subqueries)
- Database-specific optimization needs (PostgreSQL, MySQL, SQL Server)
Core Optimization Workflow
Step 1: Analyze Query Performance
Run execution plan analysis to identify bottlenecks:
PostgreSQL:
EXPLAIN ANALYZE SELECT * FROM users WHERE email = '[email protected]';
MySQL:
EXPLAIN FORMAT=JSON SELECT * FROM products WHERE category_id = 5;
SQL Server: Use SQL Server Management Studio: Display Estimated Execution Plan (Ctrl+L)
Key Metrics to Monitor:
- Cost: Estimated resource consumption
- Rows: Number of rows processed (estimated vs actual)
- Scan Type: Sequential scan vs index scan
- Execution Time: Actual time spent on operation
For detailed execution plan interpretation, see references/explain-guide.md.
Step 2: Identify Optimization Opportunities
Common Red Flags:
| Indicator | Problem | Solution |
|---|---|---|
| Seq Scan / Table Scan | Full table scan on large table | Add index on filter columns |
| High row count | Processing excessive rows | Add WHERE filter or index |
| Nested Loop with large outer table | Inefficient join algorithm | Index join columns |
| Correlated subquery | Subquery executes per row | Rewrite as JOIN or EXISTS |
| Sort operation on large result set | Expensive sorting | Add index matching ORDER BY |
For scan type interpretation, see references/scan-types.md.
Step 3: Apply Indexing Strategies
Index Decision Framework:
Is column used in WHERE, JOIN, ORDER BY, or GROUP BY?
├─ YES → Is column selective (many unique values)?
│ ├─ YES → Is table frequently queried?
│ │ ├─ YES → ADD INDEX
│ │ └─ NO → Consider based on query frequency
│ └─ NO (low selectivity) → Skip index
└─ NO → Skip index
Index Types by Use Case:
PostgreSQL:
- B-tree (default): General-purpose, supports <, ≤, =, ≥, >, BETWEEN, IN
- Hash: Equality comparisons only (=)
- GIN: Full-text search, JSONB, arrays
- GiST: Spatial data, geometric types
- BRIN: Very large tables with naturally ordered data
MySQL:
- B-tree (default): General-purpose index
- Full-text: Text search on VARCHAR/TEXT columns
- Spatial: Spatial data types
SQL Server:
- Clustered: Table data sorted by index (one per table)
- Non-clustered: Separate index structure (multiple allowed)
For comprehensive indexing guidance, see references/indexing-decisions.md and references/index-types.md.
Step 4: Design Composite Indexes
For queries filtering on multiple columns, use composite indexes:
Column Order Matters:
- Equality filters first (most selective)
- Additional equality filters (by selectivity)
- Range filters or ORDER BY (last)
Example:
-- Query pattern
SELECT * FROM orders
WHERE customer_id = 123 AND status = 'shipped'
ORDER BY created_at DESC
LIMIT 10;
-- Optimal composite index
CREATE INDEX idx_orders_customer_status_created
ON orders (customer_id, status, created_at DESC);
For composite index design patterns, see references/composite-indexes.md.
Step 5: Rewrite Inefficient Queries
Common Anti-Patterns to Avoid:
1. SELECT * (Over-fetching)
-- ❌ Bad: Fetches all columns
SELECT * FROM users WHERE id = 1;
-- ✅ Good: Fetch only needed columns
SELECT id, name, email FROM users WHERE id = 1;
2. N+1 Queries
-- ❌ Bad: 1 + N queries
SELECT * FROM users LIMIT 100;
-- Then in loop: SELECT * FROM posts WHERE user_id = ?;
-- ✅ Good: Single JOIN
SELECT users.*, posts.id AS post_id, posts.title
FROM users
LEFT JOIN posts ON users.id = posts.user_id;
3. Non-Sargable Queries (functions on indexed columns)
-- ❌ Bad: Function prevents index usage
SELECT * FROM orders WHERE YEAR(created_at) = 2025;
-- ✅ Good: Sargable range condition
SELECT * FROM orders
WHERE created_at >= '2025-01-01' AND created_at < '2026-01-01';
4. Correlated Subqueries
-- ❌ Bad: Subquery executes per row
SELECT name,
(SELECT COUNT(*) FROM orders WHERE orders.user_id = users.id)
FROM users;
-- ✅ Good: JOIN with GROUP BY
SELECT users.name, COUNT(orders.id) AS order_count
FROM users
LEFT JOIN orders ON users.id = orders.user_id
GROUP BY users.id, users.name;
For complete anti-pattern reference, see references/anti-patterns.md.
For efficient query patterns, see references/efficient-patterns.md.
Quick Reference Tables
Index Selection Guide
| Query Pattern | Index Type | Example | |--------
Install optimizing-sql in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
FAQ
What does the optimizing-sql skill do?
Optimize SQL query performance through EXPLAIN analysis, indexing strategies, and query rewriting for PostgreSQL, MySQL, and SQL Server. Use when debugging slow queries, analyzing execution plans, or improving database performance.
How do I install the optimizing-sql skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the optimizing-sql skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
by Anthropicdiscernment-nudge
After you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
by Anthropicdata-consolidate
Joins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
by Anthropicinspection-to-capa
Runs the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…
by Anthropic