覆盖索引

1、当发起一个被索引覆盖的查询时,在explain的extra列可以看到using index的信息,此时就使用了覆盖索引

  1. mysql> explain select store_id,film_id from inventory\G
  2. *************************** 1. row ***************************
  3. id: 1
  4. select_type: SIMPLE
  5. table: inventory
  6. partitions: NULL
  7. type: index
  8. possible_keys: NULL
  9. key: idx_store_id_film_id
  10. key_len: 3
  11. ref: NULL
  12. rows: 4581
  13. filtered: 100.00
  14. Extra: Using index
  15. 1 row in set, 1 warning (0.01 sec)

2、在大多数存储引擎中,覆盖索引只能覆盖那些只访问索引中部分列的查询。不过,可以进一步的进行优化,可以使用innodb的二级索引来覆盖查询。

例如:actor使用innodb存储引擎,并在last_name字段又二级索引,虽然该索引的列不包括主键actor_id,但也能够用于对actor_id做覆盖查询

  1. mysql> explain select actor_id,last_name from actor where last_name='HOPPER'\G
  2. *************************** 1. row ***************************
  3. id: 1
  4. select_type: SIMPLE
  5. table: actor
  6. partitions: NULL
  7. type: ref
  8. possible_keys: idx_actor_last_name
  9. key: idx_actor_last_name
  10. key_len: 137
  11. ref: const
  12. rows: 2
  13. filtered: 100.00
  14. Extra: Using index
  15. 1 row in set, 1 warning (0.00 sec)