refineCollectionShardKey

On this page本页内容

Definition定义

refineCollectionShardKey

New in version 4.4.在版本4.4中新增

Modifies the collection's shard key by adding new field(s) as a suffix to the existing key. 通过向现有键添加新字段作为后缀,修改集合的分片键Refining a collection's shard key can address situations where the existing key has led to jumbo (i.e. indivisible) chunks due to insufficient cardinality.优化集合的分片键可以解决现有密钥由于基数不足而导致巨大(即不可分割)块的情况。

Note注意
Data Distribution数据分布

As part of refining the shard key, the refineCollectionShardKey command updates the chunk ranges and zone ranges to incorporate the new fields without modifying the range values of the existing key fields. 作为细化分片键的一部分,refineCollectionShardKey命令更新块范围区域范围,以合并新字段,而不修改现有键字段的范围值。That is, the refinement of the shard key does not immediately affect the distribution of chunks across shards or zones. 也就是说,分片键的细化不会立即影响分片或区域之间的块分布。Any future chunk splits or migration occur as part of the routine sharding operations.任何未来的块分割或迁移都会作为常规分片操作的一部分发生。

Syntax语法

Note注意

To use the refineCollectionShardKey command, the sharded cluster must have feature compatibility version (fcv) of 4.4.要使用refineCollectionShardKey命令,分片集群的功能兼容性版本(fcv)必须为4.4

The refineCollectionShardKey command must be run against the admin database and has the following form:refineCollectionShardKey命令必须针对admin数据库运行,格式如下:

db.adminCommand( {
   refineCollectionShardKey: "<database>.<collection>",
   key: { <existing key specification>, <suffix1>: <1|"hashed">, ... }
} )

Command Fields命令字段

The refineCollectionShardKey command has the following fields:refineCollectionShardKey命令具有以下字段:

Field字段Type类型Description描述
refineCollectionShardKeystring

The namespace of the sharded collection in the form "<database>.<collection>".分片集合的名称空间,格式为"<database>.<collection>"

keydocument

The document that specifies the field or fields to use as the new shard key for the collection.指定要用作集合的新分片键的一个或多个字段的文档。

{ <existing key specification>, <suffix1>: <1|"hashed">, ... }

Important重要

For the suffix fields, set the field values to either:对于后缀字段,将字段值设置为:

Tip提示
See also: 参阅:

Access Control访问控制

When running with access control, the user must have the refineCollectionShardKey privilege actions on database and/or collection to run the command. 当使用访问控制运行时,用户必须对数据库和/或集合具有refineCollectionShardKey权限操作才能运行该命令。That is, a user must have a role that grants the following privilege:即,用户必须具有授予以下权限角色

{ resource: { db: <database>, collection: <collection> }, actions: [ "refineCollectionShardKey" ] }

The built-in clusterManager role provides the appropriate privileges.内置的clusterManager角色提供了适当的权限。

Considerations注意事项

Index Considerations索引注意事项

  • Index Existence索引存在性

    An index that supports the command's specified key must exist prior to running the command.在运行命令之前,必须存在支持命令指定的索引。

    A supporting index is an index that starts with the new shard key specification; i.e. the index prefix matches the new shard key specification. 支持索引是以新的分片键规范开头的索引;即,索引前缀匹配新的分片键规范。That is, to change the shard key to { x: 1, y: 1 } from { x: 1 }, and index that starts with { x: 1, y: 1 } must exist; e.g.也就是说,要将分片键从{ x: 1 }更改为{ x: 1, y: 1 },必须存在以{ x: 1, y: 1 }开头的索引;如:

    • { x: 1, y: 1 }
    • { x: 1, y: 1, a: 1, b: 1}
    Note注意
    • The supporting index cannot be a partial index.支持索引不能是部分索引
    • The supporting index cannot be a sparse index.支持索引不能是稀疏索引
    • If the collection uses a non-simple collation, the supporting index must specify { locale: "simple" } collation.如果集合使用非simple排序规则,则支持索引必须指定{ locale: "simple" }排序规则。
  • Unique Index唯一索引

    If the current shard index has a uniqueness constraint, the new shard key index must also have a unique constraint.如果当前分片索引具有唯一性约束,则新的分片键索引也必须具有唯一性限制。

    After creating the unique index to support the new shard key, drop the old shard key index before running refineCollectionShardKey.创建唯一索引以支持新的分片键后,在运行refineCollectionShardKey之前删除旧的分片键索引。
    Also, if the current shard index has a unique constraint, then the new shard key cannot specify "hashed" for any of its fields.此外,如果当前分片索引具有唯一约束,则新的分片键不能为其任何字段指定"hashed"
  • Index Collation索引排序规则
    If the sharded collection has a non-simple default collation, then the index must include a collation document with { locale : "simple" }. 如果分片集合具有非simple的默认排序规则,则索引必须包含带有{ locale : "simple" }的排序规则文档。At least one of the indexes whose fields support the shard key pattern must have the simple collation.字段支持分片键模式的索引中至少有一个索引必须具有简单排序规则。

Examples示例

To set up the example in the test database:要在test数据库中设置示例,请执行以下操作:

  1. Enable sharding on the database, if sharding is not already enabled:如果尚未启用分片,请在数据库上启用分片:

    sh.enableSharding("test")
  2. Use following shardCollection operation to shard the orders collection in the test database. 使用以下shardCollection操作来分片test数据库中的orders集合。The operation uses the customer_id field as the initial shard key:该操作使用customer_id字段作为初始分片键

    db.adminCommand( { shardCollection: "test.orders", key: { customer_id: 1 } } )

To modify the shard key to be the customer_id field and the order_id field { customer_id: 1, order_id: 1 },要将分片键修改为customer_id字段和order_id字段{ customer_id: 1, order_id: 1 }

  1. Create the index to support the new shard key if the index does not already exist.如果索引不存在,则创建索引以支持新的分片键。

    db.getSiblingDB("test").orders.createIndex( { customer_id: 1, order_id: 1 } )
  2. Run refineCollectionShardKey command to add the order_id field as a suffix:运行refineCollectionShardKey命令以添加order_id字段作为后缀:

    db.adminCommand( {
       refineCollectionShardKey: "test.orders",
       key: { customer_id: 1, order_id: 1 }
    } )

Upon successful completion of the command, the shard key for the collection has changed to { customer_id: 1, order_id: 1 }. 成功完成命令后,集合的分片键已更改为{ customer_id: 1, order_id: 1 }To verify, you can run sh.status().要进行验证,可以运行sh.status()

Tip提示

After you refine the shard key, it may be that not all documents in the collection have the suffix field(s). 细化分片键后,可能不是集合中的所有文档都有后缀字段。To populate the missing shard key field(s), see Missing Shard Key Fields.要填充缺少的分片键字段,请参阅缺少分片键字段

Before refining the shard key, ensure that all or most documents in the collection have the suffix fields, if possible, to avoid having to populate the field afterwards.在细化分片键之前,如果可能的话,请确保集合中的所有或大多数文档都具有后缀字段,以避免以后必须填充字段。

Collection with non-simple Collation具有非simple排序规则的集合

To set up the example in the test database:要在test数据库中设置示例,请执行以下操作:

  1. Enable sharding on the database, if sharding is not already enabled:如果尚未启用分片,请在数据库上启用分片:

    sh.enableSharding("test")
  2. Create the cafés collection in the test database, specifying French fr as the default collation.test数据库中创建cafés集合,指定French fr作为默认排序规则。

    db.getSiblingDB("test").createCollection( "cafés", { collation: { locale: "fr" } } );
  3. Shard the collection using customer_id field as the initial shard key. 使用customer_id字段作为初始分片键,对集合进行分片。Because the collection has a default fr collation and not a simple collation, the shardCollection command must include a collation: { locale: "simple" } option:由于集合具有默认的fr排序规则,而不是simple排序规则,因此shardCollection命令必须包含collation: { locale: "simple" }选项:

    db.adminCommand( {
       shardCollection: "test.cafés",
       key: { customer_id: 1 },
       collation: { locale: "simple" }
    } )

To modify the shard key to be both the customer_id field and the order_id field { customer_id: 1, order_id: 1 },要将分片键修改为customer_id字段和order_id字段{ customer_id: 1, order_id: 1 }

  1. Create the index to support the new shard key if the index does not already exist. 如果索引不存在,则创建索引以支持新的分片键。Because the collection uses a non-simple collation, the index must include the collation: { locale: "simple" } option.因为集合使用非简单排序规则,所以索引必须包含collation: { locale: "simple" }选项。

    db.getSiblingDB("test").cafés.createIndex(
       { customer_id: 1, order_id: 1 },
       { collation: { locale: "simple" } }
    )
  2. Run refineCollectionShardKey command to add the order_id field as a suffix:运行refineCollectionShardKey命令以添加order_id字段作为后缀:

    db.adminCommand( {
       refineCollectionShardKey: "test.cafés",
       key: { customer_id: 1, order_id: 1 }
    } )

Upon successful completion of the command, the shard key for the collection has changed to { customer_id: 1, order_id: 1 }. 成功完成命令后,集合的分片键已更改为{ customer_id: 1, order_id: 1 }To verify, you can run sh.status().要进行验证,可以运行sh.status()

Tip提示

After you refine the shard key, it may be that not all documents in the collection have the suffix field(s). 细化分片键后,可能不是集合中的所有文档都有后缀字段。To populate the missing shard key field(s), see Missing Shard Key Fields.要填充缺少的分片键字段,请参阅缺少分片键字段

Before refining the shard key, ensure that all or most documents in the collection have the suffix fields, if possible, to avoid having to populate the field afterwards.在细化分片键之前,如果可能的话,请确保集合中的所有或大多数文档都具有后缀字段,以避免以后必须填充字段。

Tip提示
See also: 参阅:
←  mergeChunksremoveShard →