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
Latest posts by Ajitesh Kumar (see all)
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

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

2 weeks ago

Agentic AI Design Patterns Examples

In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…

2 weeks ago

List of Agentic AI Resources, Papers, Courses

In this blog, I aim to provide a comprehensive list of valuable resources for learning…

2 weeks ago

Understanding FAR, FRR, and EER in Auth Systems

Have you ever wondered how systems determine whether to grant or deny access, and how…

3 weeks ago

Top 10 Gartner Technology Trends for 2025

What revolutionary technologies and industries will define the future of business in 2025? As we…

3 weeks ago

OpenAI GPT Models in 2024: What’s in it for Data Scientists

For data scientists and machine learning researchers, 2024 has been a landmark year in AI…

3 weeks ago