Ranged Sharding范围化分片
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. 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. X
作为分片键的分片群集。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
的值具有大范围、低频率并且以非单调速率变化,则插入的分布可能看起来类似于以下:
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> } )
Starting in MongoDB 5.0, you can reshard a collection by changing a collection's shard key.从MongoDB 5.0开始,您可以通过更改集合的分片键来重新分片集合。Starting in MongoDB 4.4, you can refine a shard key by adding a suffix field or fields to the existing shard key.从MongoDB 4.4开始,您可以通过在现有的分片键中添加一个或多个后缀字段来细化分片键。In MongoDB 4.2 and earlier, the choice of shard key cannot be changed after sharding.在MongoDB 4.2及更早版本中,分片后不能更改对分片键的选择。
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 collection (Available starting in MongoDB 4.0.3),为空集合或不存在的集合指定了区域和区域范围(从MongoDB 4.0.3开始可用),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.在初始分发之后,平衡器管理接下来的块分发。
See also: 另请参阅:
To learn how to deploy a sharded cluster and implement ranged sharding, see Deploy a Sharded Cluster.要了解如何部署分片集群和实现远程分片,请参阅部署分片群集。