1 Execution Plans in Detail
TopBuilding on execution plan basics, let's analyze complex plans with multiple tables, subqueries, and derived tables.
-- EXPLAIN ANALYZE gives actual execution times (MySQL 8.0.18+)
EXPLAIN ANALYZE
SELECT c.name, SUM(o.amount) AS total
FROM customers c
JOIN orders o ON c.id = o.customer_id
WHERE o.order_date >= '2025-01-01'
GROUP BY c.name
HAVING SUM(o.amount) > 1000;EXPLAIN ANALYZE actually runs the query and shows real timing, not just estimates. Use it to compare before/after when testing optimizations.
Reading Multi-Table Plans
In a multi-table EXPLAIN, each row represents one table. The optimizer decides which table to scan first (the "driving table") based on estimated cost. Key things to check:
- Which table is scanned first? Ideally the smallest filtered result set.
- Are all JOINs using indexes? Check the
keycolumn for each row. - Are there
Using temporaryorUsing filesortin Extra? These indicate in-memory sorting or temp tables.
Key Takeaways
-
EXPLAIN ANALYZEshows actual execution times, not just estimates - In multi-table plans, check every row for index usage
- The driving table should be the one with the smallest filtered result set