Spring 数据 REST:慢速页面摘要

Spring Data REST: slow page summary

我有一个定义为 Spring 派生自 MongoRepository 的数据存储库的查找器,它在 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
}

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

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

但是统计结果的page summary好像用了~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

有没有办法关闭页面摘要或加快计数速度?

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