MongoDB解释查询计划

时间:2015-05-06 06:40:53

标签: mongodb mongodb-query

我有一个索引的集合:

{
    "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个文件)。

1 个答案:

答案 0 :(得分:2)

您似乎使用的是较早版本的MongoDB,从3.0开始的function checkGPS(inputGPS) { var regex = new RegExp("^[0-9].[0-9], [0-9].[0-9]$", "i"); var formatGPS = inputGPS.value; if ((formatGPS.length > 0) && !regex.test(formatGPS)) { alert("The format is always as follows: numbers.numbers, numbers.numbers (With numbers=any length)"); } } 输出看起来会有所不同 - 只是旁边,因为您没有提及版本号。

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

explain()

所以是的,这是一次完整扫描。如果您想要扫描的对象少于您必须使用匹配少于全部的查询。

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

  

"n" : 22,

     

n是一个数字,反映了与查询选择标准匹配的文档数。