2011-10-21 13 views

Odpowiedz

40

Z pewnością może. db.collection.stats().indexSizes jest osadzony dokument, gdzie każda nazwa indeksu jest kluczem, a wartość to łączna wielkość indeksu w bajtach:

> db.test.stats() 
{ 
     "ns" : "test.test", 
     <snip> 
     "indexSizes" : { 
       "_id_" : 137904592, 
       "a_1" : 106925728 
     }, 
     "ok" : 1 
} 
6

Jest to prosty skrypt, aby dowiedzieć się indeksy, które zajmują najwięcej miejsca w swojej całą w bazie:

var indexesArr = {} 
db.getCollectionNames().forEach(function(collection) { 
    indexes = db[collection].stats().indexSizes 
    for (i in indexes) indexesArr[collection + " - " + i] = indexes[i]; 
}); 

var sortable = [], x; 
for (x in indexesArr) sortable.push([x, indexesArr[x]]) 
var pArr = sortable.sort(function(a, b) {return b[1] - a[1]}) 
for (x in pArr) print(pArr[x][1] + ": " + pArr[x][0]); 
1

listingu dół wielkości indeksów na gromadzenie w specyficzny db możemy użyć następującego fragmentu kodu:

use mydb; 

var collectionStats = [] 

// Get the sizes 
db.getCollectionNames().forEach(function (collectionName) { 
    var collection = db.getCollection(collectionName) 
    var collectionIndexSize = collection.totalIndexSize(); 
    var indexSizeInMB = collectionIndexSize/(1024*1024) 
    collectionStats.push({"collection": collectionName, "indexSizeInMB": indexSizeInMB}) 
}); 

// Sort on collection name or index size 
var reverse = true; 
var sortField = "indexSizeInMB"; 
collectionStats.sort(function (a, b) { 
    comparison = a[sortField] - b[sortField]; 
    if (isNaN(comparison)) comparison = a.collection.localeCompare(b.collection); 
    if (reverse) comparison *= -1; 
    return comparison; 
});undefined; 

// Print the collection stats 
collectionStats.forEach(function (collection) { 
    print(JSON.stringify(collection)); 
}); 

// Total size of indexes 
print("Total size of indexes: " + db.stats()["indexSize"]/(1024*1024) + " MB"); 

Można zmienić wartości zmiennych we fragmencie powyżej

var reverse = true; 
var sortField = "indexSizeInMB"; 

aby zmienić pole sortowania i porządek.

Powiązane problemy