翻译或纠错本页面

在集群中分裂数据块

正常情况下,如果数据块的大小已经达到设定的最大值 数据块大小 ,MongoDB会在数据再次插入时进行分裂,不过有时候,需要手动进行数据块分裂.

  • 在使用已有的数据部署完一个集群之后,可能会发生数据很多,但是集群中的 chunks 很少的情况.

  • 你希望插入大量的数据,但是这些数据所属的数据块集中在一个分片上.比如,你想插入大量 shard key300400 之间的数据,*但是* 范围在 250500 之间的数据块都在一个分片上.

注解

2.6 新版功能: MongoDB提供了 mergeChunks 命令将两个邻近的数据块合并为一个数据块,参见 在集群中合并数据块 获得更多信息.

如果 mongos 认为以后的写入会从数据迁移中获益,它会立刻将新分裂的数据块迁移到一个新的分片上,均衡器并不区分数据块是人工分裂的还是系统自动分裂的.

警告

在已经存在数据的集群中进行数据块分裂要十分小心,MongoDB自动分裂数据块并进行数据均衡,此时进行数据块分裂要考虑到分裂之后各个数据块的大小,如果分裂之后数据块大小变得不规则,有可能出现各个分片间数据块数量均衡但是数据量不均衡的情况.避免做导致数据块大小不均衡的数据块分裂.

Use sh.status() to determine the current chunk ranges across the cluster.

使用 split 进行数据块分裂,可以使用 middle 参数或者 find 参数. mongo 提供了:method: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(). The query in splitFind() does not need to use the shard key, though it nearly always makes sense to do so.

举例

下面的命令将 records 数据库中 people 集合包含 zipcode 值为 63109 的数据块分为两个数据块:

sh.splitFind( "records.people", { "zipcode": "63109" } )

使用 splitAt() 将数据块分为两个数据块,新数据块使用查询的值作为最小值.

举例

下面的命令将 records 数据库中 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.