
Mysql ページング クエリは、最初にすべてのデータをクエリし、次にオフセットをスキップして制限レコードを取得します。その結果、ページ数が長くなり、クエリ時間も長くなります。
一般的な最適化の考え方は、オフセットを変換してオフセットをできるだけ小さくすることです。毎回最初のページをクエリするのが最善です。つまり、オフセットは 0
クエリは ID によって並べ替えられます
1. クエリが ID に従って並べ替えられ、ID が連続している場合
これについてはオンラインで多くの紹介があり、 ID は、チェックするページ数に基づいて直接計算できます。範囲
たとえば、offset=40、limit=10 は、5 ページのデータをクエリし、5 ページから始まる ID を意味します。が 41 の場合、クエリ条件を追加します: id>40 制限 10
2. クエリが ID に従って並べ替えられているが、ID が連続していない場合は、
#通常、ページ ジャンプの数はそれほど大きくありません。その場合、上記に基づいて、クエリのレコードについて、次のページング クエリに対応する新しいオフセットと制限 (オフセット
#) を計算できます。 ## 前のクエリ レコードから。ページング クエリには通常、オフセットと制限の 2 つのパラメータがあります。制限は通常、limit=10 を想定して固定されています オフセットが大きすぎる状況を最適化するために、2 つの追加パラメータが必要ですクエリごとに指定する必要があります。パラメータ lastEndId: 前のクエリの最後のレコードの IDパラメータ lastEndOffset: 最後のクエリの最後のレコードに対応するオフセット。最後のクエリのオフセット制限クエリが他のフィールド (一般的に使用される作成時間 (createTime) など) に基づいて並べ替えられる場合、
This 2 番目の状況と同じです。ほぼ同じです。違いは、createTime が一意ではないため、最後のレコードに対応する作成時刻、どのレコードが次のページからのもので、どれが前のページからのものかを判断できないことです この時点で、リクエスト パラメーター lastEndCount を追加します。最後のクエリの最後のレコードに対応する作成時刻と、同時に存在するレコードの数を示します。これは、最後のデータ統計に基づいています。
2 番目のケースで計算された newOffset を追加lastEndCount は新しいオフセットです。他の処理方法は 2 番目と同じです。
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>() {
@Override
public int compare(T o1, T o2) {
return o1.getCreateTime().before(o2.getCreateTime()) ? 1 : -1;
}
});
}
page.setData(list);
return page;
} 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 ラージ テーブル ページング クエリ ページめくり最適化ソリューションの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。