Understanding mongo db explain

2019-04-17 22:24发布

问题:

I fired a query and tried to explain it on mongo console and got

"isMultiKey" : true,
"n" : 8,
"nscannedObjects" : 17272,
"nscanned" : 17272,
"nscannedObjectsAllPlans" : 21836,
"nscannedAllPlans" : 21836,
"scanAndOrder" : true,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 184,

Most of the things are explained in http://www.mongodb.org/display/DOCS/Explain, but I cannot understand what does nscannedObjectsAllPlans, nscannedAllPlans means. Can anyone help?

Thanks

回答1:

nscanned and nscannedObjects report results for the winning plan.

nscannedAllPlans and nscannedObjectsAllPlans report results for all plans

For example:

>t = db.jstests_explainb;
>t.drop();

>t.ensureIndex( { a:1, b:1 } );
>t.ensureIndex( { b:1, a:1 } );

>t.save( { a:0, b:1 } );
>t.save( { a:1, b:0 } );

>t.find( { a:{ $gte:0 }, b:{ $gte:0 } } ).explain( true );
{
  "cursor": "BtreeCursor a_1_b_1",
  "isMultiKey": false,
  "n": 2,
  "nscannedObjects": 2,
  "nscanned": 2,
  "nscannedObjectsAllPlans": 6,
  "nscannedAllPlans": 6,
  "scanAndOrder": false,
  "indexOnly": false,
  "nYields": 0,
  "nChunkSkips": 0,
  "millis": 2,
...
}