How to know MongoDB’s Memory fragmentation ?

/*
* MongoDB fragmentation estimator, this is only a guide / example.
* THIS SCRIPT IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY
* To execute on a locally running mongod on default port (27017), run:
* mongo getMongoFragmentation.js > getMongoFragmentation.log
* To execute on a remote mongod or mongos, run:
** mongo <HOST>:<PORT>/admin -u <ADMIN_USER> getMongoFragmentation.js > getMongoFragmentation.log
** /

print(“– THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY –“);

currentNodeDetails = db.getSisterDB(“local”).startup_log.findOne();
currentHostname = currentNodeDetails[‘hostname’];
currentCommandLine = currentNodeDetails[‘cmdLine’];
currentHostport = currentCommandLine[‘port’];

if (currentHostport == undefined)
{
currentHostport = 27017;
}
hostDetails = currentHostname + “:” + currentHostport;
isMaster = db.isMaster();
if (isMaster.ismaster) {
print(“\n** Connected to primary: ” + hostDetails);
}
else if (isMaster.secondary) {
print(“\n** Connected to secondary: ” + hostDetails);
rs.slaveOk();
}
else {
print(“\n** Connected : ” + hostDetails);
}

replicaSetConfig = null;
try {
replicaSetConfig = db.getSisterDB(“local”).system.replset.findOne();
} catch(err) {
if (err == null) {
print(“\n** Standalone node:”);
}
else
{
print(err);
}
}

if ( replicaSetConfig != null )
{
print(“\n** Replica set config:”);
printjson(replicaSetConfig);
}

print (“\n** Checking for Fragmentation Started **”);
dbs = db.adminCommand(‘listDatabases’);
dbs[‘databases’].forEach(function(mydb) {
indb = db.getSiblingDB(mydb.name);

if (indb == ‘local’ || indb == ‘config’ || indb == ‘admin’) {}
else {
currentstats = (indb.stats(1024*1024));

SixGBsInMBs = 1*1024;
if (currentstats[‘dataSize’]<=SixGBsInMBs)
{
newstats = (indb.stats(1024*1024*1024));
print(“\nUNABLE to apply rules of thumb to the database ‘” + indb + “‘ (dataSize ~” + newstats[‘dataSize’] + “GB/~” + currentstats[‘dataSize’] + “MB) as it is smaller than 6GB.”);
}
else
{
print(“\n** Database: ” + indb + ” – Statistics listed in MB”);
print(“\ndataSize: ” + currentstats[‘dataSize’]);
print(“indexSize: ” + currentstats[‘indexSize’]);
print(“storageSize: ” + currentstats[‘storageSize’]);
print(“fileSize: ” + currentstats[‘fileSize’]);
lowerIndexSizeToDataSize = currentstats[‘dataSize’] / 4;
upperIndexSizeToData = currentstats[‘dataSize’];
if ( currentstats[‘indexSize’] >= lowerIndexSizeToDataSize && currentstats[‘indexSize’] < upperIndexSizeToData )
{
print(“\n–WARNING– Database: ” + indb + ” — Index size is between 25% to 100% of data size, REVIEW SCHEMA.”);
}
else if ( currentstats[‘indexSize’] >= upperIndexSizeToData )
{
print(“\n–WARNING– Database: ” + indb + ” — Index size is equal or larger than data size, REVIEW SCHEMA.”);
}
else
{
print(“\nPropotion of index to data size looks adequate for Database: ” + indb);
}
twiceDataSize = currentstats[‘dataSize’]*2;
if ( currentstats[‘storageSize’] >= twiceDataSize )
{
print(“\n–WARNING– Database: ” + indb + ” — Storage size is twice or greater than data size, POSSIBLE FRAGMENTATION.”);
}
storagePlusIndexSize = currentstats[‘storageSize’] + currentstats[‘indexSize’];
sevenFivePercentOfFileSize = (currentstats[‘fileSize’] / 4) * 3;
SevenGBsinMBs = 7168;
if ( storagePlusIndexSize <= sevenFivePercentOfFileSize )
{
print(“\n–WARNING– Database: ” + indb + ” — Storage size plus Index Size is less than 75% of File Size, POSSIBLE FRAGMENTATION.”);
}
else if ( storagePlusIndexSize >= (currentstats[‘fileSize’] + SevenGBsinMBs) )
{
print(“\n–WARNING– Database: ” + indb + ” — Storage size plus Index Size is greater than File Size plus 7GB, POSSIBLE FRAGMENTATION.”);
}
else
{
print(“\nPropotion of storage and index sizes to file size looks adequate for Database: ” + indb);
}
}
}
});
print (“\n** Checking for Fragmentation Completed **”);
Mongo

testig it :
==============

[usr@dbversity.com ~]$ /tmp/mongodb/bin/mongo –port 27010 MongoDB_Fragmentation_Estimator.sh
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27010/test
— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to primary: www.dbversity.com:27010

** Replica set config:
{
“_id” : “rs2”,
“version” : 3,
“members” : [
{
“_id” : 0,
“host” : “www.dbversity.com:27010”
},
{
“_id” : 1,
“host” : “www.dbversity.com:27011”
},
{
“_id” : 2,
“host” : “www.dbversity.com:27012”
}
]
}

** Checking for Fragmentation Started **

** Database: test – Statistics listed in MB

dataSize: 2595
indexSize: 1245
storageSize: 3416
fileSize: 8125

–WARNING– Database: test — Index size is between 25% to 100% of data size, REVIEW SCHEMA.

–WARNING– Database: test — Storage size plus Index Size is less than 75% of File Size, POSSIBLE FRAGMENTATION.

** Checking for Fragmentation Completed **
[usr@dbversity.com ~]$
————————————————————————————-
[usr@dbversity.com ~]$ /tmp/mongodb/bin/mongo –port 27011 MongoDB_Fragmentation_Estimator.sh
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27011/test
— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to secondary: www.dbversity.com:27011

** Replica set config:
{
“_id” : “rs2”,
“version” : 3,
“members” : [
{
“_id” : 0,
“host” : “www.dbversity.com:27010”
},
{
“_id” : 1,
“host” : “www.dbversity.com:27011”
},
{
“_id” : 2,
“host” : “www.dbversity.com:27012”
}
]
}

** Checking for Fragmentation Started **

** Checking for Fragmentation Completed **
[usr@dbversity.com ~]$
———————————————————————————————————————————————–
[usr@dbversity.com ~]$ /tmp/mongodb/bin/mongo –port 27010 MongoDB_Fragmentation_Estimator.sh
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27010/test
— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to primary: www.dbversity.com:27010

** Replica set config:
{
“_id” : “rs2”,
“version” : 3,
“members” : [
{
“_id” : 0,
“host” : “www.dbversity.com:27010”
},
{
“_id” : 1,
“host” : “www.dbversity.com:27011”
},
{
“_id” : 2,
“host” : “www.dbversity.com:27012”
}
]
}

** Checking for Fragmentation Started **

UNABLE to apply rules of thumb to the database ‘test’ (dataSize ~0GB/~0MB) as it is smaller than 6GB.

UNABLE to apply rules of thumb to the database ‘splitter’ (dataSize ~0GB/~0MB) as it is smaller than 6GB.

** Checking for Fragmentation Completed **
[usr@dbversity.com ~]$

—————————————————————————————

[usr@dbversity.com ~]$
[usr@dbversity.com ~]$ /tmp/mongodb/bin/mongo –port 27011 MongoDB_Fragmentation_Estimator.sh
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:27011/test
— THIS SCRIPT ‘getMongoFragmentation.js’ IS NOT FOR PRODUCTION USE, IT IS FOR DEMONSTRATION PURPOSES ONLY —

** Connected to secondary: www.dbversity.com:27011

** Replica set config:
{
“_id” : “rs2”,
“version” : 3,
“members” : [
{
“_id” : 0,
“host” : “www.dbversity.com:27010”
},
{
“_id” : 1,
“host” : “www.dbversity.com:27011”
},
{
“_id” : 2,
“host” : “www.dbversity.com:27012”
}
]
}

** Checking for Fragmentation Started **

** Database: test – Statistics listed in MB

dataSize: 151
indexSize: 68
storageSize: 219
fileSize: 960

–WARNING– Database: test — Index size is between 25% to 100% of data size, REVIEW SCHEMA.

–WARNING– Database: test — Storage size plus Index Size is less than 75% of File Size, POSSIBLE FRAGMENTATION.

** Checking for Fragmentation Completed **
[usr@dbversity.com ~]$

  • Ask Question