2011-10-21 23 views

Trả lời

40

Chắc chắn là có thể. db.collection.stats().indexSizes là một tài liệu nhúng trong đó mỗi tên chỉ là một chìa khóa và giá trị là tổng kích thước chỉ mục trong byte:

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

Đây là một kịch bản dễ dàng để tìm hiểu các chỉ số mà chiếm nhiều dung lượng nhất trong toàn bộ của bạn cơ sở dữ liệu:

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

Listing xuống kích thước của chỉ số cho mỗi tập hợp trong một cụ db chúng ta có thể sử dụng đoạn mã sau:

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

Bạn có thể thay đổi giá trị của các biến trong đoạn mã trên

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

để thay đổi trường sắp xếp và thứ tự.

Các vấn đề liên quan