Let Claude Recommend the Right Database Indexes for Your Queries
Query optimisation and indexing are related but different problems. Rewriting a slow query helps — but adding the right index is often what makes it fast. Give Claude your actual query patterns and it will recommend exactly which indexes to add and why.
"Recommend database indexes for these read patterns:
1. SELECT * FROM orders WHERE user_id = ? AND status = 'pending'
ORDER BY created_at DESC
2. SELECT * FROM products WHERE category_id = ? AND active = 1
AND price BETWEEN ? AND ?
3. SELECT COUNT(*) FROM audit_logs WHERE model_type = ? AND model_id = ?
For each, explain whether to use a composite or single-column index,
the correct column order, and the trade-off on write performance.
Output Laravel migrations."
Composite index column order matters enormously — Claude explains the selectivity reasoning and picks the right order for each query. It also flags when an index will noticeably hurt write performance so you can make an informed trade-off rather than discover it in production.
// Generated migration
Schema::table('orders', function (Blueprint $table) {
$table->index(['user_id', 'status', 'created_at'], 'orders_user_status_created_idx');
});
For even better results, include your EXPLAIN output from a slow query log — Claude can read it and pinpoint exactly which index is missing or being ignored by the planner.
An index on the wrong column does nothing — describe your actual query patterns and let Claude pick the right ones.
Log in to leave a comment.
A PreToolUse hook can intercept test runner commands and filter output to show only failures, cutting thousands of tokens from Claude's context.
CLAUDE.md loads into every message. Move workflow-specific instructions into skills that load on demand to reduce token costs across your session.
Every event emitted while processing a single prompt shares a prompt.id UUID, letting you trace the complete chain of API calls and tool executions.