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 7.0 and later | |
| MongoDB 6.0 and earlier than 7.0 |
|
| Earlier than MongoDB 6.0 |
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过程
Start defragmenting the collection开始对集合进行分片整理
Run:运行:
db.adminCommand(
{
configureCollectionBalancing: "test.ordersShardedCollection",
defragmentCollection: true
}
)Ensure defragmentation started确保分片整理已开始
Ensure 确保命令输出中的ok is 1 in the command output, which indicates the command execution was successful:ok为1,这表示命令执行成功:
{
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了解更多
Print shard status, see打印分片状态,请参阅db.printShardingStatus()Retrieve shard status details, see检索分片状态详细信息,请参阅sh.status()View shard status collection fields, see查看分片状态集合字段,请参阅Sharded CollectionSee active mongos instances, see查看活动的mongos实例,请参阅Active活跃的mongosInstancesmongos实例Monitor shards using MongoDB Atlas, see Review Sharded Clusters使用MongoDB Atlas监控分片,请参阅查看分片集群。