为什么单调递增的分片键会导致插入被路由到以 maxkey 作为上限的块?

Why does a monotonically increasing shard key causes inserts to be routed to the chunk with maxkey as upper bound?

我在 MongoDB 文档中阅读了有关碎片键的内容,如下所示”

If the shard key value is always increasing, all new inserts are routed to the chunk with maxKey as the upper bound. If the shard key value is always decreasing, all new inserts are routed to the chunk with minKey as the lower bound.

[https://docs.mongodb.com/manual/core/sharding-shard-key/#sharding-shard-key-creation][1]

我不明白为什么。假设分片键的范围从 0 到 75,并被划分为 3 个块。第一个块从 0 到 24,第二个块从 25 到 50,第三个块从 51 到 75。这里的 minKey 是 0,maxKey 是 75。 如果连续的插入操作以单调递增的方式发生,例如 1,2,3,4,5 为什么这些寻址共享键值 1,2,3,4,5(单调递增)的插入是路由到从 51 到 75 的最后一个分片,最后一个分片? (这是包含以 maxKey 为上界的块的分片) ?

谢谢

假设 分片键的范围 从 0 到 75,正如你所说的 3 个块。第一个块从 0 到 24,第二个块从 25 到 50,第三个块从 51 到 75。

如果您插入 1、2、3、4、5,它们将全部转到第一个块。不是最后一个。由于它们在 0 到 24 的范围内。

文档讨论的是当您拥有 始终递增的键时 例如从 0 到无穷大。

在这种情况下,从 0 到 Infinity 可能有 3 个块。第一个块从 0 到 24,第二个块从 25 到 50,第三个块从 51 到 75。但是由于键的值总是增加到无穷大,所有值 > 50 将转到第三个块并且 <= 50 值将到前 2 个块。

在分片集群中,有一个 Balancer 会尝试平衡您的块,以便它们具有相同数量的数据。使用 始终增加的键 ,平衡器可能很难决定如何拆分块。因此,第一次将所有具有值 > 50 的插入路由到以 maxKey 作为上限的块,即第三个块。