Range-based sharding involves dividing data into contiguous ranges determined by the shard key values. 基于范围的分片涉及将数据划分为由分片键值确定的连续范围。In this model, documents with "close" shard key values are likely to be in the same chunk or shard. 在这种模型中,具有“close”分片键值的文档可能位于同一块或分片中。This allows for efficient queries where reads target documents within a contiguous range. However, both read and write performance may decrease with poor shard key selection. See Shard Key Selection.这允许在读取连续范围内的目标文档时进行高效查询。然而,由于分片键选择不佳,读写性能都可能降低。请参见分片关键点选择。
Range-based sharding is the default sharding methodology if no other options such as those required for Hashed Sharding or zones are configured.如果没有配置其他选项,例如哈希分片或区域所需的选项,则基于范围的分片是默认的分片方法。
Shard Key Selection分片键选择
Ranged sharding is most efficient when the shard key displays the following traits:当分片键显示以下特征时,范围分片最有效:
Large Shard Key Cardinality大分片键基数Low Shard Key Frequency低分片键频率Non-Monotonically Changing Shard Keys非单调变化的分片键
The following image illustrates a sharded cluster using the field 下图显示了使用字段X as the shard key. If the values for X have a large range, low frequency, and change at a non-monotonic rate, the distribution of inserts may look similar to the following:X作为分片键的分片集群。如果X的值范围大、频率低并且以非单调速率变化,则插入的分布可能类似于以下内容:
Shard a Collection分片集合
Use the 使用sh.shardCollection() method, specifying the full namespace of the collection and the target index or compound index to use as the shard key.sh.shardCollection()方法,指定集合的完整命名空间以及用作分片键的目标索引或复合索引。
sh.shardCollection( "database.collection", { <shard key> } )
Important
Starting in MongoDB 5.0, you can reshard a collection by changing a collection's shard key.从MongoDB 5.0开始,您可以通过更改集合的分片键来重新分片集合。You can refine a shard key by adding a suffix field or fields to the existing shard key.您可以通过向现有分片键添加一个或多个后缀字段来细化分片键。
Shard a Populated Collection分片化集合
If you shard a populated collection, only one chunk is created initially. The balancer then migrates ranges from that chunk if necessary according to the configured range size.如果你对一个已填充的集合进行分片,最初只会创建一个块。然后,平衡器根据配置的范围大小,在必要时从该块中迁移范围。
Shard an Empty Collection空的集合分片
If you shard an empty collection:如果你对一个空集合进行分片:
With no zones and zone ranges specified for the empty or non-existing collection:没有为空或不存在的集合指定区域和区域范围:The sharding operation creates a single empty chunk to cover the entire range of the shard key values.分片操作创建了一个空块来覆盖分片键值的整个范围。After the initial chunk creation, the balancer migrates the initial chunk across the shards as appropriate as well as manages the chunk distribution going forward.在初始块创建之后,平衡器会根据需要在分片之间迁移初始块,并管理未来的块分布。
With zones and zone ranges specified for the empty or a non-existing collections:为空集合或不存在的集合指定区域和区域范围:The sharding operation creates empty chunks for the defined zone ranges as well as any additional chunks to cover the entire range of the shard key values and performs an initial chunk distribution based on the zone ranges. This initial creation and distribution of chunks allows for faster setup of zoned sharding.分片操作为定义的区域范围创建空块以及任何额外的块,以覆盖分片键值的整个范围,并根据区域范围执行初始块分布。这种块的初始创建和分发允许更快地设置分区分片。After the initial distribution, the balancer manages the chunk distribution going forward.在初始分发之后,平衡器管理接下来的块分发。
Tip
To learn how to deploy a sharded cluster and implement ranged sharding, see Deploy a Self-Managed Sharded Cluster.要了解如何部署分片集群和实现远程分片,请参阅部署自管理分片集群。