【发布时间】:2017-04-10 12:59:52
【问题描述】:
我有一个 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
}
因此,从 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
但似乎计算结果的页面摘要需要〜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
有没有办法关闭页面摘要或加快计数?
【问题讨论】:
-
不确定这是否有效,但您是否尝试过使用切片而不是页面:docs.spring.io/spring-data/commons/docs/current/api/org/…
-
这正是我锁定的原因:) Slice 也可以与 Pageable 参数结合使用。谢谢
-
把它作为一个答案,你可以关闭它,系统认为这不是没有答案。
标签: spring mongodb spring-data spring-data-mongodb spring-data-rest