Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
When a SQL query performs poorly, guessing which index to add is ineffective. To optimize queries methodically, you must inspect the Query Execution Plan (QEP) generated by the MySQL Optimizer.
MySQL provides two premier diagnostic tools:
EXPLAIN: Shows the optimizer's estimated execution plan without running the query.EXPLAIN ANALYZE (MySQL 8.0.18+): Executes the query, instrumenting actual runtimes, actual row counts, and memory loop times alongside estimates!Prepend EXPLAIN to any SELECT, INSERT, UPDATE, or DELETE statement:
You can also output the plan in structured JSON format to see detailed cost metrics:
Traditional EXPLAIN only shows estimates based on stale table statistics. EXPLAIN ANALYZE actually executes the query, printing a tree breakdown of physical operations:
cost: The optimizer's estimated cost score based on disk page reads.actual time=start..end:start: Time in milliseconds to retrieve the first row.end: Time in milliseconds to retrieve all rows for that operation.rows=X: The actual number of rows output by this step.loops=Y: How many times this operation was repeated (e.g., in a nested loop join).EXPLAIN ANALYZE executes the query, running it on an unindexed UPDATE or DELETE on a multi-million-row production table will perform the actual modification! Use it primarily on SELECT statements.Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Full-Text Search Indexes (MATCH ... AGAINST) | Interpreting EXPLAIN Output (type, key, rows, Extra) |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.