On this page本页内容
split
Changed in version 4.2.在版本4.2中更改。
Splits a chunk in a sharded cluster into two chunks. 将分片集群中的一个块分割为两个块。Starting in MongoDB 4.2, shards manage and split chunks based on the chunk size statistics they maintain.从MongoDB 4.2开始,分片根据它们维护的块大小统计数据来管理和分割块。
For exceptional circumstances the 在特殊情况下,split
command does allow administrators to manually create splits. split
命令允许管理员手动创建拆分。See Split Chunks in a Sharded Cluster for information on these circumstances, and on the MongoDB shell commands that wrap 有关这些情况以及包装split
.split
MongoDB shell命令的信息,请参阅拆分集群中的拆分块。
The split
command must be run in the admin
database and uses the following form:split
命令必须在admin
数据库中运行,并使用以下形式:
db.adminCommand( { split: <database>.<collection>, <find|middle|bounds> } )
The split
command takes a document with the following fields:split
命令获取包含以下字段的文档:
split | string | |
find | document |
|
bounds | array |
|
middle | document | split find , bounds , or middle . find 、bound 或middle 。 |
When used with either the 当与find
or the bounds
option, the split
command splits the chunk along the median. find
或bound
选项一起使用时,split
命令会沿中间值分割块。As such, the command cannot use the 因此,该命令不能使用find
or the bounds
option to split an empty chunk since an empty chunk has no median.find
或bound
选项拆分空块,因为空块没有中间值。
To create splits in empty chunks, use either the 要在空块中创建拆分,请在middle
option with the split
command or use the sh.splitAt()
command.split
命令中使用middle
选项或使用sh.splitAt()
命令。
To create a chunk split, connect to a 要创建区块分割,请连接到mongos
instance, and issue the following command to the admin
database:mongos
实例,并向admin
数据库发出以下命令:
db.adminCommand( { split: <database>.<collection>, find: <document> } )
Or:
db.adminCommand( { split: <database>.<collection>, middle: <document> } )
Or:
db.adminCommand( { split: <database>.<collection>,
bounds: [ <lower>, <upper> ] } )
To create a split for a collection that uses a hashed shard key, use the 要为使用哈希分片键的集合创建拆分,请使用bounds
parameter. bounds
参数。Do not use the 不要为此使用middle
parameter for this purpose.middle
参数。
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.避免创建导致具有不同大小块的集合的拆分。
The following sections provide examples of the 以下部分提供了split
command.split
命令的示例。
db.adminCommand( { split : "test.people", find : { _id : 99 } } )
The split
command identifies the chunk in the people
collection of the test
database, that holds documents that match { _id : 99 }
. split
命令标识test
数据库的people
集合中的块,其中包含匹配{ _id : 99 }
的文档。split
does not require that a match exist, in order to identify the appropriate chunk. 不要求存在匹配以识别适当的块。Then the command splits it into two chunks of equal size.然后该命令将其拆分为两个大小相等的块。
To define an arbitrary split point, use the following form:要定义任意拆分点,请使用以下形式:
db.adminCommand( { split : "test.people", middle : { _id : 99 } } )
The split
command identifies the chunk in the people
collection of the test
database, that would hold documents matching the query { _id : 99 }
. split
命令标识test
数据库的people
集合中的块,该块将保存与查询{ _id : 99 }
匹配的文档。split
does not require that a match exist, in order to identify the appropriate chunk. 不要求存在匹配以识别适当的块。Then the command splits it into two chunks, with the matching document as the lower bound of one of the split chunks.然后,命令将其拆分为两个块,匹配的文档作为其中一个拆分块的下限。
This form is typically used when pre-splitting data in a collection.此表单通常在预拆分集合中的数据时使用。
This example uses the hashed shard key 此示例使用测试数据库的人员集合中的哈希分片键userid
in a people
collection of a test
database. userid
。The following command uses an array holding two single-field documents to represent the minimum and maximum values of the hashed shard key to split the chunk:以下命令使用包含两个单字段文档的数组来表示哈希分片键的最小值和最大值,以分割块:
db.adminCommand( { split: "test.people", bounds : [ { userid: NumberLong("-5838464104018346494") }, { userid: NumberLong("-5557153028469814163") } ] } )
MongoDB uses the 64-bit NumberLong type to represent the hashed value.MongoDB使用64位NumberLong类型表示哈希值。
Use 使用sh.status()
to see the existing bounds of the shard keys.sh.status()
查看分片键的现有边界。
If another process, such as a balancer process, changes metadata while 如果另一个进程(如平衡器进程)在split
is running, you may see a metadata lock error
.split
运行时更改元数据,则可能会看到metadata lock error
。
errmsg: "The collection's metadata lock is already taken."
This message indicates that the split has failed with no side effects. 此消息表示拆分失败,没有副作用。Retry the 重试split
command.split
命令。