什麼是覆寫索引
- 使用覆寫索引,隻需要從索引中就能檢索到需要的資料,而不要再掃描資料表;
- 索引的體量往往要比資料表小很多,是以隻讀取索引速度會非常快,也會極大減少資料通路量;
- MySQL的查詢優化器會在執行查詢前判斷,是否有一個索引可以覆寫所有的查詢列;
- 并非所有類型的索引都可以作為覆寫索引,覆寫索引必須要存儲索引列的值。像哈希索引、空間索引、全文索引等并不會真正存儲索引列的值。
如何判斷使用了覆寫索引
實戰案例
mysql> explain select * from employee where id=12\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employee
partitions: NULL
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
mysql> explain select id from employee where id=12\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: employee
partitions: NULL
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
filtered: 100.00
Extra: Using index
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
mysql>