不是全部類型的索引均可以成爲覆蓋索引。覆蓋索引必需要存儲索引的列,而哈希索引、空間索引和全文索引等都不存儲索引列的值,因此MySQL只能使用B-Tree索引作覆蓋索引sql
當發起一個被索引覆蓋的查詢(也叫做索引覆蓋查詢)時,在EXPLAIN的Extra列能夠看到「Using index」的信息性能
執行計劃中,type 爲ALL,表示進行了全表掃描優化
如何改進?優化措施很簡單,就是對這個查詢列創建索引。以下,spa
ALERT TABLE t1 ADD KEY(staff_id);
explain select sql_no_cache count(staff_id) from t1\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 type: index possible_keys: NULL key: staff_id key_len: 1 ref: NULL rows: 1023849
Extra: Using index
1 row in set (0.00 sec)
possible_key: NULL,說明沒有WHERE條件時查詢優化器沒法經過索引檢索數據,這裏使用了索引的另一個優勢,即從索引中獲取數據,減小了讀取的數據塊的數量。 無where條件的查詢,能夠經過索引來實現索引覆蓋查詢,但前提條件是,查詢返回的字段數足夠少,更不用說select *之類的了。畢竟,創建key length過長的索引,始終不是一件好事情。code
從時間上看,小了0.13 secblog
以下這個查詢:排序
select sql_no_cache rental_date from t1 where inventory_id<80000; … … | 2005-08-23 15:08:00 | | 2005-08-23 15:09:17 | | 2005-08-23 15:10:42 | | 2005-08-23 15:15:02 | | 2005-08-23 15:15:19 | | 2005-08-23 15:16:32 | +---------------------+ 79999 rows in set (0.13 sec)
執行計劃:索引
explain select sql_no_cache rental_date from t1 where inventory_id<80000\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 type: range possible_keys: inventory_id key: inventory_id key_len: 3 ref: NULL rows: 153734 Extra: Using index condition 1 row in set (0.00 sec)
Extra:Using index condition 表示使用的索引方式爲二級檢索,即79999個書籤值被用來進行回表查詢。可想而知,仍是會有必定的性能消耗的get
嘗試針對這個SQL創建聯合索引,以下:博客
alter table t1 add key(inventory_id,rental_date);
執行計劃:
explain select sql_no_cache rental_date from t1 where inventory_id<80000\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 type: range possible_keys: inventory_id,inventory_id_2 key: inventory_id_2 key_len: 3 ref: NULL rows: 162884 Extra: Using index 1 row in set (0.00 sec)
Extra:Using index 表示沒有會標查詢的過程,實現了索引覆蓋
以下這個查詢場景
select tid,return_date from t1 order by inventory_id limit 50000,10; +-------+---------------------+ | tid | return_date | +-------+---------------------+ | 50001 | 2005-06-17 23:04:36 | | 50002 | 2005-06-23 03:16:12 | | 50003 | 2005-06-20 22:41:03 | | 50004 | 2005-06-23 04:39:28 | | 50005 | 2005-06-24 04:41:20 | | 50006 | 2005-06-22 22:54:10 | | 50007 | 2005-06-18 07:21:51 | | 50008 | 2005-06-25 21:51:16 | | 50009 | 2005-06-21 03:44:32 | | 50010 | 2005-06-19 00:00:34 | +-------+---------------------+ 10 rows in set (0.75 sec)
在未優化以前,咱們看到它的執行計劃是如此的糟糕
explain select tid,return_date from t1 order by inventory_id limit 50000,10\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 1023675 1 row in set (0.00 sec)
看出是全表掃描。加上而外的排序,性能消耗是不低的
如何經過覆蓋索引優化呢?
咱們建立一個索引,包含排序列以及返回列,因爲tid是主鍵字段,所以,下面的複合索引就包含了tid的字段值
alter table t1 add index liu(inventory_id,return_date);
那麼,效果如何呢?
select tid,return_date from t1 order by inventory_id limit 50000,10; +-------+---------------------+ | tid | return_date | +-------+---------------------+ | 50001 | 2005-06-17 23:04:36 | | 50002 | 2005-06-23 03:16:12 | | 50003 | 2005-06-20 22:41:03 | | 50004 | 2005-06-23 04:39:28 | | 50005 | 2005-06-24 04:41:20 | | 50006 | 2005-06-22 22:54:10 | | 50007 | 2005-06-18 07:21:51 | | 50008 | 2005-06-25 21:51:16 | | 50009 | 2005-06-21 03:44:32 | | 50010 | 2005-06-19 00:00:34 | +-------+---------------------+ 10 rows in set (0.03 sec)
explain select tid,return_date from t1 order by inventory_id limit 50000,10\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 type: index possible_keys: NULL key: liu key_len: 9 ref: NULL rows: 50010
Extra: Using index
1 row in set (0.00 sec)
執行計劃也能夠看到,使用到了複合索引,而且不須要回表
對比一下以下的改寫SQL,思想是經過索引消除排序
select a.tid,a.return_date from t1 a inner join (select tid from t1 order by inventory_id limit 800000,10) b on a.tid=b.tid;
並在此基礎上,咱們爲inventory_id列建立索引,並刪除以前的覆蓋索引
alter table t1 add index idx_inid(inventory_id); drop index liu;
而後收集統計信息。
select a.tid,a.return_date from t1 a inner join (select tid from t1 order by inventory_id limit 800000,10) b on a.tid=b.tid; +--------+---------------------+ | tid | return_date | +--------+---------------------+ | 800001 | 2005-08-24 13:09:34 | | 800002 | 2005-08-27 11:41:03 | | 800003 | 2005-08-22 18:10:22 | | 800004 | 2005-08-22 16:47:23 | | 800005 | 2005-08-26 20:32:02 | | 800006 | 2005-08-21 14:55:42 | | 800007 | 2005-08-28 14:45:55 | | 800008 | 2005-08-29 12:37:32 | | 800009 | 2005-08-24 10:38:06 | | 800010 | 2005-08-23 12:10:57 | +--------+---------------------+
這種優化手段較前者時間多消耗了大約140ms。這種優化手段雖然使用索引消除了排序,可是仍是要經過主鍵值回表查詢。所以,在select返回列較少或列寬較小的時候,咱們能夠經過創建複合索引的方式優化分頁查詢,效果更佳,由於它不須要回表!
[1] 袋鼠雲技術團隊博客,https://yq.aliyun.com/articles/62419
[2] Baron Schwartz等 著,寧海元等 譯 ;《高性能MySQL》(第3版); 電子工業出版社 ,2013