At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

How To Find Unused Indexes In MongoDB

  • By Ken
  • December 7, 2018
  • 870 Views

Reason Why We should Remove The Unused Indexes In MongoDB First:

Handling MongoDB indexes over other application releases can be really challenging plot for the developers. On most of the case, the requirement for each release will get change so several new indexes will be included and old indexes may wind up. When the time passes by, it will be too hard for the developers to monitor which MongoDB Indexes are being utilized and which are not being utilized as of now.
If the relevant indexes are not updated, then the query will be slowly processed and as the result, the unused indexes will also become slow in responding to the queries. These need to be actively trimmed out to improve the database performance.
Indexes are doing a major role to increase the database performance. Whenever the changes occur in the collection then immediately the important indexes should be updated base on that DB performance increasing.
Therefore here am planning to explain the solutions to find out an unused index in MongoDB, and how to check usage statistics on the indexed collection, and how to drop indexes from the indexed collection.
In earlier versions of MongoDB, there was no such feature to identify whether an index was in an unused state or not. However, in version 3.2 MongoDB introduced a new feature to identify the unused indexes and added support for the “$indexStats” operator. Here, I am using MongoDB $indexStats to identify and remove unused indexes.

Finding Out Unused Indexes In MongoDB:

Step 1: To check for usage statistic of a particular index on a collection based on index name

$ db.collection.aggregate([{$indexStats: {}}, {$match: {"name": "<indexname>"}}])

 
Step 2: To get the statistics for all the indexes of a collection:

$ db.collection.aggregate([{$indexStats:{}}])

 
 

Output:

{
"name" : "name_-1",
"key" : {
"name" : -1
},
"host" : "ken-pc:27017",
"accesses" : {
"ops" : NumberLong(0),
"since" : ISODate("2018-12-07T07:45:44.233Z")
}
}
{
"name" : "_id_",
"key" : {
"_id" : 1
},
"host" : "ken-pc:27017",
"accesses" : {
"ops" : NumberLong(0),
"since" : ISODate("2018-12-07T07:39:01.697Z")
}
}

 
Note: Here, you have to note the two important fields:

   i) Ops:

This resembles the number of operations which had used the index.

   ii) Since:

This resembles the time from which MongoDB had started gathering stats, and its typically will be the last start time.

Related: Which Is Best? Aggregation or Map Reduce | MongoDb

 

How To Drop Unused Indexes In MongoDB:

Once the unused MongoDB indexes are identified and verified, you can drop the index using the given code:
Step 1: We can drop the unused index using the index name.

$ db.collection.dropIndex( "<index name>");

 
Step 2:  We can drop the unused index using the index key.

$ db.collection.dropIndex({ index_name: 1 })

 
Before using these commands, please verify whether the index you are about to drop is the right index or not.

Also Read: MongoDB Sharding Setup on Ubuntu 14.04

Finding out unused indexes is a regular task that every DB should do. Maintaining a proper indexing system is a critical part of a database performance. A single unindexed query is enough to down the total performance of an application so it is always mandatory to remove the unused indexes from MongoDB. 
Was it helpful? Read more from us here
Hire Highly trained MEAN STACK developers possesses who can efficiently transform your ideas into effective business.