Database Manual / Sharding

Shard Keys分片键

The shard key is either a single indexed field or multiple fields covered by a compound index that determines the distribution of the collection's documents among the cluster's shards.分片键是单个索引字段或由复合索引覆盖的多个字段,复合索引决定了集合文档在集群分片之间的分布。

MongoDB divides the span of shard key values (or hashed shard key values) into non-overlapping ranges of shard key values (or hashed shard key values). Each range is associated with a chunk, and MongoDB attempts to distribute chunks evenly among the shards in the cluster.MongoDB将分片键值(或散列分片键值)的范围划分为不重叠的分片键值范围(或散列的分片键值)。每个范围都与一个相关联,MongoDB试图在集群中的分片之间均匀分布块。

Diagram of the shard key value space segmented into smaller ranges or chunks.

The shard key has a direct relationship to the effectiveness of chunk distribution. 分片键与块分发的有效性有直接关系。See Choose a Shard Key.请参见选择分片键

Shard Key Indexes分片化关键指标

Sharded collections typically require an index that supports the shard key. 分片集合通常需要一个支持分片键的索引。The index can be an index on the shard key or a compound index where the shard key is a prefix of the index.索引可以是分片键上的索引,也可以是复合索引,其中分片键是索引的前缀

  • If the collection is empty, sh.shardCollection() creates the index on the shard key if such an index does not already exists.如果集合为空,sh.shardCollection()会在分片键上创建索引,如果这样的索引还不存在。
  • If the collection is not empty, you must create the index first before using sh.shardCollection().如果集合不为空,则必须先创建索引,然后再使用sh.shardCollection()

You cannot drop or hide an index if it is the only non-hidden index that supports the shard key.如果一个索引是唯一支持分片键的非隐藏索引,则不能删除隐藏该索引。

Starting in MongoDB 7.0.3, 6.0.12, and 5.0.22, you can drop the index for a hashed shard key. 从MongoDB 7.0.3、6.0.12和5.0.22开始,您可以删除哈希分片键的索引。For details, see Drop a Hashed Shard Key Index.有关详细信息,请参阅删除哈希分片键索引

Unique Indexes唯一索引

MongoDB can enforce a uniqueness constraint on a ranged shard key index. Through the use of a unique index on the shard key, MongoDB enforces uniqueness on the entire key combination and not individual components of the shard key.MongoDB可以对范围分片键索引强制执行唯一性约束。通过在分片键上使用唯一索引,MongoDB对整个键组合而不是分片键的单个组件强制执行唯一性。

For a ranged sharded collection, only the following indexes can be unique:对于范围分片集合,只有以下索引可以是唯一的

  • The index on the shard key分片键上的索引
  • A compound index where the shard key is a prefix一个复合索引,其中分片键是前缀
  • The default _id index; however, the _id index only enforces the uniqueness constraint per shard if the _id field is not the shard key.默认_id索引;但是,如果_id字段不是分片键,_id索引只会强制每个分片的唯一性约束。

Important

Sharded clusters do not enforce the uniqueness constraint on _id fields across the cluster when the _id field is not the shard key._id字段不是分片键时,分片集群不会对整个集群的_id字段强制执行唯一性约束。

If the _id field is the not the shard key the uniqueness constraint only applies to the shard that stores the document. This means that two or more documents can have the same _id value, provided they occur on different shards.如果_id字段不是分片键,则唯一性约束仅适用于存储文档的分片。这意味着两个或多个文档可以具有相同的_id值,只要它们出现在不同的分片上。

For example, consider a sharded collection with shard key {x: 1} that spans two shards A and B. 例如,考虑一个分片集合,其分片键{x:1}跨越两个分片A和B。Because the _id key is not the shard key, the collection could have a document with _id value 1 in shard A and another document with _id value 1 in shard B.因为_id键不是分片键,所以集合可以在分片A中有一个_id值为1的文档,在分片B中有另一个_id价值为1的文件。

In cases where the _id field is not the shard key, MongoDB expects applications to enforce the uniqueness of _id values across the shards._id字段不是分片键的情况下,MongoDB希望应用程序在分片之间强制执行_id值的唯一性。

The unique index constraints mean that:唯一的索引约束意味着:

  • For a to-be-sharded collection, you cannot shard the collection if the collection has multiple unique indexes unless the shard key is the prefix for all the unique indexes.对于要分片的集合,如果集合有多个唯一索引,则不能对集合进行分片,除非分片键是所有唯一索引的前缀。
  • For an already-sharded collection, you cannot create unique indexes on other fields unless the shard key is included as the prefix.对于已经分片的集合,除非分片键作为前缀包含在内,否则无法在其他字段上创建唯一索引。
  • A unique index stores a null value for a document missing the indexed field; that is a missing index field is treated as another instance of a null index key value. 唯一索引为缺少索引字段的文档存储一个null值;即缺失的索引字段被视为空索引键值的另一个实例。For more information, see Missing Document Field in a Unique Single-Field Index.有关详细信息,请参阅唯一单字段索引中缺少文档字段

To enforce uniqueness on the shard key values, pass the unique parameter as true to the sh.shardCollection() method:要强制分片键值的唯一性,请将unique参数作为true传递给sh.shardCollection()方法:

  • If the collection is empty, sh.shardCollection() creates the unique index on the shard key if such an index does not already exist.如果集合为空,sh.shardCollection()会在分片键上创建唯一索引(如果该索引不存在)。
  • If the collection is not empty, you must create the index first before using sh.shardCollection().如果集合不为空,则必须先创建索引,然后再使用sh.shardCollection()

Although you can have a unique compound index where the shard key is a prefix, if using unique parameter, the collection must have a unique index that is on the shard key.虽然你可以有一个唯一的复合索引,其中分片键是前缀,但如果使用unique参数,集合必须有一个位于分片键上的唯一索引。

You cannot specify a unique constraint on a hashed index.不能对哈希索引指定唯一约束。

To maintain uniqueness on a field that is not your shard key, see Unique Constraints on Arbitrary Fields.要保持非分片键字段的唯一性,请参阅任意字段的唯一约束

Missing Shard Key Fields缺少分片键段

Documents in sharded collections can be missing the shard key fields. 分片集合中的文档可能缺少分片键字段。To set missing shard key fields, see Set Missing Shard Key Fields.要设置缺少的分片键字段,请参阅设置缺少的分区键字段

Chunk Range and Missing Shard Key Fields块范围和缺失的分片键段

Missing shard key fields fall within the same chunk range as shard keys with null values. For example, if the shard key is on the fields { x: 1, y: 1 }, then:缺少的分片键字段与具有null值的分片键位于相同的块范围内。例如,如果分片键在字段{ x: 1, y: 1 }上,那么:

Document Missing Shard Key文档缺少分片键Falls into Same Range As落入与相同的范围
{ x: "hello" }{ x: "hello", y: null }
{ y: "goodbye" }{ x: null, y: "goodbye" }
{ z: "oops" }{ x: null, y: null }

Read/Write Operations and Missing Shard Key Fields读/写操作和缺失的分片键段

To target documents with missing shard key fields, you can use the { $exists: false } filter condition on the shard key fields. 要针对缺少分片键字段的文档,可以在分片键域上使用{ $exists: false }筛选条件。For example, if the shard key is on the fields { x: 1, y: 1 }, you can find the documents with missing shard key fields by running this query:例如,如果分片键位于字段{ x: 1, y: 1 }上,则可以通过运行以下查询找到缺少分片键字段的文档:

db.shardedcollection.find( { $or: [ { x: { $exists: false } }, { y: { $exists: false } } ] } )

If you specify a null equality match filter condition (e.g. { x: null }), the filter matches both those documents with missing shard key fields and those with shard key fields set to null.如果您指定了null相等匹配筛选条件(例如{ x: null }),则筛选器将匹配那些缺少分片键字段的文档和那些分片键域设置为null的文档。

Some write operations, such as a write with an upsert specification, require an equality match on the shard key. In those cases, to target a document that is missing the shard key, include another filter condition in addition to the null equality match. For example:一些写操作,例如带有upsert规范的写操作,需要分片键上的相等匹配。在这些情况下,要针对缺少分片键的文档,除了null相等匹配之外,还可以包含另一个筛选条件。例如:

{ _id: <value>, <shardkeyfield>: null } // _id of the document missing shard key

Use Shard Key Analyzer to Find Your Shard Key使用分片键分析器查找分片键

Starting in 7.0, MongoDB makes it easier to choose your shard key. 从7.0开始,MongoDB使选择分片键变得更加容易。You can use analyzeShardKey which calculates metrics for evaluating a shard key for an unsharded or sharded collection.您可以使用analyzeShardKey来计算用于评估未分片或分片集合的分片键的指标。 Metrics are based on sampled queries, allowing you to make a data-driven choice for your shard key.度量基于采样查询,允许您为分片键做出数据驱动的选择。

Enable Query Sampling启用查询采样

To analyze a shard key, you must enable query sampling on the target collection. For more information, see:要分析分片键,必须在目标集合上启用查询采样。有关更多信息,请参阅:

To monitor the query sampling process, use the $currentOp stage. 要监视查询采样过程,请使用$currentOp阶段。For an example, see Sampled Queries.有关示例,请参阅采样查询

Shard Key Analysis Commands分片化键分析命令

To analyze a shard key, see:要分析分片键,请参阅:

analyzeShardKey returns metrics about key characteristics of a shard key and its read and write distribution. The metrics are based on sampled queries.返回关于分片键的关键特性及其读写分布的度量。这些指标基于采样查询。

  • The keyCharacteristics field contains metrics about the cardinality, frequency, and monotonicity of the shard key.keyCharacteristics字段包含有关分片键的基数、频率和单调性的度量。
  • The readWriteDistribution field contains metrics about the query routing patterns and the load distribution of shard key ranges.readWriteDistribution字段包含有关查询路由模式和分片键范围负载分布的指标