MySQL 8.4 Query Optimization 3.4 | Putting It All Together: When Requirements Change, Which Indexes Do We Keep?

10 Video Views·Nov 26, 2026

The best index for the new page: 0.0124 milliseconds. The same index on the old page: 1.84, about 18 times slower.

In Episode 3.3, one covering index, (customer_id, order_time, total_amount), served the customer order page from the index alone. Now the product team adds a status filter, and the original page must stay just as fast. We test three candidates against both pages: A, keep the existing index; B, (customer_id, status, order_time, total_amount); C, (customer_id, order_time, status, total_amount).

Every round is fair: all other indexes are INVISIBLE, and only one candidate is visible at a time. Tested on the 10-million-row orders table with customer 1 (10,116 orders, 43 of them pending) and customer 500004, a typical customer with 11 orders. All results are from a live MySQL 8.4.7 recording.

⏱️ Chapters
0:00 A new request: filter by status, keep the old page fast
0:47 Index A: 52 trips back to the table, 0.807 ms
1:32 Candidate B: 20 entries, 0.0124 ms
2:07 B on the old page: filesort over 10,116 entries, 1.84 ms
2:58 Candidate C: one index for both pages, 0.120 ms
3:42 The decision: 'delivered' needs 128 entries, and what each index costs
4:51 Checklist, and next: reading the execution plan

📌 What you'll learn
• Index A covers the original page (0.104 ms), but status is not in the index. To find 20 pending orders, MySQL read 52 entries and went back to the table 52 times: 0.807 ms.
• Index B puts both equality columns first, so MySQL jumps straight to customer 1's pending orders, already in time order. It reads 20 entries with Using index and no filesort: 0.0124 ms.
• Run the original page on index B, and time order holds only inside each status group. MySQL reads all 10,116 entries and sorts them (Using filesort): 1.84 ms, about 18 times slower than index A. For a typical customer the sort is tiny, about 0.01 ms.
• Index B contains every column of index A, yet cannot serve its query. Containing the columns is not the same as serving the query; the order decides.
• Index C keeps order_time second. The original page reads 20 entries with no sort (0.105 ms), and the filter checks status inside the index: 52 entries, 0.120 ms. About 10 times slower than B for the filter, still faster than A, and one index serves both pages.
• A surprise: 'delivered' is 92% of customer 1's orders, yet index C read 128 entries to find 20. New orders start as pending, so what matters is how many matches sit among the newest orders, not how common the status is.
• Each index has a price. B and C are about 425 MB each, against 315 MB for A (estimated from InnoDB statistics), and both must be updated on every status change.
• Checklist: list your important queries; equality columns first, then the sort or range column, then output columns only if covering is worth it; test every important query against every new index, for your biggest and typical customers; drop an old index only after another one truly serves its queries.

🧪 Lab
ddt_lab.orders, 10,000,000 rows, MySQL 8.4.7. Indexes idx_m33_cust_time_amt (A), idx_m34_cust_status_time_amt (B) and idx_m34_cust_time_status_amt (C), with idx_m31_status, idx_m32_cust, idx_m32_cust_time and idx_m32_time_cust kept INVISIBLE, compared with EXPLAIN ANALYZE.
[Paste the link to the Episode 3.4 SQL scripts here]

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

新頁面最快的索引:0.0124 毫秒。同一個索引跑原本的頁面:1.84 毫秒,慢了約 18 倍。

在 3.3,一個覆蓋索引 (customer_id, order_time, total_amount) 只看索引就能回答客戶訂單頁。現在產品團隊加了狀態篩選,原本的頁面也必須一樣快。我們用兩個頁面測試三個候選索引:A,保留現有的索引;B,(customer_id, status, order_time, total_amount);C,(customer_id, order_time, status, total_amount)。

每一輪都公平:其他索引都設成隱藏(INVISIBLE),一次只開一個候選索引。在一千萬列的 orders 表上測試,對象是 customer 1(10,116 筆訂單,其中 43 筆 pending)和一般客戶 customer 500004(11 筆訂單)。所有數據都來自 MySQL 8.4.7 的實際錄影。

重點:
・索引 A 能覆蓋原本的頁面(0.104 毫秒),但索引裡沒有 status。為了找到 20 筆 pending 訂單,MySQL 讀了 52 筆,回表 52 次:0.807 毫秒。
・索引 B 把兩個等號條件的欄位放在最前面,MySQL 直接跳到 customer 1 的 pending 訂單,而且已經照時間排好。只讀 20 筆,Using index,不用 filesort:0.0124 毫秒。
・用索引 B 跑原本的頁面,時間順序只在每個狀態群組裡成立。MySQL 要讀完 10,116 筆再排序(Using filesort):1.84 毫秒,比索引 A 慢約 18 倍。一般客戶要排序的資料很少,大約 0.01 毫秒。
・索引 B 包含索引 A 的所有欄位,卻服務不了 A 的查詢。索引裡有這些欄位,不代表就能服務這個查詢,關鍵在順序。
・索引 C 把 order_time 留在第二位。原本的頁面一樣讀 20 筆、不用排序(0.105 毫秒);篩選時在索引裡直接檢查 status:讀 52 筆,0.120 毫秒。篩選比 B 慢約 10 倍,但仍比 A 快,而且一個索引就能服務兩個頁面。
・意外的結果:'delivered' 佔 customer 1 訂單的 92%,索引 C 卻要讀 128 筆才湊到 20 筆。新訂單一開始都是 pending,所以重點不是狀態多常見,而是最新的訂單裡有多少筆符合。
・每個索引都有代價。B 和 C 各約 425 MB,A 是 315 MB(依 InnoDB 統計資料估計),而且每次狀態改變,B 和 C 都要跟著更新。
・設計清單:先列出重要的查詢;等號條件的欄位放前面,排序或範圍查詢的欄位其次,值得覆蓋時才把輸出欄位放最後;每個新索引都要用所有重要的查詢測試,大客戶和一般客戶都要測;確定有別的索引真正能服務舊索引的查詢,才把舊索引刪掉。

📚 MySQL 8.4 Indexing & Query Optimization
Module 3 · Episode 3.4
Previous: 3.3 Covering Indexes: Skip the Trip Back to the Table
Next: Reading the Execution Plan: What EXPLAIN Is Really Telling Us

上一集:3.3 覆蓋索引:省掉回到資料表的那一趟
下一集:讀懂執行計畫:EXPLAIN 真正在告訴我們什麼

💬 Subtitles: Chinese (Traditional) and English captions are available. Turn on CC to choose.
字幕:提供繁體中文與英文字幕,請點選 CC 切換。

👍 If this helped, like, subscribe, and share which index you would keep: