CREATE INDEX idx_orders_created_at ON orders (created_at);Useful for range scans, sorting, and recent-record lookups.
index design, show index, explain, explain analyze, analyze table, and practical performance tuning workflows in MySQL.
Create, inspect, and reason about indexes for common query patterns.
CREATE INDEX idx_orders_created_at ON orders (created_at);Useful for range scans, sorting, and recent-record lookups.
CREATE INDEX idx_orders_user_status_created ON orders (user_id, status, created_at);Composite indexes can accelerate queries that filter by the leading columns.
SHOW INDEX FROM orders;Review cardinality, uniqueness, and column order when debugging plans.
DROP INDEX idx_orders_created_at ON orders;Dropping unused indexes can reduce write amplification and storage cost.
Understand how MySQL intends to execute a query and spot performance problems.
EXPLAIN SELECT * FROM orders WHERE user_id = 42 ORDER BY created_at DESC LIMIT 20;`EXPLAIN` is the first stop when a query is unexpectedly slow.
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 42 ORDER BY created_at DESC LIMIT 20;Great for validating whether the expected index strategy is actually helping.
ANALYZE TABLE orders;Refreshing statistics can help the optimizer make better plan choices after large data changes.
OPTIMIZE TABLE orders;Best used intentionally; its effect and cost depend on the storage engine and workload.
EXPLAIN FOR CONNECTION 12345;Helpful during live troubleshooting when you have the blocking or long-running connection id.