source

MongoDB에서 개별 인덱스의 크기를 어떻게 구합니까?

nicesource 2023. 5. 4. 20:02
반응형

MongoDB에서 개별 인덱스의 크기를 어떻게 구합니까?

사용할 수 있습니다.db.collection.totalIndexSize()전체 인덱스 크기를 가져오려면 개별 인덱스 크기를 확인해야 합니다.

지원되나요?

물론 가능합니다. db.collection.stats().indexSizes각 인덱스 이름이 키이고 값이 바이트 단위의 총 인덱스 크기인 내장 문서입니다.

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

전체 데이터베이스에서 가장 많은 공간을 차지하는 인덱스를 쉽게 찾을 수 있는 스크립트입니다.

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

특정 DB의 컬렉션당 인덱스 크기를 나열하면 다음 코드 조각을 사용할 수 있습니다.

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

위의 스니펫에서 변수 값을 변경할 수 있습니다.

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

정렬 필드 및 순서를 변경합니다.

TotalIndexSize() 함수에 다음과 같이 값을 추가합니다.

  • db.collection.totalIndexSize(1)

예를들면

db.test.totalIndexSize(1)

언급URL : https://stackoverflow.com/questions/7851563/how-do-you-get-the-size-of-an-individual-index-in-mongodb

반응형