开发者社区 > 数据库 > 正文

MongoDB 磁盘碎片率如何计算

MongoDB 磁盘碎片率如何计算

展开
收起
一人吃饱,全家不饿 2021-01-08 17:19:24 1124 0
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
    赞同 展开评论 打赏

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

相关电子书

更多
开源数据库 MongoDB 专场 MongoDB疑难杂症分析及优化 立即下载
阿里云MongoDB云服务构建 立即下载
饿了么高级架构师陈东明:MongoDB是如何逐步提高可靠性的 立即下载