Database Manual / Sharding / Administration / Scaling Strategies

Manage Unsharded Collections管理未分片的集合

Collections on the same replica set can encounter performance bottlenecks when competing for resources. As your data grows beyond the available memory, increased disk I/O introduces latency and strains system resources, degrading overall application performance. 在竞争资源时,同一副本集上的集合可能会遇到性能瓶颈。随着数据增长到可用内存之外,增加的磁盘I/O会引入延迟并使系统资源紧张,从而降低整体应用程序性能。Isolating collections on dedicated shards within your cluster reduces these resource constraints while maintaining a single connection point for your application.在集群内的专用分片上隔离集合可以减少这些资源限制,同时为应用程序维护一个连接点。

Benefits of Collection Isolation:集合隔离的好处:

When to Use 何时使用moveCollection

Moving collections on dedicated shards is particularly beneficial when your collection requirements vary in the following ways:当集合要求在以下方面有所不同时,在专用分片上移动集合特别有益:

Requirement需求 要求 规格Description描述
Access Patterns访问模式Some of your collections are read-heavy, while others are write-heavy.一些集合品读起来很重,而另一些则写得很重。
Performance演出Certain collections require more RAM, CPU, or disk throughput than others.某些集合比其他集合需要更多的RAM、CPU或磁盘吞吐量。
Scalability Demands可扩展性需求Collections that have rapid or unpredictable growth patterns require dedicated hardware.具有快速或不可预测增长模式的集合需要专用硬件。

Assigning collections to shards with the necessary hardware to meet their specific requirements optimizes performance while maintaining operational simplicity.将集合分配给具有必要硬件的分片,以满足其特定要求,从而优化性能,同时保持操作简单性。

When to Move Unsharded Collections何时移动未分片的集合

Starting in MongoDB 8.0, movable collections allow you to strategically place any unsharded collection on any shard within the cluster. Previously, unsharded collections were restricted to the primary shard of their database, leading to resource bottlenecks. 从MongoDB 8.0开始,可移动集合允许您将任何未分片的集合战略性地放置在集群内的任何分片上。以前,未分片的集合仅限于其数据库的主分片,导致资源瓶颈。Moving a collection simplifies horizontal scaling by allowing you to relocate unsharded collections without disrupting workloads.移动集合简化了水平扩展,允许您在不中断工作负载的情况下重新定位未记录的集合。

While not every collection needs to be sharded, deploying a sharded cluster provides horizontal scaling advantages even for unsharded collections. This approach maintains a single connection point for all data access, simplifying application architecture.虽然并非每个集合都需要分片,但部署分片集群即使对于未分片的集合也能提供水平扩展优势。这种方法为所有数据访问维护了一个连接点,简化了应用程序架构。

The following scenarios benefit from moving unsharded collections across shards:以下场景受益于跨分片移动未分片的集合:

Workload Isolation工作负载隔离

When multiple collections serve different workloads within the same cluster, moving unsharded collections across different shards helps prevent resource contention. This separation eliminates issues where one workload's performance negatively impacts others.当多个集合服务于同一集群内的不同工作负载时,在不同分片之间移动未分片的集合有助于防止资源争用。这种分离消除了一个工作负载的性能对其他工作负载产生负面影响的问题。

Multi-Tenant Architecture多租户架构

In environments hosting collections for different tenants, MongoDB's moveCollection command enables seamless distribution of collections across shards without downtime. 在为不同租户托管集合的环境中,MongoDB的moveCollection命令可以在不停机的情况下跨分片无缝分发集合。This flexibility optimizes resource allocation based on each tenant's specific needs.这种灵活性根据每个租户的特定需求优化了资源分配。

Geographic Data Distribution地理数据分布

Organizations may need to store user data in specific geographic regions to comply with data sovereignty regulations. With moveCollection, you can place unsharded collections on shards in different regions and relocate them as regulatory requirements evolve.组织可能需要将用户数据存储在特定的地理区域,以遵守数据主权法规。使用moveCollection,您可以将未分片的集合放置在不同地区的分片上,并随着监管要求的发展而重新定位它们。

Cost Optimization成本优化

Before MongoDB 8.0, all unsharded collections within a database were restricted to the primary shard. This limitation often forced upgrades to larger, more expensive instance tiers. MongoDB 8.0 removes this constraint, allowing movement of unsharded collections across all available shards in the cluster.在MongoDB 8.0之前,数据库中所有未分片的集合都仅限于主分片。这种限制通常迫使升级到更大、更昂贵的实例层。MongoDB 8.0消除了这一限制,允许在集群中的所有可用分片之间移动未分片的集合。

Moving unsharded collections across asymmetric shard hardware delivers significant benefits for resource optimization, allowing you to place specific collections on hardware tailored to their requirements. 非对称分片硬件上移动未分片的集合可以为资源优化带来显著的好处,使您能够将特定的集合放置在根据其要求量身定制的硬件上。By matching collections to appropriate hardware resources, you can scale different workloads independently based on their actual demands. This targeted approach improves performance while avoiding the cost of over-provisioning resources across the entire cluster.通过将集合与适当的硬件资源相匹配,您可以根据实际需求独立扩展不同的工作负载。这种有针对性的方法提高了性能,同时避免了在整个集群中过度配置资源的成本。

Reduced Collection Density集合密度降低

While MongoDB has no hard limit on collection count per instance, performance degrades when a node manages too many collections and indexes. 虽然MongoDB对每个实例的集合计数没有硬性限制,但当节点管理过多的集合和索引时,性能会下降。To learn more about these limits, see MongoDB Atlas Collection and Index Limits.要了解有关这些限制的更多信息,请参阅MongoDB Atlas集合和索引限制

By distributing unsharded collections across different shards, you can reduce collection density on any single node while maintaining a unified access point for applications.通过将未分片的集合分布在不同的分片上,您可以降低任何单个节点上的集合密度,同时为应用程序维护一个一致性的访问点。

Strategic co-location战略性同地办公

Consider co-locating unsharded collections on the same shard to minimize distributed operations, such as cross-collection transactions or join operations ($lookup). 考虑在同一分片上共置未分片的集合,以最小化分布式操作,如跨集合事务或联接操作($lookup)。Keeping related operations confined to a single shard eliminates network overhead, reduces latency, and improves overall performance. This approach is particularly effective for collections that are frequently joined or accessed together in the same transaction.将相关操作限制在单个分片中可以消除网络开销,减少延迟,并提高整体性能。这种方法对于在同一事务中经常连接或访问在一起的集合特别有效。

Command Syntax命令语法

sh.moveCollection("database.collection", "shardName")

The following example moves four unsharded collection between two shards for equal distribution of collections:以下示例在两个分片之间移动四个未分片的集合,以实现集合的均匀分布:

db.adminCommand({moveCollection:"E", toShard: "shard1"})
Diagram of unsharded collections moving to a specified shard to distribute collections across two shards evenly.

When to avoid using 何时避免使用moveCollection

While moveCollection offers significant flexibility, there are a few specific scenarios where it may not be the optimal solution:虽然moveCollection提供了极大的灵活性,但在某些特定情况下,它可能不是最佳解决方案:

Large Collections大型集合

Don't use moveCollection when a collection is too large for a single shard. Consider sharding a collection when it is approaching 3 TB in size.当一个集合对于单个分片来说太大时,不要使用moveCollection。当集合的大小接近3 TB时,考虑对其进行分片。

Collections with MongoDB Search Indexes使用MongoDB搜索索引的集合

If a specific collection uses MongoDB Search, be aware that moveCollection uses resharding to rewrite the collection on a different shard. 如果一个特定的集合使用MongoDB搜索,请注意,moveCollection使用重新分片在不同的分片上重写集合。After moving the collection, you will need to manually rebuild its MongoDB Search index. 移动集合后,您需要手动重建其MongoDB搜索索引。Until the indexes are fully rebuilt, MongoDB Search functionality will be unavailable for this specific collection, though the rest of your application will function normally.在索引完全重建之前,MongoDB搜索功能将无法用于此特定集合,尽管应用程序的其余部分将正常运行。

Before using moveCollection, evaluate these limitations against your application requirements to determine if it's the appropriate solution.在使用moveCollection之前,请根据应用程序要求评估这些限制,以确定它是否是合适的解决方案。

Learn More了解更多