简体   繁体   English

使用Java在mongodb上分页的最佳方法是什么

[英]What is the best way for pagination on mongodb using java

I am trying to create a simple pagination in mongodb by below code. 我试图通过下面的代码在mongodb中创建一个简单的分页。

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

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. 但是,如果我们发现用Java术语首先查找会返回所有记录(考虑到我有200万条记录),那么看起来是否会出现性能问题,然后将其传递给skip方法,然后传递给limit方法。 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: 在谈论MongoDB中的分页时,很容易编写以下代码:

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

Above is the native solution supported by MongoDB, but this is not efficient if there are huge documents in the collection. 上面是MongoDB支持的本机解决方案,但是如果集合中包含大量文档,则这样做效率不高。 Suppose you have 100M documents, and you want to get the data from the middle offset(50Mth). 假设您有100M个文档,并且想要从中间偏移量(50Mth)中获取数据。 MongoDB has to build up the full dataset and walk from the beginning to the specified offset , this will be low performance. MongoDB必须构建完整的数据集,并从头开始到指定的偏移量 ,这将导致性能低下。 As your offset increases, the performance keeps degrade. 随着偏移量的增加,性能会不断下降。

The root cause is the skip() command which is not efficient and can not take big benifit from index. 根本原因是skip()命令效率低下,无法从索引中获得很大收益。


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. 分页的典型使用场景是有一个表或列表来显示指定页面的数据,还有一个“上一页”“下一页”按钮来加载上一页或下一页的数据。

If you got the '_id' of the last document in current page, you can use find() instead of skip() . 如果您获得了当前页面中最后一个文档的'_id' ,则可以使用find()而不是skip() Use _id > currentPage_LastDocument._id as one of the criteria to find next page data. 使用_id> currentPage_LastDocument._id作为查找下一页数据的条件之一。 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 = ...

This will avoid MongoDB to walk through large data when using skip() . 这将避免MongoDB在使用skip()时遍历大数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM