【问题标题】:MongoDB count query performanceMongoDB 计数查询性能
【发布时间】:2016-03-07 13:29:39
【问题描述】:

我在 MongoDB 中的计数性能有问题。

我正在使用带有 SoftDelete 过滤器的 ZF2 和 Doctrine ODM。现在当使用db.getCollection('order').count({"deletedAt": null}) 查询“第一次”集合时,大约需要 30 秒,有时甚至更长时间。第二个和更多查询大约需要 150 毫秒。几分钟后查询再次需要大约 30 秒。这仅适用于大小 > 700MB 的集合。

服务器是 Amazon EC2 t2.medium 实例,Mongo 3.0.1

也许它类似于MongoDB preload documents into RAM for better performance,但这些答案并不能解决我的问题。

有什么想法吗?

/编辑

解释

{
"executionSuccess" : true,
"nReturned" : 111449,
"executionTimeMillis" : 24966,
"totalKeysExamined" : 0,
"totalDocsExamined" : 111449,
"executionStages" : {
    "stage" : "COLLSCAN",
    "filter" : {
        "$and" : []
    },
    "nReturned" : 111449,
    "executionTimeMillisEstimate" : 281,
    "works" : 145111,
    "advanced" : 111449,
    "needTime" : 1,
    "needFetch" : 33660,
    "saveState" : 33660,
    "restoreState" : 33660,
    "isEOF" : 1,
    "invalidates" : 0,
    "direction" : "forward",
    "docsExamined" : 111449
},
"allPlansExecution" : []
}

【问题讨论】:

  • 您在deletedAt 字段上有索引吗?集合中有多少文档?
  • 我有索引,计数 > 110k
  • 请向我们展示解释的结果。在 mongos shell 上,执行db.order.explain("executionStats").find()
  • 添加解释结果
  • 如果那里有一些活动,mongo 将锁定计数,直到它可以得到准确的结果。 jira.mongodb.org/browse/SERVER-23017

标签: performance mongodb


【解决方案1】:

计数将遍历每个产生性能问题的文档。

如果数字很小,请注意确切的数字。您有兴趣知道是否有 100 个结果或 500 个结果。但是一旦超出,例如 10000 个,您只需向用户说 '超过 10000 个结果'

db.getCollection('order').find({"deletedAt": null}).limit(10000).count(true)

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2022-01-04
    • 1970-01-01
    • 2021-12-15
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多