Mongodb-未使用索引
我收集了大约4200万份文档。我在这个集合上有两个索引,如下所示:Mongodb-未使用索引,mongodb,indexing,Mongodb,Indexing,我收集了大约4200万份文档。我在这个集合上有两个索引,如下所示: { "0" : { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "ns.coll1" }, "1" : { "v" : 1, "unique" : true, "key" : { "id" : 1 },
{
"0" : {
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "ns.coll1"
},
"1" : {
"v" : 1,
"unique" : true,
"key" : {
"id" : 1
},
"name" : "id_1",
"ns" : "ns.coll1"
}
}
这就是id字段的外观:
"_id" : ObjectId("55f9b6548aefbce6b2fa2fac"),
"id" : {
"pid" : {
"f1" : "val1",
"f2" : "val2"
}
},
使用索引字段对此集合进行的非常简单的查询仍然不使用索引
db.coll1.find({"id.pid.f1":"val1","id.pid.f2":"val2"}).explain();
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 42635482,
"nscanned" : 42635482,
"nscannedObjectsAllPlans" : 42635482,
"nscannedAllPlans" : 42635482,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 333089,
"nChunkSkips" : 0,
"millis" : 51312,
"filterSet" : false,
"stats" : {
"type" : "COLLSCAN",
"works" : 42635484,
"yields" : 333089,
"unyields" : 333089,
"invalidates" : 0,
"advanced" : 1,
"needTime" : 42635482,
"needFetch" : 0,
"isEOF" : 1,
"docsTested" : 42635482,
"children" : []
}
}
我错过了什么?在这里进行全表扫描是最好的方法吗?这就是为什么不使用索引的原因吗?
谢谢。您必须在这样的嵌套字段上创建索引
db.index.createIndex({“id.pid.f1”:1,“id.pid.f2”:1})您的索引作为一个整体在对象上,而不是在子字段上。在id.pid.f1和id.pid.f2上需要一个复合索引。或者您可以搜索{id:{pid:{f1:“val1”,f2:“val2”}}谢谢。添加复合索引解决了此问题。谢谢。添加复合索引解决了这个问题。