Spring Data REST:慢速页面摘要

时间:2017-04-10 12:59:53

标签: spring mongodb spring-data spring-data-mongodb spring-data-rest

我有一个finder定义为从MongoRepository派生的Spring Data存储库,它在MongoDB中搜索3个不同的属性。这三个都有一个索引。

public Page<Content> findByIdInOrAuthorUserNameInOrTagsIdIn(
    @Param("ids") Collection ids,                                                          
    @Param("userNames") Collection userName,                                                          
    @Param("tagIds") Collection tagIds,                                                            
    @Param("pageable") Pageable pageable);

问题是一个属性的结果集为2,5 mio条目:

"page": {
   "size": 20,
   "totalElements": 2531397,
   "totalPages": 126570,
   "number": 5
}

因此,对于页面的查询非常快(13毫秒),如mongo日志文件中所示:

2017-04-10T12:50:27.562+0200 I COMMAND  [conn68] command content.content command: find { find: "content", filter: { $or: [ { $or: [ { _id: { $in: [ "..." ] } }, { author.userName: { $in: [ "...", "..." ] } } ] }, { tags._id: { $in: [ "..." ] } } ] }, skip: 100, limit: 20 } planSummary: IXSCAN { _id: 1 }, IXSCAN { tags._id: 1 }, IXSCAN { author.userName: 1 } keysExamined:120 docsExamined:120 cursorExhausted:1 numYields:0 nreturned:20 reslen:21185 locks:{ Global: { acquireCount: { r: 2 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { r: 1 } } } protocol:op_query 13ms

但似乎计算结果的页面摘要需要~117s:

2017-04-10T12:52:24.172+0200 I COMMAND  [conn68] command content.content command: count { count: "content", query: { $or: [ { $or: [ { _id: { $in: [ "..." ] } }, { author.userName: { $in: [ "...", "..." ] } } ] }, { tags._id: { $in: [ "..." ] } } ] } } planSummary: IXSCAN { _id: 1 }, IXSCAN { tags._id: 1 }, IXSCAN { author.userName: 1 } keysExamined:2531466 docsExamined:2531397 numYields:21190 reslen:44 locks:{ Global: { acquireCount: { r: 42382 } }, Database: { acquireCount: { r: 21191 } }, Collection: { acquireCount: { r: 21191 } } } protocol:op_query 116592ms

有没有办法关闭页面摘要或加快计算方式?

1 个答案:

答案 0 :(得分:1)

使用Slice代替Page。它与Page非常相似,但不需要元素总数。