Agent skill
sql-optimization
Query tuning, indexing strategies, and EXPLAIN plan analysis.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/data-andreibesleaga-gabbe
SKILL.md
sql-optimization Skill
This skill focuses on ensuring database interactions are performant and scalable.
1. The Golden Rule: Indexing
- Primary Keys: Always indexed by default (B-Tree).
- Foreign Keys: MUST be indexed manually in most DBs (Postgres/MySQL) to avoid full table scans on joins.
- Where Clauses: Columns used in
WHERE,ORDER BY,GROUP BYneed indexes. - Composite Indexes: Order matters!
(last_name, first_name)supports lookup bylast_nameORlast_name + first_name, but NOT byfirst_namealone.
2. Analyzing Queries (EXPLAIN ANALYZE)
Always run EXPLAIN ANALYZE on suspect queries.
| Output Term | Meaning | Action |
|---|---|---|
| Seq Scan / Full Table Scan | Reading every row. | Bad (unless table is tiny). Add an index. |
| Index Scan | Reading index + looking up heap. | Good. |
| Index Only Scan | Reading only index. | Best. (Covering Index). |
| Nested Loop | Looping for joins. | Fine for small datasets. Bad for large. |
| Hash Join | Building hash table for join. | Good for large datasets. |
3. Implementation Patterns
1. Select Only What You Need
- Bad:
SELECT * FROM users - Good:
SELECT id, name FROM users - Reason: Reduces IO, network payload, and memory usage.
2. Solve the N+1 Problem
- Problem: Fetching 100 posts, then running 100 queries to get authors.
- Fix: Use
JOINor eager loading (ORM specific:include,with). - Detection: Use logs or APM query counts.
3. Pagination
- Offset/Limit:
OFFSET 1000000is slow (DB scans 1M rows then discards). - Cursor/Keyset:
WHERE id > last_seen_id LIMIT 10. O(1) performance.
4. Transactions
- Keep them short.
- Don't make external API calls inside a DB transaction (holds locks).
4. Schema Optimization
- Normalization (3NF): Reduces redundancy, ensures integrity. Good for write-heavy.
- Denormalization: Adds redundancy for read speed (e.g., storing
author_nameonpoststable). - Data Types: Use the smallest needed type.
VARCHAR(255)vsTEXT(Postgres treats similar, MySQL differs).INTvsBIGINT.JSONB(Postgres) for unstructured data, but don't overuse it.
5. Maintenance
- Run
VACUUM ANALYZE(Postgres) regularly to update planner statistics. - Monitor index usage. Remove unused indexes (they slow down writes).
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?