【MySQL】效能優化之 覆蓋索引
一個包含查詢所需的欄位的索引稱為 covering index 覆蓋索引。MySQL只需要通過索引就可以返回查詢所需要的資料,而不必在查到索引之後進行回表操作,減少IO,提供效率。
當你對一個sql 使用explain statement 檢視一個sql的執行計劃時,在EXPLAIN的Extra列出現Using Index提示時,就說明該select查詢使用了覆蓋索引。
【使用場景】
生產過程中遇到的一個例子,且先不討論 count(欄位)還是 count(*)
root@yang 06:38:34>select count(o.order_id) as cnt from `order` o WHERE o.settle_type = 2 and o.order_status = 2 and o.buyer_id=1979459339672858;
+------+
| cnt |
+------+
| 7574 |
+------+
1 row in set (0.22 sec)
root@yang 06:36:38>explain select count(o.order_id) as cnt from `order` o WHERE o.settle_type = 2 and o.order_status = 2 and o.buyer_id=1979459339672858;
+----+-------------+-------+-------------+-----------------------+-----------------------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+-----------------------+-----------------------+---------+------+------+-----------------------------------------------------+
| 1 | SIMPLE | o | index_merge | buyer_id,order_status | buyer_id,order_status | 9,1 | NULL | 3852 | Using intersect(buyer_id,order_status); Using where |
+----+-------------+-------+-------------+-----------------------+-----------------------+---------+------+------+-----------------------------------------------------+
1 row in set (0.00 sec)
上述select語句的執行計劃使用了index_merge 索引聚合,整體sql執行花費0.22s 。根據查詢條件對錶結構進行如下調整:
root@yang 06:33:14>alter table `ordert` add key ind_od_bid_st_ty_oid(`buyer_id`,`order_status`,`settle_type`,`order_id`);
Query OK, 0 rows affected (3.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
root@yang 06:38:50>explain select count(o.order_id) as cnt from `ordert` o WHERE o.settle_type = 2 and o.order_status = 2 and o.buyer_id=1979459339672858;
+----+-------------+-------+------+----------------------+----------------------+---------+-------------------+-------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+----------------------+----------------------+---------+-------------------+-------+--------------------------+
| 1 | SIMPLE | o | ref | ind_od_bid_st_ty_oid | ind_od_bid_st_ty_oid | 11 | const,const,const | 15242 | Using where; Using index |
+----+-------------+-------+------+----------------------+----------------------+---------+-------------------+-------+--------------------------+
1 row in set (0.00 sec)
執行計劃使用了 using index --覆蓋索引而且執行時間由0.22s,下降到小於0.01s。
root@yang 06:39:06>select count(o.order_id) as cnt from `ordert` o WHERE o.settle_type = 2 and o.order_status = 2 and o.buyer_id=1979459339672858;
+------+
| cnt |
+------+
| 7574 |
+------+
1 row in set (0.00 sec)
【覆蓋索引的限制】
遇到以下情況,執行計劃不會選擇覆蓋查詢:
1select選擇的欄位中含有不在索引中的欄位 ,也即索引沒有覆蓋全部的列。
2 where 條件中不能含有對索引進行like的操作。
root@odbsyunying 08:18:15>explain select count(*) as cnt from `ordert` o WHERE o.settle_type = 2
> and o.order_status = 2
> and o.buyer_id like '1979459339672858' \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: o
type: index
possible_keys: ind_od_bid_st_ty_oid
key: ind_od_bid_st_ty_oid
key_len: 19
ref: NULL
rows: 767466 ---覆蓋索引掃描
Extra: Using where; Using index
1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29990276/viewspace-1958558/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【MySQL】三、效能優化之 覆蓋索引MySql優化索引
- MySQL SQL 優化之覆蓋索引MySql優化索引
- 【MySQL】效能最佳化之 覆蓋索引MySql索引
- MySQL優化之覆蓋索引的使用MySql優化索引
- mysql索引覆蓋掃描優化MySql索引優化
- MySQL SQL優化 - 覆蓋索引(covering index)MySql優化索引Index
- MySQL 效能優化之索引優化MySql優化索引
- Mysql索引覆蓋MySql索引
- mysql覆蓋索引高效能的探究MySql索引
- MySQL效能優化之索引設計MySql優化索引
- MySQL 聚簇索引 和覆蓋索引MySql索引
- mysql覆蓋索引之看山還是山MySql索引
- MySQL SQL最佳化 - 覆蓋索引(covering index)MySql索引Index
- MySQL 的覆蓋索引與回表MySql索引
- MySQL 覆蓋索引、回表查詢MySql索引
- MySQL調優之索引優化MySql索引優化
- 技術分享 | MySQL 覆蓋索引最佳化案例一則MySql索引
- MySQL-覆蓋索引總結筆記MySql索引筆記
- MySQL優化之索引解析MySql優化索引
- MySQL-效能優化-索引和查詢優化MySql優化索引
- 「MySQL」高效能索引優化策略MySql索引優化
- MySQL 效能優化——B+Tree 索引MySql優化索引
- MySQL 效能優化之SQL優化MySql優化
- Mysql索引優化之索引的分類MySql索引優化
- Oracle效能優化之虛擬索引Oracle優化索引
- 【離散優化】覆蓋問題優化
- Mysql效能優化:如何給字串加索引?MySql優化字串索引
- mysql效能優化-慢查詢分析、優化索引和配置MySql優化索引
- 效能調優-Mysql索引資料結構詳解與索引優化MySql索引資料結構優化
- 【MySQL】效能優化之 order by (一)MySql優化
- MySQL索引優化MySql索引優化
- mysql優化索引MySql優化索引
- MySQL 效能優化之快取引數優化MySql優化快取
- MySQL優化學習筆記之索引MySql優化筆記索引
- 高效能MySQL讀書筆記---索引優化MySql筆記索引優化
- 還傻傻分不清MySQL回表查詢與索引覆蓋?MySql索引
- MSSQL優化之索引優化SQL優化索引
- Mysql索引優化(一)MySql索引優化