Mongo Db优化查询
问题描述:
在我的用例中,我有超过3000个变量ids,我已经将其分配给其他集合作为mongo db中的外键。在其他集合中,对于每个外键,包含列表类型中的一个字段的文档数量更多。我需要通过满足至少一个每个外键列表大小的文档大于0的条件来查询从3000中检索不同的外键。我目前使用下面的查询作为示例。Mongo Db优化查询
db.values.distinct('variable',{variable:{$in:variableIds}, $nor:[{source: {$exists: false}},{source: {$size: 0}}]})
其中变量如下面的 “值” 收藏的外键。 variableIds
是作为变量集合的主键的唯一标识列表。
{
"_id" : ObjectId("546db048e4b0c0187ab9eefd"),
"dateNum" : 41274,
"source" : [
{
"value" : 625,
"formatCode" : "General"
}
],
"variable" : ObjectId("546db048e4b0c0187ab9eefc")
}
但由于$in
条件3000个IDS查询似乎是大花更多的时间来执行有另一种方式来优化查询?
答
一个优化是将variable
字段添加到index
。
只是为了证明其效果。让值是一个只有三个文档的集合,其中两个匹配我们正在寻找的variable
。
的样本数据:
db.values.insert([{
"_id" : 1,
"dateNum" : 41274,
"source" : [
{
"value" : 625,
"formatCode" : "General"
}
],
"variable" : 1
},
{
"_id" : 2,
"dateNum" : 41274,
"source" : [
{
"value" : 625,
"formatCode" : "General"
}
],
"variable" : 1
},
{
"_id" : 3,
"dateNum" : 41274,
"source" : [
{
"value" : 625,
"formatCode" : "General"
}
],
"variable" : 2
}
])
假设我们运行在这个集合的查询,而无需对变量字段的索引。
db.runCommand({ distinct: 'sample',
key:'variable',
query:{variable:{$in:[1]},
$nor:[{source: {$exists: false}},{source: {$size: 0}}]}
})
我们得到下面的输出。在检查输出时,我们发现集合中扫描的文档总数nscannedObjects
的值为3
。所以这导致了完整的收集扫描。
{
"values" : [
1
],
"stats" : {
"n" : 2,
"nscanned" : 3,
"nscannedObjects" : 3,
"timems" : 0,
"cursor" : "BasicCursor"
},
"ok" : 1
}
现在我们在variable
字段中添加一个索引。
db.sample.ensureIndex({"variable":1})
而上运行的命令,我们得到以下输出,指出扫描的总文档仅2
。这些文档具有与搜索查询中相同的确切variable
。
{
"values" : [
1
],
"stats" : {
"n" : 2,
"nscanned" : 2,
"nscannedObjects" : 2,
"timems" : 59,
"cursor" : "BtreeCursor variable_1"
},
"ok" : 1
}
我已经添加了指数也显示了同样的结果,我也搜索在$传球超过3000个项目中会造成任何性能问题,也没有任何其他替代查询除了使用不同 – 2014-11-24 08:59:39
指数领域'variable'和'source.value'。然后尝试下面的查询:db.sample.distinct('variable',{variable:{$ in:[1]},“source.value”:{$ exists:true}}) – BatScream 2014-11-24 09:11:22