Docs HomeMongoDB Manual

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 VersionDescription描述
MongoDB 7.0 and laterMongoDB 7.0及更高版本Chunks are automatically merged. 区块会自动合并。Performance improvements from defragmenting a collection in MongoDB 7.0 are lower compared to MongoDB 6.0. 与MongoDB 6.0相比,MongoDB 7.0中对集合进行分片整理的性能改进更低。Typically, you don't need to defragment collections starting in MongoDB 7.0.通常,您不需要从MongoDB 7.0开始对集合进行分片整理。
MongoDB 6.0 and earlier than 7.0MongoDB 6.0及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. 从MongoDB 6.0开始,高写流量不应该导致分片化。Chunk migrations cause fragmentation. 块迁移会导致分片。
Earlier than MongoDB 6.0MongoDB 6.0之前Defragment collections only if you experience longer CRUD operation times during metadata updates. 只有在元数据更新期间CRUD操作时间较长时,才能对集合进行分片整理。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.对于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了解更多信息