Database Manual / Sharding / Administration / Scaling Strategies

Distribute Collection Data分发集合数据

Sharding a collection distributes its documents across multiple shards in your MongoDB cluster. MongoDB uses your specified shard key to determine precisely where each document belongs. Choosing an effective shard key is critical, ensuring even data distribution and workload balancing across all available shards. 对集合进行分片会将其文档分布在MongoDB集群中的多个分片上。MongoDB使用您指定的分片键来精确确定每个文档所属的位置。选择一个有效的分片键至关重要,可以确保所有可用分片之间的数据分布和工作负载平衡。This approach becomes essential when collections grow too large for a single shard to handle efficiently. Once sharded, MongoDB automatically distributes the collection across all available shards according to your chosen sharding strategy.当集合变得太大,单个分片无法有效处理时,这种方法变得至关重要。一旦分片,MongoDB会根据您选择的分片策略自动将集合分布到所有可用的分片上。

When to Consider Sharding何时考虑分片

You should consider sharding a collection when you approach certain resource limits or performance thresholds.当您接近某些资源限制或性能阈值时,您应该考虑对集合进行分片。

High Resource Utilization资源利用率高

If a collection’s working set fits in RAM, MongoDB serves queries from memory, which provides the fastest query response times. 如果集合的工作集适合RAM,MongoDB将从内存中提供查询,从而提供最快的查询响应时间。When the working set grows beyond available memory, query latencies grow longer due to increased disk access. Sharding a collection improves query performance by distributing the data across multiple shards, where each shard maintains its own data indexes.当工作集增长到可用内存之外时,由于磁盘访问的增加,查询延迟会变长。对集合进行分片可以通过在多个分片上分布数据来提高查询性能,每个分片都维护自己的数据索引。

Large Data Size大数据量

If your collection contains 3TB of data or more, you should consider sharding it to optimize performance.如果集合包含3TB或更多数据,您应该考虑对其进行分片以优化性能。

Distribution Options分销选项

When sharding a collection in MongoDB, you can choose from the following distribution options:在MongoDB中对集合进行分片时,您可以从以下分发选项中进行选择:

Option选项Description描述
Ranged Sharding测距分片Ranged sharding uses one or more document fields to determine data placement. Data with similar shard key values is stored on the same shard, optimizing range-based queries. This approach works best when your access patterns include range operations.范围分片使用一个或多个文档字段来确定数据放置。具有相似分片键值的数据存储在同一分片上,优化了基于范围的查询。当访问模式包括范围操作时,这种方法最有效。
Hashed Sharding哈希分片Hashed sharding computes a hash value from your specified field and distributes data randomly across shards. While useful for write scalability, this approach can impact performance for range-based queries since logically adjacent data may reside on different shards.哈希分片根据指定字段计算哈希值,并在分片之间随机分布数据。虽然这种方法对写可扩展性很有用,但它会影响基于范围的查询的性能,因为逻辑上相邻的数据可能驻留在不同的分片上。
Zone Sharding区域分割Zone sharding distributes collections across a specific subset of shards rather than the entire cluster. 区域分片将集合分布在特定的分片子集上,而不是整个集群上。This approach is ideal when collections exceed single-shard capacity but require strategic placement—whether for geographic proximity to users, optimizing for distinct access patterns with specialized hardware, or maintaining regulatory compliance by controlling data location.当集合超过单分片容量但需要战略性放置时,这种方法是理想的——无论是为了地理位置接近用户,还是为了使用专用硬件优化不同的访问模式,或者通过控制数据位置来保持监管合规性。

Behavior行为

When sharding a collection, you must:在对集合进行分片时,您必须:

Learn More了解更多