MySQL Indexes, EXPLAIN, and Performance Cheatsheet

index design, show index, explain, explain analyze, analyze table, and practical performance tuning workflows in MySQL.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## indexes
Create a simple index
CREATE INDEX idx_orders_created_at ON orders (created_at);

# Add an index for a frequently filtered column.

Create a composite index
CREATE INDEX idx_orders_user_status_created ON orders (user_id, status, created_at);

# Index multiple columns in left-to-right order.

Inspect indexes on a table
SHOW INDEX FROM orders;

# Display index metadata for a table.

Drop an index
DROP INDEX idx_orders_created_at ON orders;

# Remove an index that is unused or harmful.

## explain and analysis
Explain a SELECT query
EXPLAIN SELECT * FROM orders WHERE user_id = 42 ORDER BY created_at DESC LIMIT 20;

# View the optimizer's execution plan.

Run EXPLAIN ANALYZE
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 42 ORDER BY created_at DESC LIMIT 20;

# Execute the query and include actual timing information.

Refresh table statistics
ANALYZE TABLE orders;

# Update optimizer statistics for a table.

Optimize a table
OPTIMIZE TABLE orders;

# Reorganize table storage and update index statistics for supported engines.

Explain a currently running connection
EXPLAIN FOR CONNECTION 12345;

# Obtain the plan for a statement running in another session.

Recommended next

No recommendations yet.