Query tuning without measurement is guessing. MySQL gives you two tools for looking inside a query: EXPLAIN describes the plan the optimizer chose, and EXPLAIN ANALYZE runs the query and reports what actually happened. Reading them well turns "this is slow" into a specific, fixable cause.
EXPLAIN and EXPLAIN ANALYZE
EXPLAIN SELECT id, amount FROM payments
WHERE wallet_id = 7 ORDER BY created_at DESC LIMIT 20;
EXPLAIN ANALYZE SELECT id, amount FROM payments
WHERE wallet_id = 7 ORDER BY created_at DESC LIMIT 20;EXPLAIN shows estimates and does not run the statement. EXPLAIN ANALYZE, available from MySQL 8.0.18, executes it and adds the real time and row counts for each step. Be careful with it on statements that change data, because they are executed.
The columns that matter
- type: the access method, from best to worst:
constandeq_ref(a single row by unique key),ref(matching rows by an index),range(an index range),index(a full index scan) andALL(a full table scan). - key: the index actually used.
NULLmeans none. - possible_keys: the indexes MySQL considered. If your index is here but not in
key, the optimizer decided it was not worth it. - rows: the estimated number of rows to examine. Smaller is better, and a large number next to a small
LIMITis a red flag. - filtered: the percentage of examined rows expected to survive the conditions.
- Extra: notes about how the query runs, covered next.
Reading the Extra column
Using index: a covering index answered the query without touching the table. Good.Using index condition: index condition pushdown filters inside the index before fetching rows. Usually good.Using where: some conditions are checked after rows are read. Fine, unless the number of examined rows is large.Using filesort: MySQL sorts rows itself instead of reading them in index order. Watch it on big results.Using temporary: a temporary table is needed, often forGROUP BYorDISTINCT. Watch it too.
What EXPLAIN ANALYZE adds
The output is a tree. Each node shows the estimated cost and rows, then actual time for the first and last row, the real rows returned and the number of loops. Compare estimated rows with actual rows. A large gap means the optimizer's statistics are stale or misleading, and running ANALYZE TABLE can refresh them. Multiply the per-loop time by the loops to find the step where time really goes, especially inside nested joins.
A tuning method
- Find the slow query with data. Use the slow query log or the Performance Schema digest tables, with a sensible
long_query_time, and pick the statements that cost the most in total, not only the slowest single run. - Look at the plan. Is it scanning many rows, sorting, or building a temporary table?
- Form one hypothesis. For example, "the filter column has no index" or "the index order does not match the sort".
- Change one thing: add or reorder an index, or rewrite the query.
- Measure again with the same data volume and compare before and after, both the plan and the real time.
- Check the side effects. A new index slows writes and takes space. Other queries might change plan.
Test with data of realistic size. On a tiny table the optimizer may prefer a full scan, and a plan that looks fine at a thousand rows can collapse at fifty million.
Takeaway
Use EXPLAIN to see the plan and EXPLAIN ANALYZE to see reality. Check type, key, rows and Extra, compare estimates with actual rows, and change one thing at a time. A good optimization story has a measured before, a clear cause, a targeted change and a measured after.
Comments