Normally, MongoDB splits a chunk after an insert if the chunk exceeds the maximum chunk size. 通常,如果块超出最大块大小,MongoDB会在插入后拆分块。However, you may want to split chunks manually if:
300
and 400
, but all values of your shard keys are between 250
and 500
are in a single chunk.MongoDB provides the MongoDB提供了mergeChunks
command to combine contiguous chunk ranges into a single chunk. mergeChunks
命令,用于将连续的块范围合并为单个块。See Merge Chunks in a Sharded Cluster for more information.有关详细信息,请参阅合并分片群集中的块。
The balancer may migrate recently split chunks to a new shard immediately if the move benefits future insertions. 如果移动有利于将来的插入,平衡器可以立即将最近分割的块迁移到新的分片。The balancer does not distinguish between chunks split manually and those split automatically by the system.平衡器不区分手动分割的块和系统自动分割的块。
Be careful when splitting data in a sharded collection to create new chunks. 在分割分片集合中的数据以创建新块时要小心。When you shard a collection that has existing data, MongoDB automatically creates chunks to evenly distribute the collection. 当您对具有现有数据的集合进行分片时,MongoDB会自动创建块以均匀分布集合。To split data effectively in a sharded cluster you must consider the number of documents in a chunk and the average document size to create a uniform chunk size. 要在分片集群中有效地分割数据,必须考虑区块中的文档数和平均文档大小,以创建一致性的区块大小。When chunks have irregular sizes, shards may have an equal number of chunks but have very different data sizes. 当块的大小不规则时,分片可能具有相同数量的块,但具有非常不同的数据大小。Avoid creating splits that lead to a collection with differently sized chunks.避免创建导致具有不同大小块的集合的拆分。
Use 使用sh.status()
to determine the current chunk ranges across the cluster.sh.status()
确定集群中的当前区块范围。
To split chunks manually, use the 要手动拆分块,请使用split
command with either fields middle
or find
. split
命令,字段middle
或find
。mongosh
provides the helper methods sh.splitFind()
and sh.splitAt()
.mongosh
提供了助手方法sh.splitFind()
和sh.splitAt()
。
splitFind()
splits the chunk that contains the first document returned that matches this query into two equally sized chunks. 将包含与此查询匹配的第一个返回文档的块拆分为两个大小相等的块。You must specify the full namespace (i.e. "必须将分片集合的完整命名空间(即<database>.<collection>
") of the sharded collection to splitFind()
. <database>.<collection>
”)指定给splitFind()
。The query in splitFind()
does not need to use the shard key, though it nearly always makes sense to do so.splitFind()
中的查询不需要使用shard键,尽管这样做几乎总是有意义的。
The following command splits the chunk that contains the value of 以下命令分割包含记录数据库63109
for the zipcode
field in the people
collection of the records
database:people
集合中zipcode
字段值63109
的区块:
sh.splitFind( "records.people", { "zipcode": "63109" } )
Use 使用splitAt()
to split a chunk in two, using the queried document as the lower bound in the new chunk:splitAt()
将块拆分为两块,将查询的文档用作新块的下限:
The following command splits the chunk that contains the value of 以下命令分割包含记录数据库的63109
for the zipcode
field in the people
collection of the records
database.people
集合中zipcode
字段值63109
的块。
sh.splitAt( "records.people", { "zipcode": "63109" } )
splitAt()
does not necessarily split the chunk into two equally sized chunks. 不一定要将块分割为两个大小相等的块。The split occurs at the location of the document matching the query, regardless of where that document is in the chunk.拆分发生在与查询匹配的文档的位置,而不管该文档在区块中的位置如何。