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.请参见分片键选择

Diagram of the shard key value space segmented into smaller ranges or chunks.

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:当分片键显示以下特征时,远程分片最有效:

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的值范围大、频率低,并且以非单调速率变化,则插入的分布可能类似于以下:

Diagram of good shard key distribution

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开始,您可以通过更改集合的分片键来重新分片集合
  • 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:如果对已填充的集合进行分片:

  • The sharding operation creates the initial chunk(s) to cover the entire range of the shard key values. 分片操作创建初始块以覆盖分片键值的整个范围。The number of chunks created depends on the configured chunk size.创建的块数取决于配置的块大小
  • After the initial chunk creation, the balancer migrates these initial chunks across the shards as appropriate as well as manages the chunk distribution going forward.在初始块创建之后,平衡器将这些初始块适当地跨分片迁移,并管理块分布。

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.在初始分发之后,平衡器将继续管理块分发。
Tip提示
See also: 参阅:

To learn how to deploy a sharded cluster and implement ranged sharding, see Deploy a Sharded Cluster.要了解如何部署分片集群并实现远程分片,请参阅部署分片群集

←  Hashed ShardingDeploy a Sharded Cluster →