什么是覆盖索引
- 使用覆盖索引,只需要从索引中就能检索到需要的数据,而不要再扫描数据表;
- 索引的体量往往要比数据表小很多,因此只读取索引速度会非常快,也会极大减少数据访问量;
- 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>