- Sharding >
- Sharded Cluster Tutorials >
- Sharded Cluster Data Management >
- Merge Chunks in a Sharded Cluster
Merge Chunks in a Sharded Cluster¶
Overview¶
The mergeChunks
command allows you to collapse empty chunks
into neighboring chunks on the same shard. A chunk is empty if
it has no documents associated with its shard key range.
Important
Empty chunks can make the balancer assess the cluster as properly balanced when it is not.
Empty chunks can occur under various circumstances, including:
- If a pre-split creates too many chunks, the distribution of data to chunks may be uneven.
- If you delete many documents from a sharded collection, some chunks may no longer contain data.
This tutorial explains how to identify chunks available to merge, and how to merge those chunks with neighboring chunks.
Procedure¶
Note
Examples in this procedure use a users
collection in the
test
database, using the username
filed as a
shard key.
Identify Chunk Ranges¶
In the mongo
shell, identify the chunk
ranges with the following operation:
The output of the sh.status()
will resemble the following:
The chunk ranges appear after the chunk counts for each sharded collection, as in the following excerpts:
Chunk counts:
Chunk range:
Verify a Chunk is Empty¶
The mergeChunks
command requires at least one empty input
chunk. In the mongo
shell, check the amount of data in a
chunk using an operation that resembles:
If the input chunk to dataSize
is empty,
dataSize
produces output similar to:
Merge Chunks¶
Merge two contiguous chunks on the same shard, where at least one of the contains no data, with an operation that resembles the following:
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 all empty chunks, confirm the new chunk, as follows:
The output of sh.status()
should resemble: