gpt4 book ai didi

MongoDB解释查询计划

转载 作者:可可西里 更新时间:2023-11-01 10:28:50 25 4
gpt4 key购买 nike

我有一个带有索引的集合:

{
"authorizations.participant.participantId" : 1,
"authorizations.action" : 1
}

我有一个问题:

db.users.find({
"$query" : {
"$and" : [
{
"authorizations" : {
"$elemMatch" : {
"action" : "READ" ,
"participant.participantId" : {
"$in": ["5549b40444ae1e4a5764fb0a","5549b3f644ae1e4a5764facb"]
}
}
}
}
]
},
"$explain" : true
})

我试图了解查询执行计划:

{
"cursor" : "BtreeCursor authorizations",
"isMultiKey" : true,
"n" : 22,
"nscannedObjects" : 22,
"nscanned" : 23,
"nscannedObjectsAllPlans" : 22,
"nscannedAllPlans" : 23,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"authorizations.participant.participantId" : [
[
"5549b3f644ae1e4a5764facb",
"5549b3f644ae1e4a5764facb"
],
[
"5549b40444ae1e4a5764fb0a",
"5549b40444ae1e4a5764fb0a"
]
],
"authorizations.action" : [
[
"READ",
"READ"
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor authorizations",
"isMultiKey" : true,
"n" : 22,
"nscannedObjects" : 22,
"nscanned" : 23,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"authorizations.participant.participantId" : [
[
"5549b3f644ae1e4a5764facb",
"5549b3f644ae1e4a5764facb"
],
[
"5549b40444ae1e4a5764fb0a",
"5549b40444ae1e4a5764fb0a"
]
],
"authorizations.action" : [
[
"READ",
"READ"
]
]
}
}
],
"server" : "modi:27017",
"filterSet" : false,
"stats" : {
"type" : "KEEP_MUTATIONS",
"works" : 23,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 22,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"children" : [
{
"type" : "FETCH",
"works" : 23,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 22,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 22,
"children" : [
{
"type" : "IXSCAN",
"works" : 23,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 22,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ authorizations.participant.participantId: 1, authorizations.action: 1 }",
"isMultiKey" : 1,
"boundsVerbose" : "field #0['authorizations.participant.participantId']: [\"5549b3f644ae1e4a5764facb\", \"5549b3f644ae1e4a5764facb\"], [\"5549b40444ae1e4a5764fb0a\", \"5549b40444ae1e4a5764fb0a\"], field #1['authorizations.action']: [\"READ\", \"READ\"]",
"yieldMovedCursor" : 0,
"dupsTested" : 22,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 23,
"children" : []
}
]
}
]
}
}

据我所知,虽然我可以 "cursor": "BtreeCursor authorizations",但 "nscannedObjects"=22 不是说有一个完整的集合扫描吗? (该集合中有 22 个文档)。

最佳答案

您似乎使用的是旧版本的 MongoDB,从 3.0 开始的 explain() 输出看起来会有所不同 --- 顺便提一下,因为您没有提及版本号。

您的查询匹配了您所有的 22 个对象:

"n" : 22,

所以是的,这是一个完整的扫描。如果您想要扫描的对象少于您将不得不使用匹配少于所有的查询。

“n”含义的文档: http://docs.mongodb.org/v2.2/reference/explain/#explain-output-fields-core

explain.n

n is a number that reflects the number of documents that match the query selection criteria.

关于MongoDB解释查询计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30069325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com