Docs HomeMongoDB Manual

Troubleshoot Shard Keys分片键疑难解答

The ideal shard key allows MongoDB to distribute documents evenly throughout the cluster while facilitating common query patterns. 理想的分片键允许MongoDB在集群中均匀地分布文档,同时促进常见的查询模式。A suboptimal shard key can lead to the following problems:次优的分片键可能会导致以下问题:

In the following you can find out more about common problems with shard keys and how to resolve them.在下文中,您可以了解更多关于分片键常见问题以及如何解决这些问题的信息。

Jumbo Chunks巨型块

If you are seeing jumbo chunks, either the cardinality of your shard key is insufficient or the frequency of the shard key values is unevenly distributed.如果您看到的是巨型块,那么可能是您的分片键的基数不足,或者分片键值的频率分布不均匀。

To increase the cardinality of your shard key or change the distribution of your shard key values, you can:要增加分片键的基数或更改分片键值的分布,可以:

To learn whether you should reshard your collection or refine your shard key, see Change a Shard Key.要了解是应该重新发布集合还是细化分片键,请参阅更改分片键

To only change the distribution of your shard key values, you can also consider using Hashed Sharding to distribute your data more evenly.要只更改分片键值的分布,还可以考虑使用哈希分片来更均匀地分布数据。

For advice on choosing a shard key see Choose a Shard Key.有关选择分片键的建议,请参阅选择分片键

Uneven Load Distribution负载分布不均匀

If your cluster is experiencing uneven load distribution, check if your shard key increases monotonically. 如果集群的负载分布不均匀,请检查分片键是否单调增加。A shard key that is a monotonically increasing field, leads to an uneven read and write distribution.分片键是一个单调增加的字段,会导致读写分布不均匀。

Consider an orders collection that is sharded on an order_id field. 考虑一个在order_id字段上进行分片的订单集合。The order_id is an integer which increases by one with each order.order_id是一个整数,它随每个订单增加一。

  • New documents are generally written to the same shard and chunk. 新文档通常被写入同一个分片和区块。The shard and chunk that receive the writes are called hot shard and hot chunk. 接收写入的分片和区块称为热分片和热区块。The hot shard changes over time. When chunks are split, the hot chunk moves to a different shard to optimize data distribution.热分片会随着时间的推移而变化。分割区块时,热区块会移动到不同的分片,以优化数据分布。
  • If users are more likely to interact with recent orders, which are all on the same shard, the shard that contains recent orders will receive most of the traffic.如果用户更有可能与最近的订单进行交互,这些订单都在同一个分片上,那么包含最近订单的分片将接收大部分流量。

If you have a monotonically increasing shard key, consider resharding your collection. 如果你有一个单调增加的分片键,可以考虑重新分片集合For advice on choosing a shard key see Choose a Shard Key.有关选择分片键的建议,请参阅选择分片键

If your data model requires sharding on a key that changes monotonically, consider using Hashed Sharding.如果您的数据模型需要对单调变化的键进行分片,请考虑使用哈希分片

Decreased Query Performance Over Time随着时间的推移查询性能下降

If you are noticing decreased query performance over time, it is possible that your cluster is performing scatter-gather queries.如果您注意到查询性能随着时间的推移而降低,那么您的集群可能正在执行分散-聚集查询

To evaluate if your cluster is performing scatter-gather queries, check if your most common queries include the shard key.要评估集群是否正在执行分散-聚集查询,请检查最常见的查询是否包括分片键。

If you include the shard key in your queries, check if your shard key is hashed. 如果您在查询中包含分片键,请检查您的分片键是否经过哈希处理。With Hashed Sharding, documents are not stored in ascending or descending order of the shard key field value. 使用哈希分片,文档不会按照分片键字段值的升序或降序存储。Performing range based queries on the shard key value on data that is not stored in ascending or descending order results in less performant scatter-gather queries. 对未按升序或降序存储的数据执行基于范围的分片键值查询会导致性能较差的分散-聚集查询。If range based queries on your shard key are a common access pattern, consider resharding your collection.如果对您的分片键进行基于范围的查询是一种常见的访问模式,请考虑重新分片集合

If you do not include the shard key in your most common queries, it is possible that you could increase performance by resharding your collection. 如果您在最常见的查询中不包括分片键,则可以通过重新分片集合来提高性能。For advice on choosing a shard key see Choose a Shard Key.