赞
踩
mongodb磁盘碎片产生
一、整理数据碎片的方法
二、查看集合大小等信息
// 查看集合大小
db.collection_name.storageSize()
// 查看的集合索引大小
db.collection_name.totalIndexSize()
// 查看预计回收内存
db.collection_name.stats().wiredTiger["block-manager"]["file bytes available for reuse"]
三、使用compact命令回收
db.runCommand({compact:<collection name>,force:<boolen>})
// primary节点上执行
rs1:PRIMARY> db.runCommand({compact:"test"})
{
"ok" : 0,
"errmsg" : "will not run compact on an active replica set primary as this is a slow blocking operation. use force:true to force",
"operationTime" : Timestamp(1643269045, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1643269045, 1),
"signature" : {
"hash" : BinData(0,"o/GQKZEvC/Tp9T1QQePHdyT0xKo="),
"keyId" : NumberLong("7057052573554966530")
}
}
}
rs1:PRIMARY> db.runCommand({compact:"test",force:true})
{
"operationTime" : Timestamp(1643269055, 1),
"ok" : 0,
"errmsg" : "collection does not exist",
"code" : 26,
"codeName" : "NamespaceNotFound",
"$clusterTime" : {
"clusterTime" : Timestamp(1643269055, 1),
"signature" : {
"hash" : BinData(0,"Srm0HtbcI/6BZv6WXCZeXzdvBLA="),
"keyId" : NumberLong("7057052573554966530")
}
}
}
// secondary节点上执行
rs1:SECONDARY> db.runCommand({compact:"test"})
{
"ok" : 1,
"operationTime" : Timestamp(1643269025, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1643269025, 1),
"signature" : {
"hash" : BinData(0,"27nvRIvAmDLzwpO2bKxQ4Z3KuBw="),
"keyId" : NumberLong("7057052573554966530")
}
}
}
四、创建新的secondary回收空间
五、重建collection
// 创建新集合
db.createCollection("collection_name_new")
// 手动重建索引
db.collection_name_new.createIndex({"col_name":1},{"background":1})
// 从旧集合复制到新集合
db.collection_name_new.aggregate({$match:{}},{$out:"collection_name_old"})
// 重新命名集合
db.runCommand( { renameCollection: ".collection_name_new", to: ".collection_name" })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。