• 学派吧-由新云网络独家赞助-https://www.sq9.cn。

百万数据下mysql分页问题-mysql教程-

MySQL admin 5年前 (2019-04-24) 1776次浏览 已收录 0个评论 扫描二维码

在开发过程中我们经常会使用分页,核心技术是使用limit进行数据的读取。在使用limit进行分页的测试过程中,得到以下数据:

select * from news order by id desc limit 0,10
耗时0.003秒
select * from news order by id desc limit 10000,10
耗时0.058秒
select * from news order by id desc limit 100000,10 
耗时0.575秒
select * from news order by id desc limit 1000000,10
耗时7.28秒

我们惊讶的发现mysql在数据量大的情况下分页起点越大查询速度越慢,100万条起的查询速度已经需要7秒钟。这是一个我们无法接受的数值!

改进方案 1

select * from news 
where id >  (select id from news order by id desc  limit 1000000, 1)
order by id desc 
limit 0,10

查询时间 0.365秒,提升效率是非常明显的!!原理是什么呢???

我们使用条件对id进行了筛选,在子查询 (select id from news order by id desc limit 1000000, 1) 中我们只查询了id这一个字段比起select * 或 select 多个字段 节省了大量的查询开销!

改进方案2

适合id连续的系统,速度极快!

select * from news 
where id  between 1000000 and 1000010 
order by id desc

不适合带有条件的、id不连续的查询。速度非常快!

以上就是百万数据下mysql分页问题的详细内容,更多请关注php中文网其它相关文章!


学派吧 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:百万数据下mysql分页问题-mysql教程-
喜欢 (0)
[pay@sq9.cn]
分享 (0)
关于作者:
腾讯云-运维运维 QQ 690624
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址