项目中分页查询时间过长
项目中可能会遇到数据量很大,同时有需要多表联合查询的时候,这个时候,在计算页码的时候,就会遇到如何处理的问题。
在项目中我的处理方案
- mysql数据库索引优化(一般情况下可以满足需求)
- 前端获取数据时,不计算总数
前端页码布局
<div> <div style="float: right; margin: 20px;"> <span><el-button size="small" :disabled="flag" @click="toBeferPage">上一页</el-button></span> <span style="margin: 0 10px;"><el-button size="small" @click="toAfterPage">下一页</el-button></span> <span>前往<el-input size="small" @blur="toPageBlur" v-model.number="ruleForm.pageIndex" style="width: 80px; margin: 0 10px;" ></el-input>页</span> </div> </div>
前端样式
js代码
// 页码跳转 toPageBlur() { const that = this; if (that.ruleForm.pageIndex < 1) { that.$message.warning("请输入大于0的数字"); that.ruleForm.pageIndex = 1; that.flag = true; return false; } if (that.ruleForm.pageIndex < 2) { that.flag = true; } else { that.flag = false; } // 获取分页数据信息函数 that.getList(); }, // 上一页 toBeferPage() { const that = this; that.ruleForm.pageIndex = that.ruleForm.pageIndex - 1; if (that.ruleForm.pageIndex < 2) { that.flag = true; } // 获取分页数据信息函数 that.getList(); }, // 下一页 toAfterPage() { const that = this; that.ruleForm.pageIndex = that.ruleForm.pageIndex + 1; that.flag = false; // 获取分页数据信息函数 that.getList(); },