MongoDB Analyzing Queries - MongoDB

How to analyze queries in MongoDB?

Analyzing queries is the major aspect to measure how effective the database and indexing design is. We will explain you about the frequently used $explain and $hint queries.

Using $explain

$explain operator will provide information on the query, indexes used in a query and other statistics. It is very useful while analyzing how well your indexes are optimized.

In the last chapter, we created an index for the users collection on fields gender and user_name using the following query

We are using $explain on the following query

Above explain() query will return below analyzed result

Let’s have a look at the fields in this result set

  • True value of indexOnly will indicate that this query has used indexing.
  • Cursor field will specify the type of cursor used. BTreeCursor type will indicate that an index was used and will also give the name of the index used. BasicCursor will indicate that a full scan was made without using any indexes.
  • n will indicate the number of documents matching returned.
  • nscannedObjects will indicate the total number of documents scanned.
  • nscanned will indicate the total number of documents or index entries scanned.

Using $hint

$hint operator will force the query optimizer to use the specified index to run a query. This is mainly useful when you wish to test the performance of a query with various indexes. For example, below query will specify the index on fields gender and user_name to be used for this query

To analyze the above query using $explain

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

MongoDB Topics