What is the query execution plan and how does the MySQL optimizer work?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized MySQL / SQL deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
The query optimizer is MySQL's component that determines the most efficient way to execute a query. It analyzes the SQL, considers available indexes, table statistics, and join orders, then produces an execution plan — the sequence of operations to retrieve the data. MySQL uses a cost-based optimizer: it estimates the cost (I/O and CPU) of various execution strategies and chooses the one with the lowest estimated cost. The optimizer considers: which indexes to use (or whether to scan the table), join order (for queries joining multiple tables — it evaluates permutations, but caps at 61 tables for performance), index selectivity (high-cardinality indexes are preferred), table row counts (from statistics). Table statistics are crucial for the optimizer — it uses ANALYZE TABLE to update statistics. Stale statistics cause suboptimal plans. Viewing the plan: EXPLAIN (estimated), EXPLAIN ANALYZE (actual execution, MySQL 8.0+). Optimizer hints (MySQL 8.0+): SELECT /*+ INDEX(users idx_email) */ * — force a specific index; /*+ JOIN_ORDER(users, orders) */ — force join order. Optimizer switches: SET optimizer_switch = "index_merge=off";. Statistics for optimizer: SHOW INDEX FROM tablename\G — shows cardinality estimates.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last MySQL / SQL project, I used this when...' immediately makes your answer more credible and memorable.
Previous
What is deadlock in MySQL and how do you prevent it?
Next
What is the difference between optimistic locking with version numbers and timestamps?
More MySQL / SQL Questions
View all →- Advanced What is the difference between B-tree and Hash indexes in MySQL?
- Advanced What is MVCC (Multi-Version Concurrency Control) in MySQL?
- Advanced What is the InnoDB Buffer Pool and how does it work?
- Advanced What is deadlock in MySQL and how do you prevent it?
- Advanced What is the difference between optimistic locking with version numbers and timestamps?