2015-05-06 76 views
2

我有一個集合搭配指數:MongoDB的解釋查詢計劃

{ 
    "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個文檔)。

回答

2

您似乎使用的是舊版本的MongoDB,從3.0開始的explain()輸出看起來不一樣 - 只是放在一邊,因爲您沒有提及版本號。

您的查詢相匹配你的所有22個對象:

"n" : 22, 

所以是的,它是一個完整的掃描。如果您希望掃描的對象較少,則必須使用少於全部匹配的查詢。

文檔的 「N」 的含義是: http://docs.mongodb.org/v2.2/reference/explain/#explain-output-fields-core

explain.n

n是一個數字,反映的符合查詢選擇標準的文件數量。