Last active
December 21, 2015 10:39
-
-
Save zhengkai/6293362 to your computer and use it in GitHub Desktop.
统计 mongo 各表的 size,籍此判断是否需要做碎片整理 / 索引使用是否合理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 统计 mongo 各表的 size | |
* | |
* zhengkai@gmail.com | |
* | |
* usage: | |
* > mongo DB_NAME mongo_db_size.js | |
*/ | |
var lCollName = []; | |
var iMaxLength = 0; | |
var iTab = 12; | |
var lReturn = db.getCollectionNames(); | |
for (var i in lReturn) { | |
var sName = lReturn[i]; | |
if (sName == 'system.indexes') { | |
continue; | |
} | |
if (iMaxLength < sName.length) { | |
iMaxLength = sName.length; | |
} | |
lCollName.push(sName); | |
} | |
function show(sValue, iMaxLength, bLeft) { | |
if (!bLeft) { | |
bLeft = false; | |
} | |
sValue = sValue + ''; | |
var sReturn = ''; | |
for (var i = sValue.length; i < iMaxLength; i++) { | |
sReturn += ' '; | |
} | |
if (bLeft) { | |
sReturn = sValue + sReturn; | |
} else { | |
sReturn += sValue; | |
} | |
return sReturn; | |
} | |
var sHead = show('table:', iMaxLength + 3, true); | |
sHead += show('size: ', iTab); | |
sHead += show('index: ', iTab); | |
sHead += show('stroage: ', iTab); | |
sHead += show('s/s: ', iTab); | |
sHead += show('i/s: ', iTab); | |
print(); | |
print(sHead); | |
print(); | |
var aSum = { | |
'size': 0, | |
'index': 0, | |
'storage': 0, | |
}; | |
var sOutput = ''; | |
for (var i in lCollName) { | |
var sName = lCollName[i]; | |
var aStat = db.getCollection(sName).stats(); | |
if (!aStat.size) { | |
continue; | |
} | |
sOutput = show(sName, iMaxLength + 3, true); | |
sOutput += show((parseInt(aStat.size) / 1048576).toFixed(1) + ' MB', iTab); | |
sOutput += show((parseInt(aStat.totalIndexSize) / 1048576).toFixed(1) + ' MB', iTab); | |
sOutput += show((parseInt(aStat.storageSize) / 1048576).toFixed(1) + ' MB', iTab); | |
sOutput += show(((aStat.storageSize / aStat.size - 1) * 100).toFixed(2) + ' %', iTab); | |
sOutput += show((aStat.totalIndexSize / aStat.size * 100).toFixed(2) + ' %', iTab); | |
aSum['size'] += aStat.size; | |
aSum['index'] += aStat.totalIndexSize; | |
aSum['storage'] += aStat.storageSize; | |
print(sOutput); | |
} | |
print(); | |
print(sHead); | |
print(); | |
sOutput = show('SUM', iMaxLength + 3, true); | |
sOutput += show((parseInt(aSum.size) / 1048576).toFixed(1) + ' MB', iTab); | |
sOutput += show((parseInt(aSum.index) / 1048576).toFixed(1) + ' MB', iTab); | |
sOutput += show((parseInt(aSum.storage) / 1048576).toFixed(1) + ' MB', iTab); | |
sOutput += show(((aSum.storage / aSum.size - 1) * 100).toFixed(2) + ' %', iTab); | |
sOutput += show((aSum.index / aSum.size * 100).toFixed(2) + ' %', iTab); | |
print(sOutput); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment