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, inspect, and reason about indexes for common query patterns.

Create a simple index

Add an index for a frequently filtered column.

sqlANYindexcreate-index
sql
CREATE INDEX idx_orders_created_at ON orders (created_at);

Useful for range scans, sorting, and recent-record lookups.

Create a composite index

Index multiple columns in left-to-right order.

sqlANYindexcomposite-index
sql
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.

Inspect indexes on a table

Display index metadata for a table.

sqlANYshow-indexmetadata
sql
SHOW INDEX FROM orders;

Review cardinality, uniqueness, and column order when debugging plans.

Drop an index

Remove an index that is unused or harmful.

sqlANYdrop-indexcleanup
sql
DROP INDEX idx_orders_created_at ON orders;

Dropping unused indexes can reduce write amplification and storage cost.

explain and analysis

Understand how MySQL intends to execute a query and spot performance problems.

Explain a SELECT query

View the optimizer's execution plan.

sqlANYexplainoptimizer
sql
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.

Run EXPLAIN ANALYZE

Execute the query and include actual timing information.

sqlANYexplain-analyzeperformance
sql
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.

Refresh table statistics

Update optimizer statistics for a table.

sqlANYanalyze-tablestatistics
sql
ANALYZE TABLE orders;

Refreshing statistics can help the optimizer make better plan choices after large data changes.

Optimize a table

Reorganize table storage and update index statistics for supported engines.

sqlANYoptimize-tablemaintenance
sql
OPTIMIZE TABLE orders;

Best used intentionally; its effect and cost depend on the storage engine and workload.

Explain a currently running connection

Obtain the plan for a statement running in another session.

sqlANYexplainconnectiontroubleshooting
sql
EXPLAIN FOR CONNECTION 12345;

Helpful during live troubleshooting when you have the blocking or long-running connection id.

Recommended next

No recommendations yet.