database-indexing
4
总安装量
4
周安装量
#51819
全站排名
安装命令
npx skills add https://github.com/1mangesh1/dev-skills-collection --skill database-indexing
Agent 安装分布
opencode
4
claude-code
4
gemini-cli
4
cursor
4
kilo
3
github-copilot
3
Skill 文档
Database Indexing & Query Optimization
Strategies for optimizing database queries through proper indexing and schema design.
Index Types
B-Tree Index
- Default for most databases (MySQL, PostgreSQL)
- Balanced tree structure
- Good for range queries and sorting
Hash Index
- O(1) lookup for equality
- Not suitable for range queries
- Fast point lookups
Full-Text Index
- Optimized for text search
- Language-specific analysis
- Used with text search queries
Spatial Index
- R-tree, Quadtree for geographic data
- Optimized for spatial queries
Composite Index
- Multiple columns in one index
- Column order matters (leftmost prefix)
Query Optimization Techniques
EXPLAIN Plans
EXPLAIN ANALYZE SELECT * FROM users WHERE id = 1;
Index Selection
- Look for WHERE clause columns
- Consider JOIN conditions
- Evaluate sorting/grouping columns
- Check cardinality (selectivity)
Avoid Common Mistakes
- Creating indexes on low-cardinality columns
- Creating unused indexes
- Over-indexing (write performance impact)
- Not analyzing index usage
Performance Tuning
- Analyze queries – Use EXPLAIN
- Identify bottlenecks – Query profiling
- Test thoroughly – Before/after metrics
- Monitor regularly – Track performance changes
- Denormalize carefully – Balance read vs write
- Archive old data – Keep active data small
- Partition tables – Handle large datasets
Schema Design
- Normalization – Reduce redundancy
- Appropriate data types – Use INT not VARCHAR for IDs
- Foreign keys – Maintain referential integrity
- Constraints – Enforce data quality
Tools & Commands
PostgreSQL:
CREATE INDEX idx_users_email ON users(email);
DROP INDEX idx_users_email;
ANALYZE;
MySQL:
EXPLAIN analyzer SELECT * FROM users WHERE email = 'test@example.com';
CREATE INDEX idx_email ON users(email);
References
- PostgreSQL Index Documentation
- MySQL Performance Tuning
- Database Query Optimization Principles
- Use the Index, Luke! (Free online book)