Categories: MongoDBNoSQL

MongoDB – Evaluate Query Performance using Indexes

This blog represents commands used to manage MongoDB indexes on a particular collection and tips on how to evaluate query performance with or without indexes. Please feel free to suggest. Sorry for typos.

Why create indexes?

Indexes can significantly improve the read query performance for MongoDB collections. In absence of indexes, when searching for documents based on filter criteria, MongoDB performs collection scan, i.e., scan every document and returns the documents matching the filter criteria. This is not very efficient way of searching the document. In case of one or more fields which are frequently used for filtering out the document, it is recommended to create indexes on those fields. MongoDB, thus, limits the number of document which is scanned in presence of indexes. This results in faster queries’ execution time owing to less number of documents being scanned.

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

Different kind of indexes?

Following are different kind of indexes which can be created on one or more fields of a MongoDB collection. Details can be found 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 string
  • Hashed index: Index created for supporting 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();

Command to examine query performance based on indexes inclusion/exclusion

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

Following are three keys whose value you would want to watch out for in the output of above command execution.

  • totalKeysExamined: Total number of index entries scanned
  • totalDocsExamined: Total number of documents scanned to find the results.
  • executionTimeMillis: Time required to excute the query
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar
Tags: mongodbnosql

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

2 months ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

2 months ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

3 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

3 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

3 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

3 months ago