Last active
May 28, 2018 11:59
-
-
Save zizkebab/80078e0da269801358d37dc036f3b728 to your computer and use it in GitHub Desktop.
Find index usage accross shards and display non used ones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
databases = db.adminCommand( { listDatabases: 1 } ).databases; | |
var deleteCandidates = [] | |
databases.forEach( | |
function(database){ | |
if ((database.name != "admin") && (database.name != "config") && (database.name != "test")){ | |
print ("Connecting to: " + database.name); | |
conn = new Mongo(); | |
db = conn.getDB(database.name); | |
db.getCollectionNames().forEach(function(collection){ | |
var indexUsageCounter = {}; | |
print("Checking collection: " + collection); | |
indexStatsCursor = db.getCollection(collection).aggregate([{$indexStats:{}}]); | |
mappedStats = indexStatsCursor.map( function(stat) { return { name: stat.name, ops: stat.accesses.ops } ; } ); | |
mappedStats.forEach(function(stat){ | |
if (indexUsageCounter[stat.name] == undefined) { | |
indexUsageCounter[stat.name] = stat.ops; | |
} else { | |
indexUsageCounter[stat.name] = indexUsageCounter[stat.name] + stat.ops; | |
} | |
}); | |
print(tojson(indexUsageCounter)); | |
var items = Object.keys(indexUsageCounter).map(function(key) {return [key, indexUsageCounter[key]];}); | |
items.forEach(function(usage){ | |
if(usage[1] == 0){ | |
print(usage); | |
deleteCandidates.push(database.name + '.' + collection + '.' + usage[0]); | |
} | |
}); | |
}); | |
} | |
} | |
); | |
print(tojson(deleteCandidates)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment