侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

mysql大表分页查询翻页优化方案

数据库  /  管理员 发布于 6年前   454

mysql分页查询是先查询出来所有数据,然后跳过offset,取limit条记录,造成了越往后的页数,查询时间越长

一般优化思路是转换offset,让offset尽可能的小,最好能每次查询都是第一页,也就是offset为0

查询按id排序的情况

一、如果查询是根据id排序的,并且id是连续的

这种网上介绍比较多,根据要查的页数直接算出来id的范围

比如offset=40, limit=10, 表示查询第5页数据,那么第5页开始的id是41,增加查询条件:id>40 limit 10

二、如果查询是根据id排序的,但是id不是连续的

通常翻页页数跳转都不会很大,那我们可以根据上一次查询的记录,算出来下一次分页查询对应的新的 offset和 limit,也就是离上一次查询记录的offset

分页查询一般会有两个参数:offset和limit,limit一般是固定,假设limit=10

那为了优化offset太大的情况,每次查询需要提供两个额外的参数

参数lastEndId: 上一次查询的最后一条记录的id

参数lastEndOffset: 上一次查询的最后一条记录对应的offset,也就是上一次查询的offset+limit

  1. 第一种情况(与第二种其实是一样):跳转到下一页,增加查询条件:id>lastEndId limit 10
  2. 第二种情况:往下翻页,跳转到下任意页,算出新的newOffset=offset-lastEndOffset,增加查询条件:id>lastEndId offset newOffset limit 10,但是如果newOffset也还是很大,比如,直接从第一页跳转到最后一页,这时候我们可以根据id逆序(如果原来id是正序的换成倒序,如果是倒序就换成正序)查询,根据总数量算出逆序查询对应的offset和limit,那么 newOffset = totalCount - offset - limit, 查询条件:id<lastEndId offset newOffset limit 10 ,然后再通过代码逆序,得到正确顺序的数据,注意:最后一页 offset + limit>=totalCount ,也就是算出来的newOffset 可能小于0, 所以最后一页的newOffset=0,limit = totalCount - offset
  3. 第三种情况:往上翻页,跳转到上任意页,根据id逆序 ,newOffset = lastEndOffset- offset - limit-1, 查询条件:id<lastEndId offset newOffset limit 10 ,然后再通过代码逆序,得到正确顺序的数据

三,如果查询是根据其他字段,比如一般使用的创建时间(createTime)排序

这种跟第二种情况差不多,区别是createTime不是唯一的,所以不能确定上一次最后一条记录对应的创建时间,哪些是下一页的,哪些是上一页的

这时候,增加一个请求参数lastEndCount:表示上一次查询最后一条记录对应的创建时间,有多少条是这同一时间的,这个根据上一次的数据统计

根据第二种情况下计算出来的newOffset加上lastEndCount,就是新的offset,其他的处理方式和第二种一致

java 示例:

/** * 如果是根据创建时间排序的分页,根据上一条记录的创建时间优化分布查询 *  * @see 将会自动添加createTime排序 * @param lastEndCreateTime *上一次查询的最后一条记录的创建时间 * @param lastEndCount 上一次查询的时间为lastEndCreateTime的数量 * @param lastEndOffset  上一次查询的最后一条记录对应的偏移量     offset+limit **/public Page<T> page(QueryBuilder queryBuilder, Date lastEndCreateTime, Integer lastEndCount, Integer lastEndOffset,int offset, int limit) {FromBuilder fromBuilder = queryBuilder.from(getModelClass());Page<T> page = new Page<>();int count = dao.count(fromBuilder);page.setTotal(count);if (count == 0) {return page;}if (offset == 0 || lastEndCreateTime == null || lastEndCount == null || lastEndOffset == null) {List<T> list = dao.find(SelectBuilder.selectFrom(fromBuilder.offsetLimit(offset, limit).order().desc("createTime").end()));page.setData(list);return page;}boolean isForward = offset >= lastEndOffset;if (isForward) {int calcOffset = offset - lastEndOffset + lastEndCount;int calcOffsetFormEnd = count - offset - limit;if (calcOffsetFormEnd <= calcOffset) {isForward = false;if (calcOffsetFormEnd > 0) {fromBuilder.order().asc("createTime").end().offsetLimit(calcOffsetFormEnd, limit);} else {fromBuilder.order().asc("createTime").end().offsetLimit(0, calcOffsetFormEnd + limit);}} else {fromBuilder.where().andLe("createTime", lastEndCreateTime).end().order().desc("createTime").end().offsetLimit(calcOffset, limit);}} else {fromBuilder.where().andGe("createTime", lastEndCreateTime).end().order().asc("createTime").end().offsetLimit(lastEndOffset - offset - limit - 1 + lastEndCount, limit);}List<T> list = dao.find(SelectBuilder.selectFrom(fromBuilder));if (!isForward) {list.sort(new Comparator<T>() {@Overridepublic int compare(T o1, T o2) {return o1.getCreateTime().before(o2.getCreateTime()) ? 1 : -1;}});}page.setData(list);return page;}

前端js参数,基于bootstrap table

    this.lastEndCreateTime = null;    this.currentEndCreateTime = null;        this.isRefresh = false;  this.currentEndOffset = 0;        this.lastEndOffset = 0;        this.lastEndCount = 0;        this.currentEndCount = 0;        $("#" + this.tableId).bootstrapTable({url: url,method: 'get',contentType: "application/x-www-form-urlencoded",//请求数据内容格式 默认是 application/json 自己根据格式自行服务端处理dataType:"json",dataField:"data",pagination: true,sidePagination: "server", // 服务端请求pageList: [10, 25, 50, 100, 200],search: true,showRefresh: true,toolbar: "#" + tableId + "Toolbar",iconSize: "outline",icons: {    refresh: "icon fa-refresh",},queryParams: function(params){if(params.offset == 0){this.currentEndOffset = params.offset + params.limit;}else{if(params.offset + params.limit==this.currentEndOffset){ //刷新this.isRefresh = true;params.lastEndCreateTime = this.lastEndCreateTime;    params.lastEndOffset = this.lastEndOffset;    params.lastEndCount = this.lastEndCount;}else{ console.log(this.currentEndCount);//跳页this.isRefresh = false;params.lastEndCreateTime = this.currentEndCreateTime;    params.lastEndOffset = this.currentEndOffset;    params.lastEndCount = this.currentEndCount;    this.lastEndOffset = this.currentEndOffset;    this.currentEndOffset = params.offset + params.limit;    console.log(params.lastEndOffset+","+params.lastEndCreateTime);    }}return params;},onSearch: function (text) {    this.keyword = text;},onPostBody : onPostBody,onLoadSuccess: function (resp) {if(resp.code!=0){alertUtils.error(resp.msg);}       var data = resp.data;    var dateLength = data.length;    if(dateLength==0){    return;    }    if(!this.isRefresh){     this.lastEndCreateTime =  this.currentEndCreateTime;         this.currentEndCreateTime = data[data.length-1].createTime;         this.lastEndCount = this.currentEndCount;         this.currentEndCount = 0;         for (var i = 0; i < resp.data.length; i++) {var item = resp.data[i];if(item.createTime === this.currentEndCreateTime){this.currentEndCount++;}}    }    }        });

更多MySQL相关技术文章,请访问MySQL教程栏目进行学习!

以上就是mysql大表分页查询翻页优化方案的详细内容,更多请关注其它相关文章!


  • 上一条:
    MySQL只能做小项目?是时候说几句公道话了!
    下一条:
    mysql 存储过程中使用动态sql语句
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客