为什么散列分片键在 mongodb 中的每个分片上创建块

Why do hashed shard keys create chunks on each shard in mongodb

当我使用散列分片键时,在添加任何数据之前,我的所有分片都已经用块拆分,如下面 sh.status() 的示例输出所示。

sample.test
    shard key: { "sampleId" : "hashed" }
    unique: false
    balancing: true
    chunks:
            shard001    2
            shard002    2
    { "sampleId" : { "$minKey" : 1 } } -->> { "sampleId" : NumberLong("-4611686018427387902") } on : shard001 Timestamp(1, 0) 
    { "sampleId" : NumberLong("-4611686018427387902") } -->> { "sampleId" : NumberLong(0) } on : shard001 Timestamp(1, 1) 
    { "sampleId" : NumberLong(0) } -->> { "sampleId" : NumberLong("4611686018427387902") } on : shard002 Timestamp(1, 2) 
    { "sampleId" : NumberLong("4611686018427387902") } -->> { "sampleId" : { "$maxKey" : 1 } } on : shard002 Timestamp(1, 3) 

现在,当使用复合键或未散列键和 运行 sh.status() 时,我只看到我的一个分片显示包含 1 个块。现在我必须将它填充到最大 64mb,以便创建第二个块。在下面的示例中,我有一个 shard002 和一个 shard001。

sample.test
    shard key: { "sampleId" : 1, "uid" : 1 }
    unique: false
    balancing: true
    chunks:
            shard002    1
    { "sampleId" : { "$minKey" : 1 }, "uid" : { "$minKey" : 1 } } -->> { "sampleId" : { "$maxKey" : 1 }, "uid" : { "$maxKey" : 1 } } on : shard002 Timestamp(1, 0)

所以我的问题是为什么在使用散列分片键时没有数据时会生成块?

这是哈希分片的实际行为。

Shard an empty collection

根据 mongodb 文档

If you shard an empty collection using a hashed shard key:

With no zones and zone ranges specified for the empty or non-existing collection: The sharding operation creates empty chunks to cover the entire range of the shard key values and performs an initial chunk distribution. By default, the operation creates 2 chunks per shard and migrates across the cluster. You can use numInitialChunks option to specify a different number of initial chunks. This initial creation and distribution of chunks allows for faster setup of sharding. After the initial distribution, the balancer manages the chunk distribution going forward.