MySQL 8.4 Query Optimization 4.1 | Reading EXPLAIN: Columns and Access Types

EXPLAIN guessed 19,600 rows. The real number was 10,116.
Every episode so far has used EXPLAIN, but we only looked at one or two of its columns. Module 4 starts here: this episode reads the whole traditional EXPLAIN output column by column, then walks the access types from best to worst, const, eq_ref, ref, range, index and ALL.
Tested on the same 10-million-row orders table, with customer 1 (10,116 orders) and customers 1 to 3. All results are from a live MySQL 8.4.7 recording.
⏱️ Chapters
0:00 From 22 ms to 2.44 ms, and the columns we skipped
0:26 Before the query runs: EXPLAIN stops at the plan
0:52 The columns: key_len 4, ref const, rows 19,600, filtered 100.00
1:54 Access types: const, eq_ref, ref, range, and key_len 4 → 9
3:03 Scans: type index, then ALL at 9.7 million rows and 1.11 s
4:10 Estimates, not counts: 19,600 guessed vs 10,116 real
4:37 Recap, and next: the Extra column
📌 What you'll learn
• When you send a query, MySQL parses it, the optimizer chooses a plan, and the executor runs it. EXPLAIN stops after planning, so it comes back instantly even on 10 million rows, and every number in it is an estimate.
• How to read each column: table, possible_keys, key, key_len, ref, rows and filtered. For customer 1, key is the covering index idx_m33_cust_time_amt, key_len is 4 (one INT column), ref is const, rows is 19,600 and filtered is 100.00.
• type is the most important column. const means at most one row: a primary key lookup shows key PRIMARY and rows 1. eq_ref means exactly one matching row per row of the other table in a join.
• ref is an equality on a non-unique index, like customer_id = 1. range reads a range of index values, such as customers 1 to 3.
• key_len shows how much of the index is used. Add a condition on order_time, and key_len grows from 4 to 9: INT 4 bytes plus DATETIME 5 bytes, so both columns narrow the search.
• type index is a full scan of an index. Counting every order reads idx_m33_cust_time_amt from start to end (rows 9,693,132, Using index), even though possible_keys is NULL, because a secondary index is much smaller than the full rows.
• type ALL is a full table scan. Filtering on total_amount, which no index starts with, gives rows of about 9.7 million and filtered 33.33, a default guess. Running it takes 1.11 seconds and matches 5.77 million rows, about 58%, not 33%.
• rows is an estimate, not a count: 19,600 estimated against 10,116 real rows for customer 1, almost double. Here the gap does no harm, but a bad estimate can lead to a bad plan.
• Read EXPLAIN in this order: type, then key and key_len, then rows and filtered.
🧪 Lab
ddt_lab.orders, 10,000,000 rows, MySQL 8.4.7. Visible index idx_m33_cust_time_amt (customer_id, order_time, total_amount) plus the PRIMARY KEY (order_id); the other indexes from Module 3 are set INVISIBLE. Compared with EXPLAIN (traditional format), plus one timed full scan.
[Paste the link to the Episode 4.1 SQL scripts here]
──────────
中文說明
EXPLAIN 估計 19,600 列,實際是 10,116 列。
到目前為止每一集都用了 EXPLAIN,但我們只看過其中一兩個欄位。Module 4 從這裡開始:這一集把傳統格式的 EXPLAIN 輸出一欄一欄讀懂,再從最好到最差,依序看過各種存取類型:const、eq_ref、ref、range、index 和 ALL。
一樣在一千萬列的 orders 表上測試,對象是 customer 1(10,116 筆訂單)和 customer 1 到 3。所有數據都來自 MySQL 8.4.7 的實際錄影。
重點:
・送出查詢時,MySQL 先解析,優化器選出執行計畫,最後執行器才去跑。EXPLAIN 在規劃這一步就停下來,所以即使是一千萬列也瞬間回來;也因為這樣,裡面每個數字都是估計值。
・每個欄位怎麼讀:table、possible_keys、key、key_len、ref、rows、filtered。查 customer 1 時,key 是覆蓋索引 idx_m33_cust_time_amt,key_len 是 4(一個 INT 欄位),ref 是 const,rows 是 19,600,filtered 是 100.00。
・type 是最重要的欄位。const 代表最多一列:用主鍵查詢時,key 是 PRIMARY,rows 是 1。eq_ref 代表在 JOIN 裡,另一張表的每一列剛好對到一列。
・ref 是在非唯一索引上做等值查詢,例如 customer_id = 1。range 是用索引讀一段範圍的值,例如 customer 1 到 3。
・key_len 顯示索引用到多少。加上 order_time 的條件,key_len 從 4 變成 9:INT 4 個位元組加 DATETIME 5 個位元組,兩個欄位都用來縮小範圍。
・type 是 index,代表把整個索引掃一遍。計算所有訂單數量時,MySQL 把 idx_m33_cust_time_amt 從頭讀到尾(rows 9,693,132,Using index);雖然 possible_keys 是 NULL,但次要索引比完整資料列小得多,整個讀一遍比較省。
・type 是 ALL,代表全表掃描。用 total_amount 篩選,沒有任何索引以它開頭:rows 約 970 萬,filtered 是 33.33,這是預設的猜測。實際執行要 1.11 秒,找到 577 萬列,大約 58%,不是 33%。
・rows 是估計值,不是實際筆數:customer 1 估計 19,600 列,實際 10,116 列,差了將近一倍。這裡不造成影響,但估計錯了,就可能選錯計畫。
・讀 EXPLAIN 的順序:先看 type,再看 key 和 key_len,最後看 rows 和 filtered。
📚 MySQL 8.4 Indexing & Query Optimization
Module 4 · Episode 4.1
Previous: 3.4 Putting It All Together: When Requirements Change, Which Indexes Do We Keep?
Next: 4.2 The Extra Column, TREE and JSON
上一集:3.4 綜合應用:需求改變時,哪些索引該留下?
下一集:4.2 Extra 欄位、TREE 與 JSON
💬 Subtitles: Chinese (Traditional) and English captions are available. Turn on CC to choose.
字幕:提供繁體中文與英文字幕,請點選 CC 切換。
👍 If this helped, like, subscribe, and share an EXPLAIN output you would like us to read together.
覺得有幫助,歡迎按讚、訂閱,也可以留言分享你想一起看懂的 EXPLAIN 結果。
MySQL is a registered trademark of Oracle Corporation and/or its affiliates. This channel is not affiliated with or endorsed by Oracle.
This video is for educational purposes. Refer to the official MySQL documentation for production use.
© 2026 龍博士 Doctor Dragon Tech. All rights reserved.
歡迎分享影片連結;未經許可
