Database Manual / Sharding / Data Partitioning

Create Ranges in a Sharded Cluster在分片群集中创建范围

In most situations a sharded cluster will create, split, and distribute ranges automatically without user intervention. 在大多数情况下,分片集群将自动创建、分割和分配范围,而无需用户干预。However, in some 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 where you have inserts distributed across shards, pre-splitting the ranges of an empty sharded collection can improve throughput.例如,如果你想将大量数据摄入到一个集群中,在这个集群中,插入分布在多个分片上,预先分割空分片集合的范围可以提高吞吐量。

Note

Starting in MongoDB 6.0, the balancer does not distribute empty ranges. 从MongoDB 6.0开始,平衡器不分配空范围。To pre-split the collection, use moveRange to distribute the empty ranges across the shards in the cluster. 要预拆分集合,请使用moveRange将空范围分布到集群中的分片上。moveRange automatically splits the range to be moved, meaning moveRange performs both splitting and moving. You don't need to manually split the range with split.moveRange会自动拆分要移动的范围,这意味着moveRange既执行拆分又执行移动。您不需要使用split手动分割范围。

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.有关更多信息,请参阅空集合

Warning

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.只有空集合的预分割范围。手动拆分已填充集合的范围可能会导致不可预测的范围和大小,以及低效或无效的平衡行为。

Steps步骤

The following example shows how to manually generate and distribute ranges. The example uses a collection in the sample.documents namespace and shards that collection on the email field.以下示例显示了如何手动生成和分配范围。该示例使用sample.documents命名空间中的一个集合和email字段上的该集合的分片。

1

Define shard key ranges定义分片键范围

Create a function to define shard key ranges. This example creates ranges based on possible email addresses because email will be used as the shard key.创建一个函数来定义分片键范围。此示例基于可能的电子邮件地址创建范围,因为email将用作分片键。

// Generate two character prefix email ranges.
function getRanges(shards) {
let ranges = [];

// The total number of prefix possibilities is 26 * 26 (aa to zz).
// We calculate the number of combinations to add in a range.
const totalCombinationsPerShard = 26 * 26 / shards.length;
let minKey = {
email: MinKey
};
let maxKey = {
email: MinKey
};

for(let i = 1; i <= shards.length; ++i) {
// 97 is lower case 'a' in ASCII.
let prefix = 97 + ((totalCombinationsPerShard*i)/26);
let suffix = 97 + ((totalCombinationsPerShard*i)%26);
let initialChars = String.fromCharCode(prefix) + String.fromCharCode(suffix);

minKey = maxKey;
maxKey = {
email: i !== shards.length ? initialChars : MaxKey
};

ranges.push({
min: minKey,
max: maxKey
});
}

return ranges;
}
2

Shard the collection分割集合

To shard the sample.documents collection, run this command:要分割sample.documents集合,请运行以下命令:

db.adminCommand( {
shardCollection: 'sample.documents',
key: {
email: 1
}
} );

Note

Because the collection is empty, the shardCollection command automatically creates an index on the email field to support the shard key.由于集合为空,shardCollection命令会自动在email字段上创建一个索引来支持分片键。

3

Allocate shards to defined ranges将分片分配到定义的范围

To allocate the shards to the ranges defined in step 1, run the following command:要将分片分配到步骤1中定义的范围,请运行以下命令:

const shards = db.adminCommand({
listShards: 1
}).shards;

let ranges = getRanges(shards);

for (let i = 0; i < ranges.length; ++i) {
db.adminCommand({
moveRange: 'sample.documents',
min: ranges[i].min,
max: ranges[i].max,
toShard: shards[i]._id
});
}

Results结果

The moveRange command distributes the empty ranges across the shards in the cluster. moveRange命令将空范围分布在集群中的分片上。The cluster is now optimized for bulk inserts.该集群现在已针对批量插入进行了优化。

Next Steps后续步骤

To further improve performance, create additional indexes to support your application's common queries.为了进一步提高性能,请创建其他索引来支持应用程序的常见查询。

Learn More了解更多