mysql大表分页查询翻页优化方案
数据库  /  管理员 发布于 6年前   429
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
三,如果查询是根据其他字段,比如一般使用的创建时间(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大表分页查询翻页优化方案的详细内容,更多请关注其它相关文章!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号