百万数据下mysql分页问题


本文摘自PHP中文网,作者藏色散人,侵删。

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

1

2

3

4

5

6

7

8

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

1

2

3

4

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连续的系统,速度极快!

1

2

3

select * from news

where id  between 1000000 and 1000010

order by id desc

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

以上就是百万数据下mysql分页问题的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

百万数据下mysql分页问题

更多相关阅读请进入《mysql分页》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...