database-optimization
Scannednpx machina-cli add skill aiskillstore/marketplace/database-optimization --openclawDatabase Optimization
This skill optimizes database performance including query optimization, indexing strategies, N+1 problem resolution, and caching implementation.
When to Use This Skill
- When optimizing slow database queries
- When fixing N+1 query problems
- When designing indexes
- When implementing caching strategies
- When optimizing database migrations
- When improving database performance
What This Skill Does
- Query Optimization: Analyzes and optimizes SQL queries
- Index Design: Creates appropriate indexes
- N+1 Resolution: Fixes N+1 query problems
- Caching: Implements caching layers (Redis, Memcached)
- Migration Optimization: Optimizes database migrations
- Performance Monitoring: Sets up query performance monitoring
How to Use
Optimize Queries
Optimize this slow database query
Fix the N+1 query problem in this code
Specific Analysis
Analyze query performance and suggest indexes
Optimization Areas
Query Optimization
Techniques:
- Use EXPLAIN ANALYZE
- Optimize JOINs
- Reduce data scanned
- Use appropriate indexes
- Avoid SELECT *
Index Design
Strategies:
- Index frequently queried columns
- Composite indexes for multi-column queries
- Avoid over-indexing
- Monitor index usage
- Remove unused indexes
N+1 Problem
Pattern:
# Bad: N+1 queries
users = User.all()
for user in users:
posts = Post.where(user_id=user.id) # N queries
# Good: Single query with JOIN
users = User.all().includes(:posts) # 1 query
Examples
Example 1: Query Optimization
Input: Optimize slow user query
Output:
## Database Optimization: User Query
### Current Query
```sql
SELECT * FROM users
WHERE email = 'user@example.com';
-- Execution time: 450ms
Analysis
- Full table scan (no index on email)
- Scanning 1M+ rows
Optimization
-- Add index
CREATE INDEX idx_users_email ON users(email);
-- Optimized query
SELECT id, email, name FROM users
WHERE email = 'user@example.com';
-- Execution time: 2ms
Impact
- Query time: 450ms → 2ms (99.5% improvement)
- Index size: ~50MB
## Best Practices
### Database Optimization
1. **Measure First**: Use EXPLAIN ANALYZE
2. **Index Strategically**: Not every column needs an index
3. **Monitor**: Track slow query logs
4. **Cache**: Cache expensive queries
5. **Denormalize**: When justified by read patterns
## Reference Files
- **`references/query_patterns.md`** - Common query optimization patterns, anti-patterns, and caching strategies
## Related Use Cases
- Query optimization
- Index design
- N+1 problem resolution
- Caching implementation
- Database performance improvement
Source
git clone https://github.com/aiskillstore/marketplace/blob/main/skills/89jobrien/database-optimization/SKILL.mdView on GitHub Overview
This skill specializes in optimizing database performance through query tuning, indexing strategies, N+1 resolution, and caching. It also covers migrations and ongoing performance monitoring across PostgreSQL, MySQL, and other databases. It helps teams reduce latency and scale read workloads.
How This Skill Works
The skill analyzes SQL queries to identify bottlenecks, designs effective indexes, and implements caching layers. It employs techniques like EXPLAIN ANALYZE, JOIN optimization, and avoiding SELECT * to reduce scanned data, while addressing N+1 patterns and monitoring performance over time.
When to Use It
- When optimizing slow database queries
- When fixing N+1 query problems
- When designing indexes
- When implementing caching strategies
- When optimizing database migrations
Quick Start
- Step 1: Identify slow queries and run EXPLAIN ANALYZE to locate bottlenecks
- Step 2: Design and apply appropriate indexes; avoid SELECT * and optimize JOINs
- Step 3: Implement a caching layer (Redis or Memcached) and set up ongoing performance monitoring
Best Practices
- Measure first with EXPLAIN ANALYZE to establish baselines
- Index strategically; avoid over-indexing and remove unused indexes
- Monitor slow query logs and set up alerts
- Cache expensive queries to reduce load
- Denormalize only when justified by read patterns and performance needs
Example Use Cases
- Example 1: Query Optimization — Optimizing a slow user query by adding an index and rewriting the query
- Example 2: N+1 Resolution — Replacing N+1 queries with a single join or preloading associations
- Example 3: Index Design — Creating composite indexes for multi-column filters
- Example 4: Caching Implementation — Introducing Redis/Memcached to cache hot queries
- Example 5: Migration Optimization — Streamlining migrations to reduce locking and downtime