MySQL 8.4 Query Optimization 4.2 | The Extra Column, TREE and JSON: Reading the Optimizer's Notes and Cost

6 Video Views·Dec 2, 2026

Pushdown off: 190 ms. Pushdown on: 40 ms. Same query, same plan, and Extra tells you which one you got.

In Episode 4.1 we read every column of EXPLAIN except the last one, Extra. type tells you which road MySQL takes to the rows; Extra tells you what it does along the way. This episode reads the most common Extra notes, Using index, Using where, Using index condition, Using filesort and Using temporary, then shows the same plan in TREE and JSON format, where the optimizer's cost appears.

Tested on the same 10-million-row orders table, with customer 1 and the first 100 customers (99,867 orders). All results are from a live MySQL 8.4.7 recording.

⏱️ Chapters
0:00 type vs. Extra: the column we skipped in 4.1
0:25 Using index, then Using where with filtered 33.33
1:04 Using index condition: pushdown, 190 ms → 40 ms
1:59 Using filesort and Using temporary, and how to avoid them
3:03 FORMAT=TREE: steps, rows and cost 6,861 vs 980,324
4:12 FORMAT=JSON: query_cost 6861.34 and using_filesort
4:46 Recap, and next: EXPLAIN ANALYZE

📌 What you'll learn
• type tells you how MySQL reaches the rows; Extra tells you what it does along the way. Both are in the same EXPLAIN row.
• Using index means every column comes from the index, with no trip back to the table. Asking customer 1 only for order_id, order_time and total_amount gives Using index on the covering index idx_m33_cust_time_amt.
• Using where means the server checks each full row against the rest of the WHERE clause. Adding item_count gt 3, a column in no index, gives Using where and filtered 33.33. It is only a warning sign when rows is large and filtered is small.
• Using index condition is index condition pushdown. Filtering on total_amount, the third index column, keeps key_len at 4 because order_time sits in between with no condition, but InnoDB checks total_amount inside each index entry before going back to the table.
• Turn pushdown off and the same plan says Using where. For the first 100 customers (99,867 orders), the query took 0.19 s with pushdown off and 0.04 s with it on, nearly five times faster, with the same result.
• Using filesort: ordering customer 1's orders by total_amount DESC LIMIT 10 sorts all 19,600 estimated rows to return 10, because inside customer 1 the index is sorted by order_time. Ordering by order_time instead gives Backward index scan and no sort.
• Using temporary: grouping the first 100 customers by payment_method needs an internal temporary table. Grouping by customer_id does not, because the index already delivers rows customer by customer.
• FORMAT=TREE shows the plan as steps, read from the inside out: Index lookup, then Sort, then Limit. Using filesort becomes the Sort step, and pushdown shows up as an index lookup with index condition.
• Every step carries rows and cost. All three steps of the top-10 query show cost 6,861; the full table scan from Episode 4.1 costs 980,324, more than 140 times as much. Cost is the optimizer's own unit, not milliseconds.
• FORMAT=JSON spells out the same plan as named fields: query_cost 6861.34, split under the table into read_cost 4901.34 and eval_cost 1960.00, plus using_filesort: true.
• Rows and cost are still estimates. Episode 5.1 adds the real numbers with EXPLAIN ANALYZE.

🧪 Lab
ddt_lab.orders, 10,000,000 rows, MySQL 8.4.7. Visible indexes: PRIMARY KEY (order_id), idx_m31_status (status) and idx_m33_cust_time_amt (customer_id, order_time, total_amount); the m32 and m34 indexes from Module 3 are set INVISIBLE. No new indexes. Compared with EXPLAIN in traditional, TREE and JSON format, plus one timed query with index_condition_pushdown off and on.
[Paste the link to the Episode 4.2 SQL scripts here]

──────────
中文說明

下推關閉 190 毫秒,開啟 40 毫秒。同一個查詢、同一個計畫,Extra 會告訴你是哪一種。

在 4.1,我們讀過 EXPLAIN 的每一個欄位,只差最後一欄:Extra。type 告訴你 MySQL 走哪一條路去拿資料;Extra 告訴你它在路上做了哪些事。這一集看最常見的幾種 Extra:Using index、Using where、Using index condition、Using filesort 和 Using temporary,再用 TREE 和 JSON 格式看同一個計畫,優化器的成本就在那裡出現。

一樣在一千萬列的 orders 表上測試,對象是 customer 1 和前 100 位客戶(99,867 筆訂單)。所有數據都來自 MySQL 8.4.7 的實際錄影。

重點:
・type 告訴你 MySQL 怎麼拿到資料;Extra 告訴你它在路上做了什麼。兩者都在同一列 EXPLAIN 裡。
・Using index 代表所有欄位都從索引取得,不用回表。只向 customer 1 要 order_id、order_time 和 total_amount,覆蓋索引 idx_m33_cust_time_amt 就會顯示 Using index。
・Using where 代表伺服器拿每一筆完整的列去比對 WHERE 的其他條件。加上 item_count 大於 3(這個欄位不在任何索引裡),就變成 Using where,filtered 是 33.33。只有在 rows 很大、filtered 又很小時,它才是警訊。
・Using index condition 就是索引條件下推。用索引的第三個欄位 total_amount 篩選時,因為中間的 order_time 沒有條件,key_len 仍然是 4;但 InnoDB 會在回表之前,先在索引項目裡檢查 total_amount。
・把下推關掉,同一個計畫就變成 Using where。前 100 位客戶(99,867 筆訂單):下推關閉要 0.19 秒,開啟只要 0.04 秒,快了將近 5 倍,結果完全一樣。
・Using filesort:customer 1 的訂單依 total_amount 由大到小取前 10 筆時,因為在 customer 1 裡索引是依 order_time 排的,MySQL 要把估計的 19,600 列全部排過,只為了回傳 10 筆。改成依 order_time 排序,就變成 Backward index scan,不用排序。
・Using temporary:前 100 位客戶依 payment_method 分組,需要一張內部暫存表;改成依 customer_id 分組就不需要,因為索引本來就是一位客戶接著一位客戶交出資料列。
・FORMAT=TREE 把計畫拆成步驟,從最裡面往外讀:先 Index lookup,再 Sort,最後 Limit。Using filesort 變成 Sort 這一步,下推則顯示