> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  drivers: shell, compass
-->

# Interpret Explain Plan Results

You can use explain results to determine the following information about a query:

- The amount of time a query took to complete

- Whether the query used an index

- The number of documents and index keys scanned to fulfill a query

**Note:**

Explain plan results for queries are subject to change between MongoDB versions.

### MongoDB Shell

The [`cursor.explain("executionStats")`](https://www.mongodb.com/docs/manual/reference/method/cursor.explain.md#mongodb-method-cursor.explain) and the [`db.collection.explain("executionStats")`](https://www.mongodb.com/docs/manual/reference/method/db.collection.explain.md#mongodb-method-db.collection.explain) methods provide statistics about the performance of a query. These statistics can be useful in measuring if and how a query uses an index. See [`db.collection.explain()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.explain.md#mongodb-method-db.collection.explain) for details.

## Evaluate the Performance of a Query

### MongoDB Shell

The examples on this page use data from the [sample\_mflix dataset](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix). For details on how to load this dataset into your self-managed MongoDB deployment, see [Load the Sample Dataset](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local). If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

### Query with No Index

### MongoDB Shell

The following query retrieves documents where the `year` field has a value between `2000` and `2005`, inclusive:

```javascript
db.movies.find( { year: { $gte: 2000, $lte: 2005 } } )

```

This query matches `3809` of the `21349` documents in the collection.

To view the query plan selected, chain the [`cursor.explain("executionStats")`](https://www.mongodb.com/docs/manual/reference/method/cursor.explain.md#mongodb-method-cursor.explain) cursor method to the end of the **find** command:

```javascript
db.movies.find(
   { year: { $gte: 2000, $lte: 2005 } }
).explain("executionStats")

```

**Output:**

```javascript
{
  queryPlanner: {
    namespace: 'sample_mflix.movies',
    winningPlan: {
      stage: 'COLLSCAN',
      '...': '...'
    },
    '...': '...'
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 3809,
    totalKeysExamined: 0,
    totalDocsExamined: 21349,
    executionStages: {
      stage: 'COLLSCAN',
      '...': '...'
    },
    '...': '...'
  },
  '...': '...'
}

```

- [`queryPlanner.winningPlan.stage`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.queryPlanner.winningPlan.stage) displays `COLLSCAN` to indicate a collection scan.

  Collection scans indicate that the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) had to scan the entire collection document by document to identify the results. This is a generally expensive operation and can result in slow queries.

- [`executionStats.nReturned`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.nReturned) displays `3809` to indicate that the winning query plan returns 3,809 documents.

- [`executionStats.totalKeysExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined) displays `0` to indicate that this query is not using an index.

- [`executionStats.totalDocsExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalDocsExamined) displays `21349` to indicate that MongoDB had to scan all 21,349 documents in the collection to find the 3,809 matching documents.

The difference between the number of matching documents and the number of examined documents may suggest that the query might benefit from the use of an index to improve efficiency.

### Query with Index

### MongoDB Shell

To support the query on the `year` field, add an index on the `year` field:

```javascript
db.movies.createIndex( { year: 1 } )

```

To view the query plan statistics, use the [`explain()`](https://www.mongodb.com/docs/manual/reference/method/cursor.explain.md#mongodb-method-cursor.explain) method:

```javascript
db.movies.find(
   { year: { $gte: 2000, $lte: 2005 } }
).explain("executionStats")

```

**Output:**

```javascript
{
  queryPlanner: {
    namespace: 'sample_mflix.movies',
    winningPlan: {
      stage: 'FETCH',
      inputStage: {
        stage: 'IXSCAN',
        keyPattern: {
          year: 1
        },
        '...': '...'
      },
      '...': '...'
    },
    '...': '...'
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 3809,
    totalKeysExamined: 3809,
    totalDocsExamined: 3809,
    '...': '...'
  },
  '...': '...'
}

```

- [`queryPlanner.winningPlan.inputStage.stage`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.queryPlanner.winningPlan.inputStage) displays `IXSCAN` to indicate index use.

- [`executionStats.nReturned`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.nReturned) displays `3809` to indicate that the winning query plan returns 3,809 documents.

- [`executionStats.totalKeysExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined) displays `3809` to indicate that MongoDB scanned 3,809 index entries. The number of keys examined match the number of documents returned, meaning that the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) only had to examine index keys to return the results. The [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) did not have to scan all of the documents, and only the 3,809 matching documents had to be pulled into memory. This results in a very efficient query.

- [`executionStats.totalDocsExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalDocsExamined) display `3809` to indicate that MongoDB scanned 3,809 documents.

Without the index, the query scans the entire collection to return the matching documents. The query also has to scan the entirety of each document, potentially pulling them into memory. This results in an expensive and potentially slow query operation.

When run with an index, the query scans only as many index entries and documents as it returns, resulting in a more efficient query.

### MongoDB Shell

**Compare Performance of Indexes**

To manually compare the performance of a query using more than one index, you can use the [`hint()`](https://www.mongodb.com/docs/manual/reference/method/cursor.hint.md#mongodb-method-cursor.hint) method in conjunction with the [`explain()`](https://www.mongodb.com/docs/manual/reference/method/cursor.explain.md#mongodb-method-cursor.explain) method.

Consider the following query:

```javascript
db.movies.find( {
   year: {
      $gte: 2000, $lte: 2005
   },
   rated: "PG"
} )

```

This query matches `235` documents.

To support the query, add a [compound index](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-compound.md#std-label-index-type-compound). With compound indexes, the order of the fields matter.

For example, add the following two compound indexes. The first index orders by `year` field first, and then the `rated` field. The second index orders by `rated` first, and then the `year` field.

```javascript
db.movies.createIndex( { year: 1, rated: 1 } )
db.movies.createIndex( { rated: 1, year: 1 } )

```

Evaluate the effect of the first index on the query:

```javascript
db.movies.find(
   { year: { $gte: 2000, $lte: 2005 }, rated: "PG" }
).hint( { year: 1, rated: 1 } ).explain("executionStats")

```

**Output:**

```javascript
{
  queryPlanner: {
    namespace: 'sample_mflix.movies',
    winningPlan: {
      stage: 'FETCH',
      inputStage: {
        stage: 'IXSCAN',
        keyPattern: {
          year: 1,
          rated: 1
        },
        '...': '...'
      },
      '...': '...'
    },
    '...': '...'
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 235,
    totalKeysExamined: 246,
    totalDocsExamined: 235,
    '...': '...'
  },
  '...': '...'
}

```

MongoDB scanned `246` index keys ([`executionStats.totalKeysExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined)) to return `235` matching documents ([`executionStats.nReturned`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.nReturned)).

Evaluate the effect of the second index on the query:

```javascript
db.movies.find(
   { year: { $gte: 2000, $lte: 2005 }, rated: "PG" }
).hint( { rated: 1, year: 1 } ).explain("executionStats")

```

**Output:**

```javascript
{
  queryPlanner: {
    namespace: 'sample_mflix.movies',
    winningPlan: {
      stage: 'FETCH',
      inputStage: {
        stage: 'IXSCAN',
        keyPattern: {
          rated: 1,
          year: 1
        },
        '...': '...'
      },
      '...': '...'
    },
    '...': '...'
  },
  executionStats: {
    executionSuccess: true,
    nReturned: 235,
    totalKeysExamined: 235,
    totalDocsExamined: 235,
    '...': '...'
  },
  '...': '...'
}

```

MongoDB scanned `235` index keys ([`executionStats.totalKeysExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined)) to return `235` matching documents ([`executionStats.nReturned`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.nReturned)).

The second compound index, `{ rated: 1, year: 1 }`, is therefore the more efficient index for supporting the example query, as the MongoDB server only needs to scan `235` [`index keys`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined) to find all matching documents using this index, compared to `246` when using the compound index `{ year: 1, rated: 1 }`.

**See also:**

- [Query Optimization](https://www.mongodb.com/docs/manual/core/query-optimization.md#std-label-read-operations-indexing)

- [Query Plans](https://www.mongodb.com/docs/manual/core/query-plans.md#std-label-read-operations-query-optimization)

- [Indexing Strategies](https://www.mongodb.com/docs/manual/applications/indexes.md#std-label-manual-indexing-strategies)
