Create Ranges in a Sharded Cluster在分片群集中创建范围
In most situations a sharded cluster will create/split and distribute ranges automatically without user intervention. 在大多数情况下,分片集群将自动创建/分割和分发范围,而无需用户干预。However, in a limited number of cases, MongoDB cannot create enough ranges or distribute data fast enough to support the required throughput.然而,在少数情况下,MongoDB无法创建足够的范围或以足够快的速度分发数据以支持所需的吞吐量。
For example, if you want to ingest a large volume of data into a cluster that is unbalanced, or where the ingestion of data will lead to data imbalance, such as with monotonically increasing or decreasing shard keys. 例如,如果您想将大量数据摄入到不平衡的集群中,或者摄入数据会导致数据不平衡的情况下,例如分片键单调增加或减少。Pre-splitting the ranges of an empty sharded collection can help with the throughput in these cases.在这些情况下,预先分割空分片集合的范围可以帮助提高吞吐量。
Alternatively, by defining the zones and zone ranges before sharding an empty or a non-existing collection, the shard collection operation creates ranges for the defined zone ranges as well as any additional ranges to cover the entire range of the shard key values and performs an initial range distribution based on the zone ranges. 或者,通过在对空集合或不存在的集合进行分片之前定义区域和区域范围,分片集合操作为已定义的区域范围以及任何其他范围创建范围,以覆盖分片键值的整个范围,并基于区域范围执行初始范围分布。For more information, see Empty Collection.有关详细信息,请参阅空集合。
Only pre-split ranges for an empty collection. Manually splitting ranges for a populated collection can lead to unpredictable range ranges and sizes as well as inefficient or ineffective balancing behavior.仅为空集合预拆分范围。手动拆分已填充集合的范围可能导致不可预测的范围和大小,以及低效或无效的平衡行为。
To split empty ranges manually, you can run the 要手动拆分空范围,可以运行split
command:split
命令:
To create ranges for documents in the 要使用myapp.users
collection using the email
field as the shard key, use the following operation in mongosh
:email
字段作为分片键为myapp.users
集合中的文档创建范围,请在mongosh
中使用以下操作:
for ( var x=97; x<97+26; x++ ){
for ( var y=97; y<97+26; y+=6 ) {
var prefix = String.fromCharCode(x) + String.fromCharCode(y);
db.adminCommand( { split: "myapp.users", middle: { email : prefix } } );
}
}
This assumes a collection size of 100 million documents.这假设了1亿个文档的集合大小。
For information on the initial ranges created and distributed by the sharding command, see Empty Collection.有关分片命令创建和分发的初始范围的信息,请参阅空集合。For information on the balancer and automatic distribution of ranges across shards, see Balancer Internals and Range Migration.有关平衡器和范围在分片之间的自动分配的信息,请参阅平衡器内部和范围迁移。For information on manually migrating ranges, see Migrate Ranges in a Sharded Cluster.有关手动迁移范围的信息,请参阅在分片群集中迁移范围。