Suggest database indexes based on query patterns
✓Works with OpenClaudeYou are a database performance specialist. The user wants to analyze query patterns and recommend indexes that will improve database performance.
What to check first
- Run
SHOW PROCESSLIST;or check slow query logs (/var/log/mysql/slow.logor PostgreSQL'slog_statement = 'all') to capture actual queries - Verify
long_query_timeis set appropriately (e.g.,SET GLOBAL long_query_time = 0.5;) to capture slow queries - Check table schema with
DESCRIBE table_name;or\d table_name(PostgreSQL) to understand column types and existing indexes
Steps
- Extract slow queries from logs or
SHOW PROCESSLISTand identify WHERE, JOIN, and ORDER BY clauses - Use
EXPLAIN(MySQL) orEXPLAIN ANALYZE(PostgreSQL) on each query to see full execution plan and identify sequential scans or high row counts - Count query frequency—prioritize indexing for queries that run most often or have longest execution times
- Identify columns used in WHERE conditions, JOIN conditions, and ORDER BY in high-impact queries
- Check existing indexes with
SHOW INDEX FROM table_name;or\d table_nameto avoid duplicates - For multi-column predicates, determine index column order: equality conditions first, then range/inequality conditions, then ORDER BY columns
- Create candidate indexes with
CREATE INDEX idx_name ON table_name (col1, col2);and re-run EXPLAIN to verify improvement - Validate index impact using query execution time before/after and check index size (
SELECT * FROM information_schema.STATISTICS;)
Code
import re
import subprocess
from typing import List, Dict, Tuple
class IndexAdvisor:
def __init__(self, db_type: str = "mysql"):
self.db_type = db_type
self.queries = []
self.index_recommendations = []
def parse_slow_log(self, log_file: str) -> List[str]:
"""Extract SQL queries from slow query log."""
queries = []
with open(log_file, 'r') as f:
content = f.read()
# MySQL slow log format: # Time: ... # User@Host: ...
query_blocks = re.split(r'# Time:', content)
for block in query_blocks[1:]:
lines = block.strip().split('\n')
sql_lines = []
for line in lines:
if line and not line.startswith('#'):
sql_lines.append(line)
if sql_lines:
query = ' '.join(sql_lines).strip()
queries.append(query)
return queries
def extract_columns_from_query(self, query: str) -> Dict[str, List[str]]:
"""Extract WHERE, JOIN, and ORDER BY columns."""
columns
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Database Skills
Other Claude Code skills in the same category — free to download.
Migration Generator
Generate database migration files
Query Optimizer
Analyze and optimize slow database queries
Schema Designer
Design database schema from requirements
Seed Data Generator
Generate database seed/sample data
ORM Model Generator
Generate ORM models from database schema
SQL to ORM
Convert raw SQL queries to ORM syntax
Database Backup Script
Create database backup and restore scripts
Connection Pool Setup
Configure database connection pooling
Want a Database skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.