Database Manual / Sharding / Reference / Defragment Sharded Collections

Start Defragmenting a Sharded Collection开始对分片集合进行分片整理

To start defragmenting a sharded collection, use the configureCollectionBalancing command with the defragmentCollection option set to true.要开始对分片集合进行分片整理,请使用configureCollectionBalancing命令,并将defragmentCollection选项设置为true

About this Task关于此任务

Fragmentation is where a sharded collection's data is broken up into an unnecessarily large number of small chunks. This can increase operation times of CRUD operations run on that collection. 分片化是指将分片集合的数据分解为不必要的大量小块。这可能会增加在该集合上运行的CRUD操作的操作时间。Defragmentation reduces the number of chunks by merging smaller chunks into larger ones, resulting in lower CRUD operation times.分片整理通过将较小的块合并为较大的块来减少块的数量,从而缩短CRUD操作时间。

If CRUD operation times are acceptable, you don't need to defragment collections.如果CRUD操作时间可以接受,则不需要对集合进行分片整理。

The following table summarizes defragmentation information for various MongoDB versions.下表总结了各种MongoDB版本的分片整理信息。

MongoDB VersionMongoDB版本Description描述
MongoDB 7.0 and laterChunks are automatically merged. Performance improvements from defragmenting a collection in MongoDB 7.0 are lower compared to MongoDB 6.0. Typically, you don't need to defragment collections starting in MongoDB 7.0.块会自动合并。与MongoDB 6.0相比,MongoDB 7.0中对集合进行分片整理的性能改进较低。通常,从MongoDB 7.0开始,您不需要对集合进行分片整理。
MongoDB 6.0 and earlier than 7.0

Defragment collections only if you experience CRUD operation delays when the balancer migrates chunks or a node starts.仅当平衡器迁移块或节点启动时遇到CRUD操作延迟时,才对分片整理集合进行整理。

Starting in MongoDB 6.0, high write traffic should not cause fragmentation. Chunk migrations cause fragmentation.从MongoDB 6.0开始,高写流量不应导致分片化。块迁移导致分片化。

Earlier than MongoDB 6.0Defragment collections only if you experience longer CRUD operation times during metadata updates. For MongoDB versions earlier than 6.0, a sharded collection becomes fragmented when the collection size grows significantly because of many insert or update operations.只有在元数据更新过程中遇到较长的CRUD操作时间时,才对集合进行分片整理。对于早于6.0的MongoDB版本,当集合大小因许多插入或更新操作而显著增长时,分片集合会变得分片化。

The procedure in this task uses an example sharded collection named ordersShardedCollection in a database named test.此任务中的过程在名为test的数据库中使用名为ordersShardedCollection的示例分片集合。

You can use your own sharded collection and database in the procedure.您可以在过程中使用自己的分片集合和数据库。

Before you Begin开始之前

Connect to mongos.连接到mongos

Procedure过程

1

Start defragmenting the collection开始对集合进行分片整理

Run:运行:

db.adminCommand(
{
configureCollectionBalancing: "test.ordersShardedCollection",
defragmentCollection: true
}
)
2

Ensure defragmentation started确保分片整理已开始

Ensure ok is 1 in the command output, which indicates the command execution was successful:确保命令输出中的ok1,这表示命令执行成功:

{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1677616966, i: 8 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1677616966, i: 8 })
}

Next Steps后续步骤

You can monitor the collection's defragmentation progress. For details, see Monitor Defragmentation of a Sharded Collection.您可以监视集合的分片整理进度。有关详细信息,请参阅监视分片集合的分片整理

Learn More了解更多