Execution Plan Analysis: EXPLAIN, EXPLAIN FORMAT=TREE & EXPLAIN ANALYZE in MySQL 8.0
Overview & Learning Objectives
When a query runs slowly in production, guessing at fixes is a recipe for failure. Senior data analysts use MySQL's diagnostic profiling tools: `EXPLAIN` to inspect the optimizer's planned execution path, and MySQL 8.0's game-changing `EXPLAIN ANALYZE`. While standard `EXPLAIN` only shows estimates, `EXPLAIN ANALYZE` actually executes the query, recording exact elapsed wall-clock times, actual row counts, and memory iterator costs.
- Interpret standard `EXPLAIN` tabular output (type, possible_keys, key, rows, Extra).
- Read modern hierarchical execution trees using `EXPLAIN FORMAT=TREE`.
- Deploy MySQL 8.0 `EXPLAIN ANALYZE` to compare estimated costs against actual millisecond runtimes.
Industry Standard
Standard: Production database engineering at Uber and Meta requires attaching `EXPLAIN ANALYZE` output to any pull request modifying high-frequency reporting queries.
Real-World Example: ScoopCraft Dark-Store Bottleneck Diagnosis
ScoopCraft diagnoses why a daily order rollup query takes 14 seconds, discovering a full table scan (`type: ALL`) that is resolved by a single composite index.
| EXPLAIN Column | Unoptimized Value | Optimized Value | Meaning |
|---|---|---|---|
| type | ALL (Worst) | ref / eq_ref (Best) | Full table scan replaced with index seek |
| rows | 5,000,000 (Scanned) | 12 (Targeted) | Rows scanned reduced by 99.99% |
| Extra | Using filesort; Using temporary | Using index | Eliminated disk temp tables and memory sorts |
| Actual Time | 14,200 ms | 3.8 ms | Sub-millisecond executive response |
Syntax & Core SQL Implementation
-- 1. Traditional tabular EXPLAIN
EXPLAIN
SELECT o.order_id, c.customer_name, o.order_total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= '2026-11-01';
-- 2. Modern MySQL 8.0 Hierarchical Tree Format
EXPLAIN FORMAT=TREE
SELECT o.order_id, c.customer_name, o.order_total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= '2026-11-01';
-- 3. MySQL 8.0 EXPLAIN ANALYZE (Measures actual execution time)
EXPLAIN ANALYZE
SELECT o.order_id, c.customer_name, o.order_total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= '2026-11-01';
Parameters & Arguments Breakdown
type: Join access method. Ranked best to worst: system > const > eq_ref > ref > range > index > ALL.key: The actual index chosen by the MySQL query optimizer.rows: Estimated number of rows the engine believes it must examine.EXPLAIN ANALYZE: Executes the query and outputs actual row counts, iterator loops, and startup/total time in milliseconds.
Sample Walkthrough (Q&A)
Question: What is the most dangerous value to see in the "type" column of a MySQL EXPLAIN output on a 10M row table?
SQL Pattern: type: ALL
Result: Full Table Scan: The engine must read every single data page from disk from row 1 to row 10,000,000
Explanation: On large tables, "ALL" causes extreme disk I/O bottlenecks. It must be resolved with appropriate B-Tree indexing.
Common Mistakes & Limitations
- Confusing `type: index` with good performance; `type: index` is a Full Index Scan (scanning the entire index tree from start to end), not an index seek (`ref` or `range`).
- Forgetting that `EXPLAIN ANALYZE` actually executes the query (do not run `EXPLAIN ANALYZE DELETE ...` expecting it to be a dry run!).
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Visual Explain in Workbench | Ctrl + Alt + X | Cmd + Option + X |
Pro Tips & Modern Best Practices
- In `EXPLAIN ANALYZE` output, focus on the operator with the highest difference between `actual time` and prior steps—that is your true bottleneck.
- Watch out for `Using filesort` or `Using temporary` in the `Extra` column; these indicate that MySQL had to allocate memory/disk temp tables to sort or group.