【发布时间】:2021-04-28 10:27:18
【问题描述】:
我正在使用 Spring Data 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 首先填充了结果,然后进行了分页?
如果为真,我怎样才能在不使服务器超载的情况下实现高效的大数据查询(带分页)?谢谢。
【问题讨论】:
标签: java mongodb spring-boot spring-data spring-data-mongodb