- Sharding >
- Ranged Sharding
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:
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:
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.
Important
Starting in MongoDB 4.4, you can refine a collection’s shard key by adding a suffix field or fields to the existing key. In earlier versions, once you shard a collection, the selection of the shard key is immutable; i.e. you cannot select a different shard key for that collection. For details on refining a shard key, see Refine a Shard Key.
Starting in MongoDB 4.2, you can update a document’s shard key value unless the shard key field is the immutable
_id
field. Before MongoDB 4.2, a document’s shard key field value is immutable.For details on updating the shard key, see Change a Document’s Shard Key Value.
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),
- 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.