28

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

Community
  • 1
  • 1
Global Warrior
  • 5,050
  • 9
  • 45
  • 75

1 Answers1

24

nscanned and nscannedObjects report results for the winning query plan.

nscannedAllPlans and nscannedObjectsAllPlans report results for all plans.

Doc

Number of index entries scanned. totalKeysExamined corresponds to the nscanned field returned by cursor.explain() in earlier versions of MongoDB.

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 } );

// Older mongodb (< 3.0? )
t.find( { a:{ $gte:0 }, b:{ $gte:0 } } ).explain( true );
    {
      "isMultiKey": false,
      "n": 2,
      "nscannedObjects": 2,
      "nscanned": 2,
      "nscannedObjectsAllPlans": 6,
      "nscannedAllPlans": 6,
      "scanAndOrder": false,
      "indexOnly": false,
      "nYields": 0,
      "nChunkSkips": 0,
      "millis": 2,
    ...
    }

// MongoDB 4.4
t.find( { a:{ $gte:0 }, b:{ $gte:0 } } ).explain( true );
{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.jstests_explainb",
        ...
        "queryHash" : "CB67518C",
        "planCacheKey" : "5E76CDD1",
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "a" : 1,
                    "b" : 1
                },
                "indexName" : "a_1_b_1",
            }
        },
        "rejectedPlans" : [
            {
                "stage" : "FETCH",
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "b" : 1,
                        "a" : 1
                    },
                    "indexName" : "b_1_a_1",
                }
            }
        ],
        ...
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 2,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 2, // <-- same as `nscanned`
        "totalDocsExamined" : 2, // <--
        "executionStages" : { ... }
        "allPlansExecution" : [
            {...},
            {...}
        ]
    }

Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
  • So should I worry if nscannedObjectsAllPlans, nscannedAllPlans are giving large value while the nscanned is still in the acceptable range? – Global Warrior Sep 20 '12 at 16:39
  • I don't think so - I believe it's only when explain() is called that it performs those other plans in the first place. – Eric Mill Nov 21 '12 at 05:37
  • 4
    Running multiple plans is basically how MongoDB decides which query plan it should use. It executes all the plans it could use in parallel the first time the query is run, then periodically after a number of queries or changes to the data. As soon as one finishes, it caches it as the plan to use, cancels the others, and will use that plan until the next time it decides to refresh it. – David Mason Sep 09 '13 at 04:36
  • @DavidMason the query optimizer decides which plan to use based on which plan returns 101 documents the fastest. See here http://docs.mongodb.org/manual/core/query-plans/ – Gianfranco P. Sep 09 '13 at 11:53