我目前正在尝试将索引集成到我的一个集合中。我的所有测试都有效,我可以真正看到改进(扫描文档的数量等)但是我的一个结果有点奇怪(或者我可能不太了解)。 你能解释一下吗?
我有一个所有者/持有人/产品的集合(这是一个例子,并且是相同的文件):
{
"_id": ObjectId("59ef7c84f545c8278c8cd967"),
"owner": "59ef7c83f545c8278c8cd965",
"holder": "59ef7c84f545c8278c8cd967",
"product": 1,
"date" : ISODate("2017-10-24T17:46:44.367Z")
}
以下是索引:
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.relation"
},
{
"v" : 1,
"key" : {
"owner" : 1
},
"name" : "onlyOwner",
"ns" : "mydb.relation"
},
{
"v" : 1,
"key" : {
"holder" : 1
},
"name" : "onlyHolder",
"ns" : "mydb.relation"
},
{
"v" : 1,
"key" : {
"owner" : 1,
"holder" : 1
},
"name" : "ownerAndHolder",
"ns" : "mydb.relation"
},
{
"v" : 1,
"key" : {
"owner" : 1,
"product" : 1
},
"name" : "ownerAndProduct",
"ns" : "mydb.relation"
}
我执行了以下查询:
db.relation.find({ $and : [{"owner" : "59ef7c83f545c8278c8cd965"}, {"holder" : "59ef7c84f545c8278c8cd967"}]})
我的关系得到了很好的回报(前面提到过)。所以我决定检查executionStats以查看状态,结果如下:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "mydb.relation",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"holder" : {
"$eq" : "59ef7c84f545c8278c8cd967"
}
},
{
"owner" : {
"$eq" : "59ef7c83f545c8278c8cd965"
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"holder" : {
"$eq" : "59ef7c84f545c8278c8cd967"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"owner" : 1,
"product" : 1
},
"indexName" : "ownerAndProduct",
"isMultiKey" : false,
"multiKeyPaths" : {
"owner" : [ ],
"product" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"owner" : [
"[\"59ef7c83f545c8278c8cd965\", \"59ef7c83f545c8278c8cd965\"]"
],
"product" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"owner" : 1,
"holder" : 1
},
"indexName" : "ownerAndHolder",
"isMultiKey" : false,
"multiKeyPaths" : {
"owner" : [ ],
"holder" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"owner" : [
"[\"59ef7c83f545c8278c8cd965\", \"59ef7c83f545c8278c8cd
],
"holder" : [
"[\"59ef7c84f545c8278c8cd967\", \"59ef7c84f545c8278c8cd
]
}
}
},
{
"stage" : "FETCH",
"filter" : {
"holder" : {
"$eq" : "59ef7c84f545c8278c8cd967"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"owner" : 1
},
"indexName" : "onlyOwner",
"isMultiKey" : false,
"multiKeyPaths" : {
"owner" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"owner" : [
"[\"59ef7c83f545c8278c8cd965\", \"59ef7c83f545c8278c8cd
]
}
}
},
{
"stage" : "FETCH",
"filter" : {
"owner" : {
"$eq" : "59ef7c83f545c8278c8cd965"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"holder" : 1
},
"indexName" : "onlyHolder",
"isMultiKey" : false,
"multiKeyPaths" : {
"holder" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"holder" : [
"[\"59ef7c84f545c8278c8cd967\", \"59ef7c84f545c8278c8cd
]
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 1,
"totalDocsExamined" : 1,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"holder" : {
"$eq" : "59ef7c84f545c8278c8cd967"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 1,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"owner" : 1,
"product" : 1
},
"indexName" : "ownerAndProduct",
"isMultiKey" : false,
"multiKeyPaths" : {
"owner" : [ ],
"product" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"owner" : [
"[\"59ef7c83f545c8278c8cd965\", \"59ef7c83f545c8278c8cd965\"]"
],
"product" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 1,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "NCEL15605",
"port" : 27017,
"version" : "3.4.6",
"gitVersion" : "c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5"
},
"ok" : 1
}
所以我理解只有Holder onlyOwner被拒绝但我不明白为什么ownerAndHolder被拒绝而且ownerAndProduct被使用,因为我没有将它用作变量。
有人可以帮助我理解这个结果(从查找中回复是正确的,但这是MongoDB的选择,我不明白)?
提前致谢
此致
答案 0 :(得分:1)
看看这两个字段:
"nReturned" : 1,
"totalDocsExamined" : 1,
这意味着Mongo必须只检查一个文档才能返回一个。然后,所选择的查询计划虽然反直觉,却可以获得最佳结果。看起来至少有一个其他计划可以达到相同的结果(所有者持有人索引)。在这种情况下,Mongo可以自由选择计划。
如果您想了解查询优化的详细信息,请阅读此material。