使用 java 在 mongodb 上进行分页的最佳方法是什么 [英] What is the best way for pagination on mongodb using java

查看:34
本文介绍了使用 java 在 mongodb 上进行分页的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过以下代码在 mongodb 中创建一个简单的分页.

I am trying to create a simple pagination in mongodb by below code.

collection.find().skip(n).limit(n);

但是,如果我们看到在 Java 术语中首先 find 将返回所有记录,考虑到我有 200 万条记录,那么它看起来是不是会出现性能问题,然后它将它传递给 skip 方法然后它会被传递来限制方法.这意味着每次此查询都会获取所有 db 记录或 mongodb 驱动程序的工作方式不同,我错过了什么?

but doesn't it looks like there will be a performance issue if we see that in java terms first find will return all the records consider i have 2 million records, then it will pass it to skip method then it will be passed to limit method. it means every time this query will be fetching all the db records or mongodb drivers work differently, what i missed?

推荐答案

说到MongoDB中的分页,很容易写出这段代码:

When talking about pagination in MongoDB, it is easily to write this code:

collection.find().skip(pageSize*(pageNum-1)).limit(pageSize);

以上是MongoDB支持的原生解决方案,但是如果集合中有大量文档,则效率不高.假设您有 100M 个文档,并且您想从中间偏移量(第 50M 个)中获取数据.MongoDB 必须建立完整的数据集并从头走到指定的偏移量,这将是低性能的.随着偏移量的增加,性能会不断下降.

Above is the native solution supported by MongoDB, but this is not efficient if there are huge documents in the collection. Suppose you have 100M documents, and you want to get the data from the middle offset(50Mth). MongoDB has to build up the full dataset and walk from the beginning to the specified offset, this will be low performance. As your offset increases, the performance keeps degrade.

根本原因是skip()命令效率不高,不能从索引中得到很大的好处.

The root cause is the skip() command which is not efficient and can not take big benifit from index.

下面是另一种提高大数据分页性能的解决方案:

Below is another solution to improve performance on large data pagination:

分页的典型使用场景是有一个表格或列表显示指定页面的数据,还有一个'上一页' &下一页"按钮加载上一页或下一页的数据.

The typical usage scenario of pagination is that there is a table or list to show data of specified page, and also a 'Previous Page' & 'Next Page' button to load data of previous or next page.

如果你得到当前页面最后一个文档的'_id',你可以用find()代替skip().使用 _id > currentPage_LastDocument._id 作为查找下一页数据的条件之一.这是伪代码:

If you got the '_id' of the last document in current page, you can use find() instead of skip(). Use _id > currentPage_LastDocument._id as one of the criteria to find next page data. Here is pseudocode:

//Page 1
collection.find().limit(pageSize);
//Get the _id of the last document in this page
last_id = ...

//Page 2
users = collection.find({'_id': {$gt: last_id}}).limit(pageSize);
//Update the last id with the _id of the last document in this page
last_id = ...

这将避免 MongoDB 在使用 skip() 时遍历大数据.

This will avoid MongoDB to walk through large data when using skip().

这篇关于使用 java 在 mongodb 上进行分页的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆