面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?

沙海 2021年4月30日01:09:20Java评论41字数 4503阅读15分0秒阅读模式
摘要

速读摘要

速读摘要文章源自JAVA秀-https://www.javaxiu.com/21822.html

对于数据库分页查询,也有很多种方法和优化的点。7.16线下找一张百万级的测试表可不容易,如果需要自己测试的话,可以写shell脚本什么的插入数据进行测试。限制是只能使用于明确知道id的情况,不过一般建立表的时候,都会添加基本的id字段,这为分页查询带来很多便利。这样能够极大的提高传统的分页查询速度,尤其是数据量上千万的时候。MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/21822.html

原文约 4997 | 图片 6 | 建议阅读 10 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/21822.html

面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?

点击关注 ? Java基基 文章源自JAVA秀-https://www.javaxiu.com/21822.html

收录于话题文章源自JAVA秀-https://www.javaxiu.com/21822.html

#Java基基文章源自JAVA秀-https://www.javaxiu.com/21822.html

37个文章源自JAVA秀-https://www.javaxiu.com/21822.html

点击上方“Java基基”,选择“设为星标”文章源自JAVA秀-https://www.javaxiu.com/21822.html

做积极的人,而不是积极废人!文章源自JAVA秀-https://www.javaxiu.com/21822.html

文章源自JAVA秀-https://www.javaxiu.com/21822.html

源码精品专栏文章源自JAVA秀-https://www.javaxiu.com/21822.html

 文章源自JAVA秀-https://www.javaxiu.com/21822.html

文章源自JAVA秀-https://www.javaxiu.com/21822.html

来源:cnblogs.com/youyoui/p/7851007.html文章源自JAVA秀-https://www.javaxiu.com/21822.html

大家好,我是基基!文章源自JAVA秀-https://www.javaxiu.com/21822.html

面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?文章源自JAVA秀-https://www.javaxiu.com/21822.html

当需要从数据库查询的表有上万条记录的时候,一次性查询所有结果会变得很慢,特别是随着数据量的增加特别明显,这时需要使用分页查询。对于数据库分页查询,也有很多种方法和优化的点。下面简单说一下我知道的一些方法。文章源自JAVA秀-https://www.javaxiu.com/21822.html

准备工作

为了对下面列举的一些优化进行测试,下面针对已有的一张表进行说明。文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 表名:order_history文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 描述:某个业务的订单历史表文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 主要字段:unsigned int id,tinyint(4) int type文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 字段情况:该表一共37个字段,不包含text等大型数据,最大为varchar(500),id字段为索引,且为递增。文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 数据量:5709294文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • MySQL版本:5.7.16 线下找一张百万级的测试表可不容易,如果需要自己测试的话,可以写shell脚本什么的插入数据进行测试。以下的 sql 所有语句执行的环境没有发生改变,下面是基本测试结果:文章源自JAVA秀-https://www.javaxiu.com/21822.html

select count(*) from orders_history;

返回结果:5709294文章源自JAVA秀-https://www.javaxiu.com/21822.html

三次查询时间分别为:文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 8903 ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 8323 ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 8401 ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

一般分页查询

一般的分页查询使用简单的 limit 子句就可以实现。limit 子句声明如下:文章源自JAVA秀-https://www.javaxiu.com/21822.html

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset

LIMIT 子句可以被用于指定 SELECT 语句返回的记录数。需注意以下几点:文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 第一个参数指定第一个返回记录行的偏移量,注意从0开始文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 第二个参数指定返回记录行的最大数目文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 如果只给定一个参数:它表示返回最大的记录行数目文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 第二个参数为 -1 表示检索从某一个偏移量到记录集的结束所有的记录行文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 初始记录行的偏移量是 0(而不是 1)文章源自JAVA秀-https://www.javaxiu.com/21822.html

下面是一个应用实例:文章源自JAVA秀-https://www.javaxiu.com/21822.html

select * from orders_history where type=8 limit 1000,10;

该条语句将会从表 orders_history 中查询offset: 1000开始之后的10条数据,也就是第1001条到第1010条数据(1001 <= id <= 1010)。文章源自JAVA秀-https://www.javaxiu.com/21822.html

数据表中的记录默认使用主键(一般为id)排序,上面的结果相当于:文章源自JAVA秀-https://www.javaxiu.com/21822.html

select * from orders_history where type=8 order by id limit 10000,10;

三次查询时间分别为:文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 3040 ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 3063 ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 3018 ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

针对这种查询方式,下面测试查询记录量对时间的影响:文章源自JAVA秀-https://www.javaxiu.com/21822.html

select * from orders_history where type=8 limit 10000,1;select * from orders_history where type=8 limit 10000,10;select * from orders_history where type=8 limit 10000,100;select * from orders_history where type=8 limit 10000,1000;select * from orders_history where type=8 limit 10000,10000;

三次查询时间如下:文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询1条记录:3072ms 3092ms 3002ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询10条记录:3081ms 3077ms 3032ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询100条记录:3118ms 3200ms 3128ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询1000条记录:3412ms 3468ms 3394ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询10000条记录:3749ms 3802ms 3696ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

另外我还做了十来次查询,从查询时间来看,基本可以确定,在查询记录量低于100时,查询时间基本没有差距,随着查询记录量越来越大,所花费的时间也会越来越多。文章源自JAVA秀-https://www.javaxiu.com/21822.html

针对查询偏移量的测试:文章源自JAVA秀-https://www.javaxiu.com/21822.html

select * from orders_history where type=8 limit 100,100;select * from orders_history where type=8 limit 1000,100;select * from orders_history where type=8 limit 10000,100;select * from orders_history where type=8 limit 100000,100;select * from orders_history where type=8 limit 1000000,100;

三次查询时间如下:文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询100偏移:25ms 24ms 24ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询1000偏移:78ms 76ms 77ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询10000偏移:3092ms 3212ms 3128ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询100000偏移:3878ms 3812ms 3798ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 查询1000000偏移:14608ms 14062ms 14700ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

随着查询偏移的增大,尤其查询偏移大于10万以后,查询时间急剧增加。文章源自JAVA秀-https://www.javaxiu.com/21822.html

这种分页查询方式会从数据库第一条记录开始扫描,所以越往后,查询速度越慢,而且查询的数据越多,也会拖慢总查询速度。文章源自JAVA秀-https://www.javaxiu.com/21822.html

使用子查询优化

这种方式先定位偏移位置的 id,然后往后查询,这种方式适用于 id 递增的情况。文章源自JAVA秀-https://www.javaxiu.com/21822.html

select * from orders_history where type=8 limit 100000,1;select id from orders_history where type=8 limit 100000,1;select * from orders_history where type=8 andid>=(select id from orders_history where type=8 limit 100000,1)limit 100;select * from orders_history where type=8 limit 100000,100;

4条语句的查询时间如下:文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 第1条语句:3674ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 第2条语句:1315ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 第3条语句:1327ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 第4条语句:3710ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

针对上面的查询需要注意:文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 比较第1条语句和第2条语句:使用 select id 代替 select * 速度增加了3倍文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 比较第2条语句和第3条语句:速度相差几十毫秒文章源自JAVA秀-https://www.javaxiu.com/21822.html

  • 比较第3条语句和第4条语句:得益于 select id 速度增加,第3条语句查询速度增加了3倍文章源自JAVA秀-https://www.javaxiu.com/21822.html

这种方式相较于原始一般的查询方法,将会增快数倍。文章源自JAVA秀-https://www.javaxiu.com/21822.html

使用 id 限定优化

这种方式假设数据表的id是连续递增 的,则我们根据查询的页数和查询的记录数可以算出查询的id的范围,可以使用 id between and 来查询:文章源自JAVA秀-https://www.javaxiu.com/21822.html

select * from orders_history where type=2and id between 1000000 and 1000100 limit 100;

查询时间:15ms 12ms 9ms文章源自JAVA秀-https://www.javaxiu.com/21822.html

这种查询方式能够极大地优化查询速度,基本能够在几十毫秒之内完成。限制是只能使用于明确知道id的情况,不过一般建立表的时候,都会添加基本的id字段,这为分页查询带来很多便利。文章源自JAVA秀-https://www.javaxiu.com/21822.html

还可以有另外一种写法:文章源自JAVA秀-https://www.javaxiu.com/21822.html

select * from orders_history where id >= 1000001 limit 100;

当然还可以使用 in 的方式来进行查询,这种方式经常用在多表关联的时候进行查询,使用其他表查询的id集合,来进行查询:文章源自JAVA秀-https://www.javaxiu.com/21822.html

select * from orders_history where id in(select order_id from trade_2 where goods = 'pen')limit 100;

这种 in 查询的方式要注意:某些 mysql 版本不支持在 in 子句中使用 limit。文章源自JAVA秀-https://www.javaxiu.com/21822.html

使用临时表优化

这种方式已经不属于查询优化,这儿附带提一下。文章源自JAVA秀-https://www.javaxiu.com/21822.html

对于使用 id 限定优化中的问题,需要 id 是连续递增的,但是在一些场景下,比如使用历史表的时候,或者出现过数据缺失问题时,可以考虑使用临时存储的表来记录分页的id,使用分页的id来进行 in 查询。这样能够极大的提高传统的分页查询速度,尤其是数据量上千万的时候。文章源自JAVA秀-https://www.javaxiu.com/21822.html

关于数据表的id说明

一般情况下,在数据库中建立表的时候,强制为每一张表添加 id 递增字段,这样方便查询。文章源自JAVA秀-https://www.javaxiu.com/21822.html

如果像是订单库等数据量非常庞大,一般会进行分库分表。这个时候不建议使用数据库的 id 作为唯一标识,而应该使用分布式的高并发唯一 id 生成器来生成,并在数据表中使用另外的字段来存储这个唯一标识。文章源自JAVA秀-https://www.javaxiu.com/21822.html

使用先使用范围查询定位 id (或者索引),然后再使用索引进行定位数据,能够提高好几倍查询速度。即先 select id,然后再 select *;文章源自JAVA秀-https://www.javaxiu.com/21822.html

本人才疏学浅,难免犯错,若发现文中有错误遗漏,望不吝赐教。文章源自JAVA秀-https://www.javaxiu.com/21822.html

欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢文章源自JAVA秀-https://www.javaxiu.com/21822.html

面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?文章源自JAVA秀-https://www.javaxiu.com/21822.html

已在知识星球更新源码解析如下:文章源自JAVA秀-https://www.javaxiu.com/21822.html

面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?文章源自JAVA秀-https://www.javaxiu.com/21822.html

面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?文章源自JAVA秀-https://www.javaxiu.com/21822.html

面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?文章源自JAVA秀-https://www.javaxiu.com/21822.html

面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?面试官扎问一问:数据量很大,分页查询很慢,有什么优化方案?文章源自JAVA秀-https://www.javaxiu.com/21822.html

最近更新《芋道 SpringBoot 2.X 入门》系列,已经 20 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/21822.html

提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。文章源自JAVA秀-https://www.javaxiu.com/21822.html

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/21822.html

文章源自JAVA秀-https://www.javaxiu.com/21822.html

文章有帮助的话,在看,转发吧。谢谢支持哟 (*^__^*)
文章源自JAVA秀-https://www.javaxiu.com/21822.html

阅读原文文章源自JAVA秀-https://www.javaxiu.com/21822.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定