我们对分片的mongo环境进行了模拟测试,但是如果分片键值为小int,则集合不会分发,但如果分片键为big int,则其工作正常。请继续阅读...
用于从mongos shell插入记录的代码。
var shId = 15;
for (var i = 0; i < 100; i++) {
if(i%50 == 0){
shId = shId + 1;
}
db.Foo.insert( { shKeyId : shId , text:"this is a test" } );
}
如果shId = 15,则Foo集合拆分为两个分片不起作用。
环境:两个分片,每个分片都有Primary1和两个辅助mongod实例。 Mongo配置正在其中一个分片上运行。
通过shKeyId对'Foo'集合启用分片作为散列分片键。 db.runCommand({shardcollection:“test.Foo”,key:{shKeyId:“hashed”}});
sh.status()输出
mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("516ea48e979736fd306973c9")
}
shards:
{ "_id" : "mongo-perf-shrd1", "host" : "mongo-perf-shrd1/sh1-prim-ip:27017,sh1-sec1-ip:27017,sh1-sec2-ip:27017" }
{ "_id" : "mongo-perf-shrd2", "host" : "mongo-perf-shrd2/sh2-prim-ip:27017,sh2-sec1-ip:27017,sh2-sec2-ip:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "mongo-perf-shrd2" }
test.Foo
shard key: { "shKeyId" : "hashed" }
chunks:
mongo-perf-shrd2 2
mongo-perf-shrd1 2
{ "shKeyId" : { "$minKey" : 1 } } -->> { "shKeyId" : NumberLong("-4611686018427387902") } on : mongo-perf-shrd2 { "t" : 2, "i" : 2 }
{ "shKeyId" : NumberLong("-4611686018427387902") } -->> { "shKeyId" : NumberLong(0) } on : mongo-perf-shrd2 { "t" : 2, "i" : 3 }
{ "shKeyId" : NumberLong(0) } -->> { "shKeyId" : NumberLong("4611686018427387902") } on : mongo-perf-shrd1 { "t" : 2, "i" : 4 }
{ "shKeyId" : NumberLong("4611686018427387902") } -->> { "shKeyId" : { "$maxKey" : 1 } } on : mongo-perf-shrd1 { "t" : 2, "i" : 5 }
分片分配输出
mongos> db.Foo.getShardDistribution();
Shard mongo-perf-shrd1 at mongo-perf-shrd1/ip1:27017,ip2,ip3:27017
data : 6KiB docs : 100 chunks : 2
estimated data per chunk : 3KiB
estimated docs per chunk : 50
Shard mongo-perf-shrd2 at mongo-perf-shrd2/ip4:27017,ip5:27017,ip6:27017
data : 0B docs : 0 chunks : 2
estimated data per chunk : 0B
estimated docs per chunk : 0
Totals
data : 6KiB docs : 100 chunks : 4
Shard mongo-perf-shrd1 contains 100% data, 100% docs in cluster, avg obj size on shard : 64B
Shard mongo-perf-shrd2 contains 0% data, 0% docs in cluster, avg obj size on shard : NaNGiB
答案 0 :(得分:0)
你只是单调的碎片键, 这不是一个好的解决方案:http://docs.mongodb.org/manual/core/sharded-cluster-internals/
仅用于模拟测试,足以在默认情况下选择分片键_id字段