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.如果没有配置其他选项(如散列分片或分区所需的选项),则基于范围的分片是默认的分片方法。
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
的值范围大、频率低,并且以非单调速率变化,则插入的分布可能类似于以下:
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> } )
If you shard a populated collection:如果对已填充的集合进行分片:
If you shard an empty collection:如果将空集合分片:
With no zones and zone ranges specified for the empty or non-existing collection:没有为空集合或不存在集合指定区域和区域范围:
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开始可用),
To learn how to deploy a sharded cluster and implement ranged sharding, see Deploy a Sharded Cluster.要了解如何部署分片集群并实现远程分片,请参阅部署分片群集。