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. Defragmentation reduces the number of chunks by merging smaller chunks into larger ones, resulting in lower CRUD operation times.分片化是指将分片集合的数据分解为不必要的大量小块。这可能会增加在该集合上运行的CRUD操作的操作时间。分片整理通过将较小的块合并为较大的块来减少块的数量,从而缩短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 | |
|---|---|
| MongoDB 7.0 and later | |
| MongoDB 6.0 and earlier than 7.0 |
|
| Earlier than MongoDB 6.0 |
To defragment a sharded collection, use the 要对分片集合进行分片整理,请使用configureCollectionBalancing command's defragmentCollection option. The option is available starting in MongoDB 6.0.configureCollectionBalancing命令的defragmentCollection选项。该选项从MongoDB 6.0开始可用。
Before you Begin开始之前
Consider these issues before you defragment collections:在对集合进行分片整理之前,请考虑以下问题:
Defragmentation might cause many metadata updates on the shards.分片整理可能会导致分片上的许多元数据更新。If your CRUD operations are already taking longer than usual during migrations, you should only run defragmentation during a shard balancing window to reduce the system workload.如果CRUD操作在迁移过程中已经花费了比平时更长的时间,您应该只在分片平衡窗口期间运行分片整理,以减少系统工作负载。If defragmentation is impacting workload and CRUD latency on the cluster, you can reduce the impact using the如果分片整理正在影响集群上的工作负载和CRUD延迟,您可以使用chunkDefragmentationThrottlingMSparameter.chunkDefragmentationThrottlingMS参数来减少影响。Merged chunks lose their placement history.合并的块将丢失其放置历史记录。This means that while defragmentation is running, snapshot reads and indirectly, transactions, could fail with stale chunk history errors.这意味着,在分片整理运行时,快照读取和间接事务可能会因过时的块历史错误而失败。Placement history records the shards that a chunk was stored on. Defragmentation erases the placement history and some operations could fail, but will typically resolve after around five minutes.放置历史记录了存储块的分片。分片整理会擦除放置历史,一些操作可能会失败,但通常会在大约五分钟后解决。
Defragmentation affects the locality of the documents in a collection by moving data between shards. If a collection has ranges of data that are frequently accessed, after defragmenting the collection it is possible that the frequently accessed data will be on one shard. This might decrease the performance of CRUD operations by placing the workload on one shard instead of multiple shards.分片整理通过在分片之间移动数据来影响集合中文档的位置。如果一个集合有频繁访问的数据范围,在对集合进行分片整理后,频繁访问的信息可能会出现在一个分片上。这可能会通过将工作负载放在一个分片上而不是多个分片上来降低CRUD操作的性能。
Tasks任务
Manually start defragmenting a sharded collection手动开始对分片集合进行分片整理Monitor defragmentation of a sharded collection监视分片集合的分片整理Manually stop defragmenting a sharded collection手动停止对分片集合进行分片整理
Note
Typically, you should use a shard balancing window to specify when the balancer runs instead of manually starting and stopping defragmentation.通常,您应该使用分片平衡窗口来指定平衡器何时运行,而不是手动启动和停止分片整理。
Details详情
This section describes additional details related to defragmenting sharded collections.本节介绍与分片化集合分片整理相关的其他详细信息。
Configure Collection Balancing Status配置集合平衡状态
The defragmentCollection field returned by the configureCollectionBalancing command is only true when defragmentation is running.configureCollectionBalancing命令返回的分片defragmentCollection字段仅在分片整理运行时为真。
After defragmentation automatically ends or you manually stop defragmentation, the 在分片整理自动结束或您手动停止分片整理后,defragmentCollection field is removed from the returned document.defragmentCollection字段将从返回的文档中删除。
Operations运营
Secondary node reads are permitted during defragmentation, but might take longer to complete until metadata updates on the primary node are replicated to the secondary nodes.在分片整理期间允许读取辅助节点,但可能需要更长的时间才能完成,直到主节点上的元数据更新被复制到辅助节点。
Chunk Size, Balancing, and Defragmentation块大小、平衡和分片整理
For details about the MongoDB balancer, see Sharded Cluster Balancer.有关MongoDB平衡器的详细信息,请参阅分片集群平衡器。
For an introduction to 有关chunkSize, see Modify Range Size in a Sharded Cluster.chunkSize的介绍,请参阅修改分片群集中的范围大小。
When the collection data shared between two shards differs by three or more times the configured 当两个分片之间共享的集合数据与配置的chunkSize setting, the balancer migrates chunks between the shards.chunkSize设置相差三倍或更多时,平衡器会在分片之间迁移块。
For example, if 例如,如果chunkSize is 128 MB and the collection data differs by 384 MB or more, the balancer migrates chunks between the shards.chunkSize为128 MB,而集合数据相差384 MB或更多,则平衡器会在分片之间迁移块。
When chunks are moved, split, or merged, the shard metadata is updated after the chunk operation is committed by a config server.当块被移动、拆分或合并时,分片元数据会在配置服务器提交块操作后更新。Shards not involved in the chunk operation are also updated with new metadata.未参与块操作的分片也会用新的元数据进行更新。
The time for the shard metadata update is proportional to the size of the routing table. CRUD operations on the collection are temporarily blocked while the shard metadata is updated, and a smaller routing table means shorter CRUD operation delays.分片元数据更新的时间与路由表的大小成正比。在更新分片元数据时,集合上的CRUD操作会被临时阻止,较小的路由表意味着更短的CRUD操作延迟。
Defragmenting a collection reduces the number of chunks and the time to update the chunk metadata.对集合进行分片整理可以减少块的数量和更新块元数据的时间。
To reduce the system workload, configure the balancer to run only at a specific time using a shard balancing window. Defragmentation runs during the balancing window time period.为了减少系统工作负载,请使用分片平衡窗口将平衡器配置为仅在特定时间运行。分片整理在平衡窗口时间段内运行。
You can use the 您可以使用chunkDefragmentationThrottlingMS parameter to limit the rate of split and merge commands run by the balancer.chunkDefragmentationThrottlingMS参数来限制平衡器运行的拆分和合并命令的速率。
You can start and stop defragmentation at any time.您可以随时启动和停止分片整理。
You can also set a shard zone. A shard zone is based on the shard key, and you can associate each zone with one or more shards in a cluster.您还可以设置一个分片区域。分片区域基于分片键,您可以将每个区域与集群中的一个或多个分片相关联。
A sharded cluster only splits chunks when chunks must be migrated. This means the chunk size may exceed 分片集群仅在必须迁移块时才分割块。这意味着块大小可能超过chunkSize. Larger chunks reduce the number of chunks on a shard and improve performance because the time to update the shard metadata is reduced. For example, you might see a 1 TB chunk on a shard even though you have set chunkSize to 256 MB.chunkSize。较大的块减少了分片上的块数,并提高了性能,因为更新分片元数据的时间减少了。例如,即使您已将chunkSize设置为256MB,您也可能在分片上看到1TB的块。
chunkSize affects the following:chunkSize会影响以下内容:
Maximum amount of data the balancer attempts to migrate between two shards in a single chunk migration operation.平衡器在单个块迁移操作中尝试在两个分片之间迁移的最大数据量。Amount of data migrated during defragmentation.分片整理期间迁移的数据量。
Learn More了解更多
Introduction to sharding, see Sharding分片介绍,请参阅分片Partition data with chunks, see Data Partitioning with Chunks使用块对数据进行分区,请参阅使用块进行数据分区Configure collection balancing, see配置集合平衡,请参阅configureCollectionBalancingExamine balancer collection status, see检查平衡器集合状态,请参阅balancerCollectionStatusConfigure shard balancing windows, see Schedule the Balancing Window配置分片平衡窗口,请参阅计划平衡窗口。Monitor shards using MongoDB Atlas, see Review Sharded Clusters使用MongoDB Atlas监控分片,请参阅查看分片集群