- Sharding >
- Data Partitioning with Chunks >
- Merge Chunks in a Sharded Cluster
Merge Chunks in a Sharded Cluster¶
Overview¶
The mergeChunks
command allows you to combine continuous
chunks on the same shard into a single chunk. This tutorial explains
how to merge neighboring chunks in a sharded cluster.
Procedure¶
Note
Examples in this procedure use a members
collection in the
test
database, using the username
field as the
shard key.
Identify Chunk Ranges¶
In the mongo
shell, identify the chunk
ranges with the following operation:
In the output, the chunk ranges appear after the chunk counts for each sharded collection, as in the following example:
The chunk ranges appear after the chunk counts for each sharded
collection. For example, the following are the chunk ranges for the
test.members
collection:
Merge Chunks¶
Merge contiguous chunks on the same shard.
For example, consider the following chunk ranges on shardA
:
Note
The chunks to be merged are highlighted.
To merge the highlighted contiguous chunks, issue the
mergeChunks
command against the admin
database:
On success, mergeChunks
produces the following output:
On any failure condition, mergeChunks
returns a document
where the value of the ok
field is 0
.
View Merged Chunks Ranges¶
After merging the identified chunks, confirm the new chunk, as follows:
The output of sh.status()
should resemble:
After the merge, the balancer may migrate chunks across shards to ensure a more even distribution of chunks.