Database Manual / Reference / Database Commands / Sharding

mergeAllChunksOnShard (database command数据库命令)

Definition定义

mergeAllChunksOnShard
mergeAllChunksOnShard finds and merges all mergeable chunks that a shard owns for a given collection.查找并合并一个分片为给定集合拥有的所有可合并块

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
mergeAllChunksOnShard: <name of the collection>,
shard: <name of the shard>,
maxNumberOfChunksToMerge: <maximum number of chunks to merge> /* optional */
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Necessity必要性Description描述
mergeAllChunksOnShardstring字符串Required必需Name of the collection.集合的名称。
shardstring字符串Required必需Name of the shard.分片的名称。
maxNumberOfChunksToMergeinteger整数Optional可选Maximum number of chunks to merge.要合并的最大块数。

Behavior行为

mergeAllChunksOnShard finds and merges all mergeable chunks for a collection on the same shard. Two or more contiguous chunks in the same collection are mergeable when they meet all of these conditions:查找并合并同一分片上集合的所有可合并块。当同一集合中的两个或多个连续块满足所有这些条件时,它们是可以合并的:

  • They are owned by the same shard.它们属于同一个分片。
  • They are not jumbo chunks. jumbo chunks are not mergeable because they cannot participate in migrations.它们不是jumbo块的。jumbo块不可合并,因为它们不能参与迁移。
  • Their history can be purged safely, without breaking transactions and snapshot reads:可以安全地清除它们的历史记录,而不会中断事务和快照读取:

Example示例

This example assumes that history is empty for all chunks and all chunks are non-jumbo. Since both conditions are true, all contiguous intervals on the same shard are mergeable.此示例假设所有块的历史记录都是空的,并且所有块都是非巨型的。由于这两个条件都为真,因此同一分片上的所有连续区间都是可合并的

Setup设置

These chunks belong to a collection named coll with shard key x. There are nine chunks in total.这些块属于一个名为coll的集合,其分片键为x。总共有九块。

Chunk IDMinMaxShard
Ax: 0x: 10Shard0
Bx: 10x: 20Shard0
Cx: 20x: 30Shard0
Dx: 30x: 40Shard0
Ex: 40x: 50Shard1
Fx: 50x: 60Shard1
Gx: 60x: 70Shard0
Hx: 70x: 80Shard0
Ix: 80x: 90Shard1

Steps步骤

1

Merge All Mergeable Chunks on Shard0合并Shard0上的所有可合并块

db.adminCommand( { mergeAllChunksOnShard: "db.coll", shard: "Shard0" } )

This command merges the contiguous sequences of chunks:此命令合并连续的块序列:

  • A-B-C-D
  • G-H
2

Merge All Mergeable Chunks on Shard1合并Shard1上的所有可合并块

db.adminCommand( { mergeAllChunksOnShard: "db.coll", shard: "Shard1" } )

This command merges the contiguous sequences of chunks E-F.此命令合并块E-F的连续序列。

Result结果

After these commands have completed, the contiguous chunks have been merged. There are four total chunks instead of the original nine.完成这些命令后,连续的块已被合并。总共有四个块,而不是原来的九个。

Chunk IDMinMaxShard
A-B-C-Dx: 0x: 40Shard0
E-Fx: 40x: 60Shard1
G-Hx: 60x: 80Shard0
Ix: 80x: 90Shard1