mingche 发表于 2018-10-26 07:37:16

Mongodb 分片 手动维护chunk

--显示collection的chunk分布信息  
db.collection.getShardDistribution()
  

  
显示chunk信息脚本:
  
AllChunkInfo = function(ns, est){
  
    var chunks = db.getSiblingDB("config").chunks.find({"ns" : ns}).sort({min:1}); //this will return all chunks for the ns ordered by min
  
    //some counters for overall stats at the end
  
    var totalChunks = 0;
  
    var totalSize = 0;
  
    var totalEmpty = 0;
  
    print("ChunkID,Shard,ChunkSize,ObjectsInChunk"); // header row
  
    // iterate over all the chunks, print out info for each
  
    chunks.forEach(
  
      function printChunkInfo(chunk) {
  

  
      var db1 = db.getSiblingDB(chunk.ns.split(".")); // get the database we will be running the command against later
  
      var key = db.getSiblingDB("config").collections.findOne({_id:chunk.ns}).key; // will need this for the dataSize call
  
      // dataSize returns the info we need on the data, but using the estimate option to use counts is less intensive
  
      var dataSizeResult = db1.runCommand({datasize:chunk.ns, keyPattern:key, min:chunk.min, max:chunk.max, estimate:est});
  
      // printjson(dataSizeResult); // uncomment to see how long it takes to run and status
  
      print(chunk._id+","+chunk.shard+","+dataSizeResult.size+","+dataSizeResult.numObjects);
  
      totalSize += dataSizeResult.size;
  
      totalChunks++;
  
      if (dataSizeResult.size == 0) { totalEmpty++ }; //count empty chunks for summary
  
      }
  
    )
  
    print("***********Summary Chunk Information***********");
  
    print("Total Chunks: "+totalChunks);
  
    print("Average Chunk Size (bytes): "+(totalSize/totalChunks));
  
    print("Empty Chunks: "+totalEmpty);
  
    print("Average Chunk Size (non-empty): "+(totalSize/(totalChunks-totalEmpty)));
  
}
  

  

  

  
使用示例:
  
mongos> AllChunkInfo("test1.users001", true);
  
ChunkID,Shard,ChunkSize,ObjectsInChunk
  
test1.users001-_id_MinKey,shard3,11347710,171935
  
test1.users001-_id_-6148914691236517204,shard1,11293458,171113
  
test1.users001-_id_-3074457345618258602,shard1,11320716,171526
  
test1.users001-_id_0,shard3,11349096,171956
  
test1.users001-_id_3074457345618258602,shard2,11340054,171819
  
test1.users001-_id_6148914691236517204,shard2,11328966,171651
  
***********Summary Chunk Information***********
  
Total Chunks: 6
  
Average Chunk Size (bytes): 11330000
  
Empty Chunks: 0
  
Average Chunk Size (non-empty): 11330000


页: [1]
查看完整版本: Mongodb 分片 手动维护chunk