Advertisement
Guest User

Compute chunks size in mongodb

a guest
Sep 18th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //works only for mongo 2.2
  2. //works better if you use mongo-hacker (https://github.com/TylerBrock/mongo-hacker)
  3. var ns ="products.products";
  4. var key = {partnerId : 1, id : 1}; //the shard key of the collection
  5. if (typeof colorize != "function") colorize = function(s) {return s};
  6. var zeroChunk =0
  7. var chunkSize = db.getSiblingDB("config").settings.findOne({_id : "chunksize"}).value; //target chunkSize in mb
  8. db.getSiblingDB("config").chunks.find({ns : ns}).limit(500).forEach(function(chunk) {
  9.         var ds = db.getSiblingDB(ns.split(".")[0]).runCommand({datasize:chunk.ns,keyPattern:key,min:chunk.min,max:chunk.max});
  10.         if (ds.numObjects ==0)
  11.           zeroChunk++;
  12.         else {
  13.           if (ds.size > chunkSize*1024 *1024) color = "red"; else color = "black";
  14.           print("Chunk: "+chunk._id +" has a size of "+colorize(ds.size,color)+", and includes "+ds.numObjects+" objects (took "+ds.millis+"ms)")
  15.          }
  16.     }
  17. )
  18. print(zeroChunk*100/db.getSiblingDB("config").chunks.find({ns : ns}).count() + " chunks were empty");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement