我使用MongoDB 3.2.13并且我有一个带有大约500K文档的集合C.这些文档包含一个String字段A,它只能是少数几个不同的值。如果我查询集合中的特定值,如
db.getCollection('C').count({'A':{'$eq': 'valueA'}})
db.getCollection('C').count({'A':{'$eq': 'valueB'}})
我在 1秒下获得了结果。 如果我使用$或
进行组合搜索 db.getCollection('C').count({'$or':
[
{'A':{'$eq': 'valueA'}},
{'A':{'$eq': 'valueB'}}
]
})
查询大约 165秒。 我有一个索引A.我无法解释为什么$或查询这么慢?这只是一个简单的例子,可以通过添加前两个查询的结果来解决,但我们还有其他查询无法轻易拆分。
任何人都可以解释$或查询的错误吗?或者给我一个提示如何加快速度?
db.getCollection('C')。find(query).explain()给出:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "db.C",
"indexFilterSet" : false,
"parsedQuery" : {
"$or" : [
{
"A" : {
"$eq" : "valueA"
}
},
{
"A" : {
"$eq" : "valueB"
}
}
]
},
"winningPlan" : {
"stage" : "SUBPLAN",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"A" : 1
},
"indexName" : "A",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"A" : [
"[\"valueA\", \"valueA\"]",
"[\"valueB\", \"valueB\"]"
]
}
}
}
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2513596,
"executionTimeMillis" : 133764,
"totalKeysExamined" : 2513597,
"totalDocsExamined" : 2513596,
"executionStages" : {
"stage" : "SUBPLAN",
"nReturned" : 2513596,
"executionTimeMillisEstimate" : 131660,
"works" : 2513597,
"advanced" : 2513596,
"needTime" : 0,
"needYield" : 0,
"saveState" : 20912,
"restoreState" : 20912,
"isEOF" : 1,
"invalidates" : 0,
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 2513596,
"executionTimeMillisEstimate" : 131490,
"works" : 2513597,
"advanced" : 2513596,
"needTime" : 0,
"needYield" : 0,
"saveState" : 20912,
"restoreState" : 20912,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 2513596,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2513596,
"executionTimeMillisEstimate" : 4420,
"works" : 2513597,
"advanced" : 2513596,
"needTime" : 0,
"needYield" : 0,
"saveState" : 20912,
"restoreState" : 20912,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"A" : 1
},
"indexName" : "A_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"A" : [
"[\"valueA\", \"valueA\"]",
"[\"valueB\", \"valueB\"]"
]
},
"keysExamined" : 2513597,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"allPlansExecution" : []
},
"serverInfo" : {
"host" : "xxxx",
"port" : 27017,
"version" : "3.2.13",
"gitVersion" : "23899209cad60aaafe114f6aea6cb83025ff51bc"
},
"ok" : 1.0
}
答案 0 :(得分:1)
使用$in
代替$or
(Mongo $or vs $in docs):
db.getCollection('C').count({'A':{'$in': ['valueA', 'valueB']}})
UPDATE
或尝试使用find(..)。count(),如下所示:
db.getCollection('C').find({'A':{'$in': ['valueA', 'valueB']}}).count()
答案 1 :(得分:0)
搜索和搜索后我找到了原因。我搜索的值之一具有空值。索引不包含Null,因此从磁盘读取每个文档以检查是否为null。
Mongo大学研讨会也提到了这一点。我强烈建议上课。他们帮助了我很多!