MySQL 8.4 Query Optimization 3.3 | Covering Indexes: Skip the Trip Back to the Table

3 Video Views·Nov 22, 2026  #MySQL #MySQL84 #InnoDB

One more column in the SELECT: 1.45 milliseconds, or 22.

In Episode 3.2, the index (customer_id, order_time) found a customer's 20 most recent orders in about 2 ms. But for every row, MySQL still followed the primary key back to the clustered index. This episode removes that trip with a covering index, then shows how easily the cover breaks.

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

⏱️ Chapters
0:00 The trip MySQL still makes
0:18 The primary key bookmark, and what "covering" means
0:49 order_id comes free: Using index
1:14 Add total_amount: 10,116 trips back, 22 ms
1:46 Cover it: (customer_id, order_time, total_amount)
2:13 Add status, lose the cover
2:32 Covering is not free
2:50 Recap, and next: putting it all together

📌 What you'll learn
• A secondary index entry stores its own columns plus the primary key. For any other column, MySQL follows that primary key back to the clustered index, one trip per row.
• If the index holds every column the query needs, MySQL answers from the index alone. EXPLAIN shows this as Using index.
• order_id is the primary key, so every entry of (customer_id, order_time) already carries it. Selecting order_id and order_time for customer 1 is covered for free: 1.45 ms.
• Ask for total_amount as well, and Using index disappears. MySQL goes back to the table for each of customer 1's 10,116 orders: about 22 ms, 15× slower. Summing total_amount for the first 100 customers takes 99,867 trips and 211 ms.
• Add total_amount to the end of the index, (customer_id, order_time, total_amount), and Using index is back: 2.44 ms for customer 1 (about 9× faster) and 30.6 ms for the first 100 customers (about 7× faster).
• A covering index covers one exact list of columns. Add status to the SELECT, and every row needs a trip again: back to 22.4 ms.
• Covering has a cost: wider entries mean more storage, more memory, and more work on every insert and update. SELECT * can almost never be covered, so ask only for the columns you need, and cover only your most important queries.

🧪 Lab
ddt_lab.orders, 10,000,000 rows, MySQL 8.4.7. Indexes idx_m32_cust_time (customer_id, order_time) and idx_m33_cust_time_amt (customer_id, order_time, total_amount), compared with EXPLAIN and EXPLAIN ANALYZE.
[Paste the link to the Episode 3.3 SQL scripts here]

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

SELECT 多要一個欄位:1.45 毫秒,或 22 毫秒。

在 3.2,索引 (customer_id, order_time) 大約 2 毫秒就找到某位客戶最近的 20 筆訂單。但每一筆,MySQL 還是要拿主鍵回到聚簇索引再跑一趟。這一集用覆蓋索引省掉這一趟,也示範覆蓋有多容易被破壞。

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

重點:
・次要索引的每個項目,存的是自己的欄位加上主鍵。要其他欄位時,MySQL 得拿主鍵回聚簇索引查,每一列跑一趟(回表)。
・如果索引裡已經有查詢需要的所有欄位,MySQL 只看索引就能回答。EXPLAIN 會顯示 Using index。
・order_id 是主鍵,所以 (customer_id, order_time) 的每個項目本來就帶著它。查 customer 1 的 order_id 和 order_time,不用多做什麼就被覆蓋了:1.45 毫秒。
・多要 total_amount,Using index 就消失了。customer 1 的 10,116 筆訂單每一筆都要回表:大約 22 毫秒,慢了 15 倍。前 100 位客戶加總 total_amount,要回表 99,867 次,花了 211 毫秒。
・把 total_amount 加到索引最後面,變成 (customer_id, order_time, total_amount),Using index 又回來了:customer 1 只要 2.44 毫秒(快約 9 倍),前 100 位客戶 30.6 毫秒(快約 7 倍)。
・覆蓋索引只覆蓋一組固定的欄位。SELECT 再加上 status,每一列又要回表:回到 22.4 毫秒。
・覆蓋是有代價的:項目變寬,就更佔空間和記憶體,每次新增和修改也更費工。SELECT * 幾乎不可能被覆蓋,所以只要你需要的欄位,也只替最重要的查詢做覆蓋。

📚 MySQL 8.4 Indexing & Query Optimization
Module 3 · Episode 3.3
Previous: 3.2 Composite Indexes: Why Column Order Matters
Next: 3.4 Putting It All Together: When Requirements Change, Which Indexes Do We Keep?

上一集:3.2 複合索引:欄位順序為什麼重要
下一集:3.4 綜合應用:需求改變時,哪些索引該留下?

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

👍 If this helped, like, subscribe, and share a query you would like to cover.
覺得有幫助,歡迎按讚、訂閱,也可以留言分享你想要覆蓋的查詢。

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.
歡迎分享影片連結;未經許可,請勿轉載、重製或用於商業用途。

#MySQL #MySQL84 #InnoDB