1

MySQL优化案例--limit优化

 2 years ago
source link: https://blog.51cto.com/u_13874232/5688366
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

MySQL优化案例--limit优化

精选 原创

进击的CJR 2022-09-19 11:25:28 博主文章分类:MySQL ©著作权

文章标签 数据 聚簇索引 子节点 文章分类 MySQL 数据库 yyds干货盘点 阅读数227

案例SQL

生产有这样一条sql,其中val是辅助索引

select * from test where val=4 limit 300000,5;

查询过程:

  • 查询到索引叶子节点数据。
  • 根据叶子节点上的主键值去聚簇索引上查询需要的全部字段值。

类似于下面这张图:

MySQL优化案例--limit优化_数据

像上面这样,需要查询300005次索引节点,查询300005次聚簇索引的数据,最后再将结果过滤掉前300000条,取出最后5条。MySQL耗费了大量随机I/O在查询聚簇索引的数据上,而有300000次随机I/O查询到的数据是不会出现在结果集当中的。

肯定会有人问:既然一开始是利用索引的,为什么不先沿着索引叶子节点查询到最后需要的5个节点,然后再去聚簇索引中查询实际数据。这样只需要5次随机I/O,类似于下面图片的过程:

MySQL优化案例--limit优化_聚簇索引_02

改造SQL

​select * from test a inner join (select id from test where val=4 limit 300000,5) b on ​ ​a.id=b.id​​;

有效避免的回表IO消耗

  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK