Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any easy way to tell if mongodb indexes are still being used or not?

As code for my project is evolving, new indexes are being put in place and I am sure some old ones are no longer necessary. However before I just drop them to see if anything slows down or not, I'd prefer if there is a more programmatic or analytic way to determine if an index is being used any more.

I don't see anything in the system.indexes collection, but ideally there is some stats on index accesses somewhere! Is this the case?

like image 833
Aaron Silverman Avatar asked Sep 10 '12 16:09

Aaron Silverman


People also ask

How do you check if indexes are being used in MongoDB?

In MongoDB, you can use the cursor. explain() method or the db. collection. explain() method to determine whether or not a query uses an index.

Why index is not used in MongoDB?

Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement.

Where indexes are stored in MongoDB?

The index data is stored along with the collection data in the data directory of the MongoDB Server installation. You can also specify the data directory using the --dbpath storage option when you start the mongod.

Are MongoDB indexes strongly consistent?

Consistency. MongoDB is strongly consistent by default as all read/writes go to the primary in a MongoDB replica set, scaled across multiple partitions (shards). If desired, consistency requirements for read operations can be relaxed.


2 Answers

Run your db.collection.find(...) queries with .explain() tacked onto the end. That is the single most useful way to see how queries are running: http://www.mongodb.org/display/DOCS/Explain

You can also turn on profiling, and the profile log entries give you index usage for each query profiled. There aren't any aggregate stats, as of yet, though. In particular, it won't give you which indexes aren't being used, only the index that is being used (for each query). (Edit: I must have been imagining that--you have to get the query from the profiler and then run explain on it. Kind of painful manually.) http://www.mongodb.org/display/DOCS/Database+Profiler

Update: Just saw this from mongolab; looks like an interesting new project along these lines: http://blog.mongolab.com/2012/06/introducing-dex-the-index-bot/

Update: I created a script to check for indexes that aren't used, via the profiler: https://github.com/wfreeman/indexalizer

like image 138
Eve Freeman Avatar answered Nov 09 '22 15:11

Eve Freeman


Yes, agreed with other answer, using explain() to queries being executed should give us information, but with latest versions we can try something like :

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

Please check below links for more information :

How do I check if an index is being used

https://docs.mongodb.com/manual/reference/operator/aggregation/indexStats/#pipe._S_indexStats

like image 40
whoami - fakeFaceTrueSoul Avatar answered Nov 09 '22 15:11

whoami - fakeFaceTrueSoul