Docs HomeMongoDB Manual

Refine a Shard Key优化分片关键帧

New in version 4.4.

Refining a collection's shard key allows for a more fine-grained data distribution and can address situations where the existing key has led to jumbo chunks due to insufficient cardinality.细化集合的分片键可以实现更细粒度的数据分发,并可以解决现有键由于基数不足而导致巨型块的情况。

Warning

Do not modify the range or hashed type for any of the current shard key fields. 不要修改任何当前分片键字段的范围或哈希类型。It causes data inconsistencies. 它会导致数据不一致。For example, do not modify a shard key from { customer_id: 1 } to { customer_id: "hashed", order_id: 1 }.例如,不要将分片键从{ customer_id: 1 }修改为{ customer_id: "hashed", order_id: 1 }

Note

Starting in MongoDB 5.0, you can also reshard your collection by providing a new shard key for the collection. 从MongoDB 5.0开始,您还可以通过为集合提供新的分片键来重新分片集合To learn whether you should reshard your collection or refine your shard key, see Change a Shard Key.要了解是应该重新发布集合还是细化分片键,请参阅更改分片键

To refine a collection's shard key, use the refineCollectionShardKey command. 若要细化集合的分片键,请使用refineCollectionShardKey命令。The refineCollectionShardKey adds a suffix field or fields to the existing key to create the new shard key.refineCollectionShardKey向现有键添加一个或多个后缀字段,以创建新的分片键。

For example, you may have an existing orders collection in a test database with the shard key { customer_id: 1 }. 例如,您可能在test数据库中有一个现有的orders集合,其中包含分片键{ customer_id: 1 }You can use the refineCollectionShardKey command to change the shard key to the new shard key { customer_id: 1, order_id: 1 }:您可以使用refineCollectionShardKey命令将分片键更改为新的分片键{ customer_id: 1, order_id: 1 }

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