mysql大表分页查询翻页优化方案
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
- 首先种状况(与第二种其实是同样):跳转到下一页,添加查询前提:id>lastEndId limit 10
- 第二种状况:往下翻页,跳转到下任意页,算出新的newOffset=offset-lastEndOffset,添加查询前提:id>lastEndId offset newOffset limit 10,但是要是newOffset也还是很大,比方,直接从首先页跳转到最后一页,这时候我们可以依据id逆序(要是本来id是正序的换成倒序,要是是倒序就换成正序)查询,依据总数目算出逆序查询对应的offset和limit,那么 newOffset = totalCount - offset - limit, 查询前提:id
=totalCount ,也就是算出来的newOffset 可能小于0, 所以最后一页的newOffset=0,limit = totalCount - offset - 第三种状况:往上翻页,跳转到上任意页,依据id逆序 ,newOffset = lastEndOffset- offset - limit-1, 查询前提:id
三,要是查询是依据其他字段,比方个别运用的新建工夫(createTime)排序
这种跟第二种状况差未几,区别是createTime不是独一的,所以不克不及肯定上一次最后一笔记录对应的新建工夫,哪些是下一页的,哪些是上一页的
这时候,添加一个要求参数lastEndCount:表示上一次查询最后一笔记录对应的新建工夫,有多少条是这统一工夫的,这个依据上一次的数据统计
依据第二种状况下盘算出来的newOffset加上lastEndCount,就是新的offset,其他的处置方式和第二种一致
java 示例:
/** * 要是是依据新建工夫排序的分页,依据上一笔记录的新建工夫优化散布查询 * * @see 将会主动增加createTime排序 * @param lastEndCreateTime * 上一次查询的最后一笔记录的新建工夫 * @param lastEndCount 上一次查询的工夫为lastEndCreateTime的数目 * @param lastEndOffset 上一次查询的最后一笔记录对应的偏移量 offset+limit **/ public Pagepage(QueryBuilder queryBuilder, Date lastEndCreateTime, Integer lastEndCount, Integer lastEndOffset, int offset, int limit) { FromBuilder fromBuilder = queryBuilder.from(getModelClass()); Page 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 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 list = dao.find(SelectBuilder.selectFrom(fromBuilder)); if (!isForward) { list.sort(new Comparator () { @Override public 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大表分页查询翻页优化方案的细致内容,更多请关注 百分百源码网 其它相干文章!