2011-10-21 27 views

Respuesta

40

Ciertamente, puede. db.collection.stats().indexSizes es un documento incrustado en donde cada nombre de índice es una clave y el valor es el tamaño del índice total en bytes:

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

Ésta es una escritura fácil averiguar los índices que ocupan la mayoría del espacio en su toda base de datos:

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

inmueble por el tamaño de índices por recogida en una específica db podemos utilizar el siguiente fragmento de código:

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"); 

Puede cambiar el valor de las variables en el fragmento anterior

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

para cambiar el campo de clasificación y el orden.

Cuestiones relacionadas