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中对集合进行分片时,您可以从以下分发选项中进行选择:
Behavior行为
When sharding a collection, you must:在对集合进行分片时,您必须:
Choose a shard key. Use the选择一个分片键。使用analyzeShardKeydatabase command to facilitate this choice.analyzeShardKey数据库命令来简化此选择。Select a sharding method:选择分片方法:Sharded Cluster Balancer for automatic data migration用于自动数据迁移的分片集群平衡器Reshard to shard for complete redistribution重新分区以实现完全重新分发