使用索引扫描做排序

使用索引扫描来做排序

mysql有两种方式可以生成有序的结果:通过排序操作或者按索引顺序扫描

  • 如果explain出来的type列的值为index,则说明mysql使用了索引扫描来做排序
  • ​扫描索引本身是很快的,因为只需要从一条索引记录移动到紧接着的下一条记录
  • 但如果索引不能覆盖查询所需的全部列,那么就不得不每扫描一条索引记录就得回表查询一次对应的行
  • 这基本都是随机IO,因此按索引顺序读取数据的速度通常要比顺序地全表扫描

mysql可以使用同一个索引即满足排序,又用于查找行,如果可能的话,设计索引时应该尽可能地同时满足这两种任务

  • ​只有当索引的列顺序order by子句的顺序完全一致,并且所有列的排序方式都一样时,mysql才能够使用索引来对结果进行排序
  • 如果查询需要关联多张表,则只有当order by子句引用的字段全部为第一张表时,才能使用索引做排序
  • order by子句和查找型查询的限制是一样的,需要满足索引的最左前缀的要求

否则,mysql都需要执行顺序操作,而无法利用索引排序

举例

rental表在rentaldate,inventoryid,customerid上有rentaldate的索引

使用rentaldate索引为下面的查询做排序

explain select rentalid,staffid from rental where rentaldate='2005-05-25' order by inventory_id,customeridG
*************************** 1. row *************************** 
id: 1 selecttype: SIMPLE table: rental partitions: NULL type: ref possiblekeys: rentaldate key: rentaldate keylen: 5 ref: const rows: 1 filtered: 100.00 Extra: Using index condition 1 row in set, 1 warning (0.00 sec)

order by子句不满足索引的最左前缀的要求,也可以用于查询排序,这是因为索引的第一列被指定为一个常数

该查询为索引的第一列提供了常量条件,而使用第二列进行排序,将两个列组合在一起,就形成了索引的最左前缀

explain select rentalid,staffid from rental where rentaldate='2005-05-25' order by inventoryid descG
*************************** 1. row ***************************
id: 1 selecttype: SIMPLE table: rental partitions: NULL type: ref possiblekeys: rentaldate key: rentaldate key_len: 5 ref: const rows: 1 filtered: 100.00 Extra: Using where
1 row in set, 1 warning (0.00 sec)

下面的查询不会利用索引

explain select rentalid,staffid from rental where rentaldate>'2005-05-25' order by rentaldate,inventoryidG
*************************** 1. row ***************************
id: 1 selecttype: SIMPLE table: rental partitions: NULL type: ALL possiblekeys: rentaldate key: NULL key_len: NULL ref: NULL rows: 16005 filtered: 50.00 Extra: Using where; Using filesort;

该查询使用了两中不同的排序方向,但是索引列都是正序排序的

explain select rentalid,staffid from rental where rentaldate>'2005-05-25' order by inventoryid desc,customerid ascG
*************************** 1. row ***************************
id: 1 selecttype: SIMPLE table: rental partitions: NULL type: ALL possiblekeys: rentaldate key: NULL key_len: NULL ref: NULL rows: 16005 filtered: 50.00 Extra: Using where; Using filesort
1 row in set, 1 warning (0.00 sec)

该查询中引用了一个不在索引中的列

explain select rentalid,staffid from rental where rentaldate>'2005-05-25' order by inventoryid,staffidG 
*************************** 1. row ***************************
id: 1 selecttype: SIMPLE table: rental partitions: NULL type: ALL possiblekeys: rentaldate key: NULL key_len: NULL ref: NULL rows: 16005 filtered: 50.00 Extra: Using where; Using filesort
1 row in set, 1 warning (0.00 sec)
论读书
睁开眼,书在面前
闭上眼,书在心里
原文地址:https://www.cnblogs.com/YC-L/p/14461561.html