How do I check if an index is being used

后端 未结 2 1082
梦如初夏
梦如初夏 2020-12-08 20:10

I have a mongodb replica set with a lot of databases, collections & indexes.

We did a lot of refactor and optimization and, of course, I have a lot of \"creative

相关标签:
2条回答
  • 2020-12-08 20:50

    The simplest solution to this is to use the mongodb inbuilt $indexStats

    Using the Mongo console run -

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

    Using PyMongo -

    from pymongo import MongoClient
    collection = MongoClient()[db_name][collection_name]
    index_stats = collection.aggregate([{'$indexStats':{}}])
    
    for index_info in index_stats:
        print index_info
    

    Apologies for re-opening an old question. This shows up on the first page of google searches and the only answer is to use a snippet of unmaintained code.

    0 讨论(0)
  • 2020-12-08 21:00

    There is a pretty cool script out on Github that you should look at:

    https://github.com/wfreeman/indexalizer

    Basically it involves turning on profiling for your database and then it will use the data collected by the profiler to drive explain() calls. It then tells you both which indexes are not being used and which queries are not using indexes. Pretty slick.

    More about mongoDB database profiling:

    http://docs.mongodb.org/manual/reference/database-profiler/

    0 讨论(0)
提交回复
热议问题