MongoDB 磁盘碎片率如何计算

MongoDB 磁盘碎片率如何计算

展开
收起
一人吃饱,全家不饿 2021-01-08 17:19:24 1209 分享 版权
1 条回答
写回答
取消 提交回答
  • # 一次性查看所有collection 碎片率
    function getCollectionDiskSpaceFragRatio(dbname, coll) {
        var res = db.getSiblingDB(dbname).runCommand({
            collStats: coll
        });
        var totalStorageUnusedSize = 0;
        var totalStorageSize = res['storageSize'] + res['totalIndexSize'];
        Object.keys(res.indexDetails).forEach(function(key) {
            var size = res['indexDetails'][key]['block-manager']['file bytes available for reuse'];
            print("index table " + key + " unused size: " + size);
            totalStorageUnusedSize += size;
        });
        var size = res['wiredTiger']['block-manager']['file bytes available for reuse'];
        print("collection table " + coll + " unused size: " + size);
        totalStorageUnusedSize += size;
        print("collection and index table total unused size: " + totalStorageUnusedSize);
        print("collection and index table total file size: " + totalStorageSize);
        print("Fragmentation ratio: " + ((totalStorageUnusedSize * 100.0) / totalStorageSize).toFixed(2) + "%");
    }
    use xxxdb
    db.getCollectionNames().forEach((c) => {print("\n\n" + c); getCollectionDiskSpaceFragRatio(db.getName(), c);});
    
    2021-01-08 17:19:34
    赞同 展开评论

数据库领域前沿技术分享与交流

收录在圈子:
+ 订阅
让用户数据永远在线,让数据无缝的自由流动
还有其他疑问?
咨询AI助理