Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

refineCollectionShardKey

在此页面上

  • 定义
  • 语法
  • 访问控制
  • 注意事项
  • 举例
refineCollectionShardKey

通过添加新字段作为现有键的后缀,修改集合的片键。优化集合的分片键可以解决由于关联基数不足而导致现有键产生巨型(即不可分割)数据段的情况。

注意

数据分布

作为优化分片键的一部分, refineCollectionShardKey命令会更新数据段范围区域范围以纳入新字段,而无需修改现有键字段的范围值。 也就是说,分片键的细化不会立即影响跨分片或区域的数据段分布。 任何未来的数据段分割或迁移都会作为常规分片操作的一部分发生。

注意

要使用refineCollectionShardKey命令,分片集群的特征兼容性版本 (fcv)必须为4.4

refineCollectionShardKey命令必须针对admin数据库运行,并采用以下形式:

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

refineCollectionShardKey 命令拥有以下字段:

字段
类型
说明
字符串

sharded collection的命名空间,采用"<database>.<collection>"形式。

文档

指定要用作集合的新分片键的一个或多个字段的文档。

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

重要

  • 新键必须以现有分片键开头。

  • 在运行refineCollectionShardKey 命令 之前 ,新密钥必须得到索引的支持,该索引必须存在 。有关更多信息,请参阅索引注意事项

对于后缀字段,将字段值设置为:

提示

另请参阅:

使用访问控制运行时,用户必须对refineCollectionShardKey 数据库和/或集合 具有 操作权限才能运行命令。也就是说,用户必须具有授予以下 权限 的 角色

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

内置 clusterManager 角色提供了相应的特权。

  • 索引存在性

    在运行命令 之前 ,必须存在支持命令指定 的索引。

    支持索引是以新的分片键规范开头的索引;即索引前缀与新的分片键规范匹配。 也就是说,要将分片键从{ x: 1 }更改为{ x: 1, y: 1 } ,必须存在以{ x: 1, y: 1 }开头的索引;例如

    • { x: 1, y: 1 }

    • { x: 1, y: 1, a: 1, b: 1}

    注意

    • 支持索引不能是部分索引。

    • 支持索引不能是稀疏索引。

    • 如果集合使用非simple排序规则,则支持索引必须指定{ locale: "simple" }排序规则。

  • 唯一索引

    如果当前分片索引具有唯一性约束,则新的分片键索引也必须具有唯一性约束。

    After creating the unique index to support the new shard key, drop the old shard key index before running refineCollectionShardKey.
    Also, if the current shard index has a unique constraint, then the new shard key cannot specify "hashed" for any of its fields.
  • 索引排序规则
    如果分片collection具有非simple默认排序规则,则索引必须包含具有{ locale : "simple" }的排序规则文档。 对于字段支持分片键模式的索引,必须至少有一个索引采用简易排序规则。

警告

请勿修改任何当前分片键字段的范围或哈希类型。这会导致数据不一致。例如,请勿将分片密钥从 { customer_id: 1 } 修改为 { customer_id: "hashed", order_id: 1 }

要在test数据库中设置示例,请执行以下操作:

  1. 如果尚未启用分片,请在数据库上启用分片:

    sh.enableSharding("test")
  2. 使用以下shardCollection 操作对orders test数据库中的collection进行分片。该操作使用customer_id字段作为初始分片键:

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

要将分片键修改为customer_id字段和order_id字段{ customer_id: 1, order_id: 1 }

  1. Create the index 以支持新的分片键(如果索引尚不存在)。

    db.getSiblingDB("test").orders.createIndex( { customer_id: 1, order_id: 1 } )
  2. 运行refineCollectionShardKey命令以将order_id字段添加为后缀:

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

成功完成该命令后,集合的分片键已更改为{ customer_id: 1, order_id: 1 } 。 要进行验证,您可以运行sh.status()

提示

优化分片键后,可能并非集合中的所有文档都具有后缀字段。要填充缺失的分片键字段,请参阅缺失的分片键字段

在细化分片键之前,请确保集合中的所有或大多数文档都具有后缀字段(如果可能),以避免后期再填充字段。

要在test数据库中设置示例,请执行以下操作:

  1. 如果尚未启用分片,请在数据库上启用分片:

    sh.enableSharding("test")
  2. test数据库中创建caféscollection,指定法语fr作为默认排序规则。

    db.getSiblingDB("test").createCollection( "cafés", { collation: { locale: "fr" } } );
  3. 使用customer_id字段作为初始分片键对collection进行分片。由于该collection具有默认的fr排序规则而不是simple排序规则,因此shardCollection命令必须包含collation: { locale: "simple" }选项:

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

要将分片键修改为customer_id字段和order_id字段{ customer_id: 1, order_id: 1 }

  1. Create the index以支持新的分片键(如果该索引尚不存在)。 由于该collection使用非简单排序规则,因此索引必须包含collation: { locale: "simple" }选项。

    db.getSiblingDB("test").cafés.createIndex(
    { customer_id: 1, order_id: 1 },
    { collation: { locale: "simple" } }
    )
  2. 运行refineCollectionShardKey命令以将order_id字段添加为后缀:

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

成功完成该命令后,集合的分片键已更改为{ customer_id: 1, order_id: 1 } 。 要进行验证,您可以运行sh.status()

提示

优化分片键后,可能并非集合中的所有文档都具有后缀字段。要填充缺失的分片键字段,请参阅缺失的分片键字段

在细化分片键之前,请确保集合中的所有或大多数文档都具有后缀字段(如果可能),以避免后期再填充字段。

提示

另请参阅:

后退

mergeChunks

来年

removeShard