MySQL interview question · Most asked
How do you read EXPLAIN output in MySQL?
Summary
Look at type (access method, from best const/eq_ref/ref/range to worst ALL = full scan), key (the index chosen), rows (estimated rows examined), filtered, and Extra (Using index, Using where, Using filesort, Using temporary).
Longer Answer
type = ALLon a large table means a full table scan.indexmeans a full index scan (better, but still everything).possible_keysvskey: an index exists but was not chosen, often because of low selectivity or a type mismatch.rows x filteredestimates how many rows flow to the next join step.Using filesort: an extra sort step (not necessarily on disk); often fixed with an index matchingORDER BY.Using temporary: an internal temporary table, common withGROUP BYorDISTINCTon non-indexed columns.
EXPLAIN FORMAT=TREE shows the plan as a tree, and EXPLAIN ANALYZE (8.0.18+) actually runs the query and reports real row counts and timings per step, which reveals wrong estimates.
Key Points
type: const > eq_ref > ref > range > index > ALL.key,rows,filtered.Using filesort/Using temporaryare warning signs.EXPLAIN ANALYZEshows actual timings.
Explanation
What interviewers look for
The ability to turn EXPLAIN output into an action (which index to add, which query to rewrite).
Follow-up questions
- What does
Using index conditionmean?
More most-asked MySQL questions
- Why is OFFSET pagination slow on large tables, and what is keyset pagination?
- Why might MySQL not use an index you created?
- What is a clustered index in InnoDB, and why does primary key choice matter so much?
- What are the transaction isolation levels, and which does InnoDB use by default?
- How does a composite index work, and what is the leftmost-prefix rule?
- What causes deadlocks in MySQL, and how do you handle them?
- What are gap locks and next-key locks, and why do they surprise developers?
- How does MySQL replication work? Explain the binary log, asynchronous and semi-synchronous replication, and GTIDs.