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:集合隔离的好处:
Workload Separation – Prevent resource contention by assigning collections to specific shards.工作负载分离 – 通过将集合分配给特定的分片来防止资源争用。Hardware Optimization – Configure asymmetric shards with hardware tailored to specific collection requirements.硬件优化 – 使用根据特定集合要求定制的硬件配置非对称分片。Independent Scaling – Scale collections individually based on workload growth.独立扩展 – 根据工作负载增长单独扩展集合。Improved Resilience – Reduce recovery time by isolating potential failures.提高弹性 – 通过隔离潜在故障来减少恢复时间。
When to Use 何时使用moveCollection
Moving collections on dedicated shards is particularly beneficial when your collection requirements vary in the following ways:当集合要求在以下方面有所不同时,在专用分片上移动集合特别有益:
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 在为不同租户托管集合的环境中,MongoDB的moveCollection command enables seamless distribution of collections across shards without downtime. 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"})
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 如果一个特定的集合使用MongoDB搜索,请注意,moveCollection uses resharding to rewrite the collection on a different shard. 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之前,请根据应用程序要求评估这些限制,以确定它是否是合适的解决方案。