Spring 数据 MongoDB - 使用 Pageable 查找数百万数据 - 内存过载?

Spring Data MongoDB - Find millions of data with Pageable - Memory overload?

我正在使用 Spring 数据 MongoDB 并且我有这个简单的存储库:

@Repository
public interface TracksRepository extends MongoRepository<Track, String> {

}

我正在使用 Pageable 像这样 tracksRepository.findAll(PageRequest.of(0,100))

获取我的曲目

例如,如果我有 1 亿首曲目,会发生什么情况?

它们是否会全部加载到内存中(可能会破坏我的服务器)以便它们被分页?

我问这个是因为我看到 SpringDataMongo 在内部使用这个 code

@Override
public <S extends T> Page<S> findAll(final Example<S> example, Pageable pageable) {

    Assert.notNull(example, "Sample must not be null!");
    Assert.notNull(pageable, "Pageable must not be null!");

    Query q = new Query(new Criteria().alike(example)).with(pageable);
    List<S> list = mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName());

    return PageableExecutionUtils.getPage(list, pageable,
            () -> mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()));
}

这表明 list 首先填充结果然后进行分页?

如果为真,我如何在不使服务器超载的情况下实现高效的大数据查询(使用分页)?谢谢。

您误解了代码。

此行定义要执行的主要查询:

Query q = new Query(new Criteria().alike(example)).with(pageable);

它已经完成了分页的主要工作:限制结果。

下面的表达式只是执行计数查询,统计元素的总数,但仅当无法从已查询的结果中确定总数时才这样做。如果它包含的元素少于请求的元素,这是可能的。

PageableExecutionUtils.getPage(list, pageable,
            () -> mongoOperations.count(q, example.getProbeType(), entityInformation.getCollectionName()));

因此,在对数百万文档进行分页时,没有理由预期会出现任何固有问题。