为什么MySQL分页用limit会越来越慢
16lz
2021-12-10
语句如下:
select * from orders where name=‘lilei' and create_time>'2020-01-01 00:00:00' limit start,end
一、测试实验
mysql分页直接用limit start, count分页语句:
select * from product limit start, count
select * from product limit 10, 20 0.016秒select * from product limit 100, 20 0.016秒select * from product limit 1000, 20 0.047秒select * from product limit 10000, 20 0.094秒
select * from product limit 400000, 20 3.229秒
select * from product limit 866613, 20 37.44秒
二、 对limit分页问题的性能优化方法
2.1 利用表的覆盖索引来加速分页查询
我们都知道,利用了索引查询的语句中如果只包含了那个索引列(覆盖索引),那么这种情况会查询很快。
因为利用索引查找有优化算法,且数据就在查询索引上面,不用再去找相关的数据地址了,这样节省了很多时间。
另外Mysql中也有相关的索引缓存,在并发高的时候利用缓存就效果更好了。
在我们的例子中,我们知道id字段是主键,自然就包含了默认的主键索引。现在让我们看看利用覆盖索引的查询效果如何:
这次我们之间查询最后一页的数据(利用覆盖索引,只包含id列),如下:
select id from product limit 866613, 20
2.2 利用 id>=的形式:
SELECT * FROM product WHERE ID > =(select id from product limit 866613, 1) limit 20
2.3 利用join
SELECT * FROM product a JOIN (select id from product limit 866613, 20) b ON a.ID = b.id
更多相关文章
- MySQL系列多表连接查询92及99语法示例详解教程
- Linux下MYSQL 5.7 找回root密码的问题(亲测可用)
- MySQL 什么时候使用INNER JOIN 或 LEFT JOIN
- android从服务器下载文件(php+apache+win7+MySql)
- 【有图】android通过jdbc连接mysql(附文件)
- android分页查询获取系统联系人信息
- Android入门教程(三十一)------SQLite分页读取
- android ListView的分段显示、分页显示(附源码)
- android左右滑动加载分页以及动态加载数据