博主信息
博文 24
粉丝 0
评论 2
访问量 33614
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
百万数据下mysql分页问题
路雲萧的博客
原创
1130人浏览过

在开发过程中我们经常会使用分页,核心技术是使用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不连续的查询。速度非常快!


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学