【发布时间】: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