Explain
- MongoDB
Explain shows how the server runs your query: which indexes it uses, how many keys and documents it
examined, how many it returned and how long it took. The plan is drawn as a tree of stages. A
collection scan (COLLSCAN) is flagged, so a query that reads every document stands out.
Explain a query
Section titled “Explain a query”- Set up the query in the collection view, or the pipeline in the aggregation editor.
- Choose Explain to run the query on the server and measure it, or Plan only to see the chosen plan without running it.
- The Explain view opens with the result. In the collection view, its icon sits with Tree, Table and JSON at the right end of the status bar, so you can switch back to the documents and return to the plan.
Read the result
Section titled “Read the result”The top of the view says either:
- “COLLSCAN: the query reads every document of the collection. An index on the filtered or sorted fields avoids that.” The collection scan stage is highlighted in the plan too.
- “Uses index …” with the names of the indexes the plan uses.
Below that are four figures:
| Figure | Meaning |
|---|---|
| Returned | Documents the query returned. |
| Keys examined | Index keys the server read. |
| Docs examined | Documents the server read. |
| Time | Execution time in milliseconds. |
With Plan only the query was not run, so there are no counts.
The plan is a tree of stages, such as IXSCAN or COLLSCAN. Each stage shows its index, the
documents it returned and its time, with details such as keysExamined, docsExamined,
keyPattern, filter, sortPattern and direction. Raw explain output shows the server’s
full answer as JSON.
Fix a collection scan
Section titled “Fix a collection scan”If the plan shows COLLSCAN for a query you run often, create an index on the fields the query
filters or sorts on in the index manager, then choose Explain again.
The plan then shows an IXSCAN stage on the new index.
Related
Section titled “Related”Documents Querybara 0.1.1 · built frombc9f5aa