Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MongoDB: Evaluate Query Performance Using Indexes

DZone's Guide to

MongoDB: Evaluate Query Performance Using Indexes

Learn how to manage your MongoDB indexes on any given collection and get some tips for evaluating query performance whether or not indexes are present.

· Database Zone ·
Free Resource

Slow SQL Server? These SentryOne resources share tips and tricks for not only troubleshooting SQL Server performance issues, but also preventing them before they hit your production environment.

This blog shows commands that you can use to manage MongoDB indexes on a particular collection, as well as tips on how to evaluate query performance with or without indexes. 

Why Create Indexes?

Indexes can significantly improve read query performance for MongoDB collections. In the absence of indexes, when searching for documents based on filter criteria, MongoDB performs a collection scan in which it scans every document and returns the documents matching the filter criteria. This is not a very efficient way of searching the document. For example, if one or more fields are frequently used for filtering out the document, it is recommended to create indexes on those fields. MongoDB thus limits the number of documents that are scanned when indexes are present. When there are fewer documents being scanned, there is faster query execution time.

It should be noted that MongoDB creates a unique index on the _id  field during the creation of a collection.

Different Kinds of Indexes

Following are different kinds of indexes that can be created in one or more fields of a MongoDB collection. Details can be found in the documentation on MongoDB Indexes.

  • Single field: Index created on a single field.
    db.collectionName.createIndex({"field1": 1}); 
  • Compound index: Index created on multiple fields.
    db.collectionName.createIndex({"field1": 1, "field2": 1}); 
  • Multikey index: Index created on sub-documents.
    db.collectionName.createIndex({"field1.subfield": 1}); 
  • Text index: Index created on a string.
  • Hashed index: Index created to support hash-based sharding.

Commands to Manage Indexes

  • Create index:
    db.collectionName.createIndex({“fieldName”: 1});
  • Remove index:
    db.collectionName.dropIndex({“fieldName”:1});
  • Get index information:
    db.collectionName.getIndexes();

Examine Query Performance Based on Indexes

Create (drop) indexes on one or more fields in a collection and execute the command given below to evaluate query performance with or without indexes.

db.collectionName.find({"fieldName": "some value"}).explain("executionStats"); 

Evaluate Query Performance Metrics

Here are three keys whose values you should watch out for in the output of the above command execution.

  • totalKeysExamined: The total number of index entries scanned.

  • totalDocsExamined: The total number of documents scanned to find the results.

  • executionTimeMillis: The time required to execute the query.

And that's it!

Database monitoring tools letting you down? See how SentryOne empowers Enterprises to go faster.

Topics:
database ,queries ,query performance ,indexes ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}