我有一个拥有大量数据库,集合和索引的mongodb副本集.
我们做了很多重构和优化,当然,我有很多来自消费者的"创意查询".
我想清理未使用的索引.只是想节省一些空间.
如何检查是否正在使用索引?我可以负担得起索引索引并删除未使用的索引.
在所有可能的查询中运行"解释"不是一个选项:)
编辑:基于接受的答案的解决方案
脚本被窃听了.我不是一个JavaScript专家,但我把更正后的脚本.我希望对某人有用:
DB.prototype.indexStats = function() {
  var queries = [];
  var collections = db.getCollectionNames();
  var findQuery = function(q) {
    for(entryIdx in queries) {
      if(q == queries[entryIdx].query) {
        return entryIdx;
      }
    }
    return -1;
  }
  for(cIdx in collections) {
    var cName = collections[cIdx];
    var nsName = db.getName()+"."+cName;
    if(cName.indexOf("system") == -1) {
      var i = 1;
      var count = db.system.profile.count({ns:nsName});
      print('scanning profile {ns:"'+nsName+'"} with '+count+' records... this could take a while...');
      db.system.profile.find({ns:nsName}).addOption(16).batchSize(10000).forEach(function(profileDoc) {           
        if(profileDoc.query && !profileDoc.query["$explain"]) { …mongodb ×1