使用索引扫描做排序

使用索引扫描来做排序#

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,customerid\G
*************************** 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 desc\G
*************************** 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,inventoryid\G
*************************** 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 asc\G
*************************** 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,staffid\G 
*************************** 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)
posted @   BigBender  阅读(150)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示
主题色彩