清除大
标志
本页内容
如果 MongoDB 无法分割超过指定的范围大小,MongoDB 将数据块标记为 巨型。
以下步骤概述了手动清除 巨型
标志的过程。
步骤
可分割数据块
从数据块中清除 巨型
标志的推荐手动方法是尝试分割数据块。如果数据块可以分割,MongoDB 将在成功分割数据块后移除该标志。
找到 jumbo
数据块。
运行 sh.status(true)
来找到标签为 jumbo
的数据块。
sh.status(true)
例如,以下来自 sh.status(true) 的输出显示了具有分片键范围 { "x" : 2 } -->> { "x" : 4 }
的数据块是 jumbo
。
--- Sharding Status --- sharding version: { ... } shards: ... databases: ... test.foo shard key: { "x" : 1 } chunks: shard-b 2 shard-a 2 { "x" : { "$minKey" : 1 } } -->> { "x" : 1 } on : shard-b Timestamp(2, 0) { "x" : 1 } -->> { "x" : 2 } on : shard-a Timestamp(3, 1) { "x" : 2 } -->> { "x" : 4 } on : shard-a Timestamp(2, 2) jumbo { "x" : 4 } -->> { "x" : { "$maxKey" : 1 } } on : shard-b Timestamp(3, 0)
拆分 jumbo
数据块。
使用 sh.splitAt()
或 sh.splitFind()
来拆分 jumbo
数据块。
sh.splitAt( "test.foo", { x: 3 })
MongoDB 在成功拆分数据块后会移除 jumbo
标志。
不可分割的数据块
在某些情况下,MongoDB 不能拆分不再是 jumbo
的数据块,例如具有单个分片键值的范围的数据块。因此,您不能拆分数据块以清除标志。
在这种情况下,您可以选择更改分片键,以便数据块可以分割,或者手动清除标志。
优化分片键
MongoDB提供了refineCollectionShardKey
命令。使用refineCollectionShardKey
命令,您可以通过向现有键添加后缀字段或字段来优化集合的分片键。通过向分片键添加新字段,不可分割的大块可以变为可分割的。
查找大块
。
运行 sh.status(true)
来找到标签为 jumbo
的数据块。
sh.status(true)
例如,以下从 sh.status(true)
的输出显示,对于分片集合 test.orders
,具有分片键范围 { "status" : "A" } -->> { "status" : "D" }
的块和具有范围 { "status" : "D" } -->> { "status" : "P" }
的块都是 jumbo
。
--- Sharding Status --- sharding version: { ... } shards: ... databases: ... test.orders shard key: { "status" : 1 } unique: false balancing: true chunks: shardA 2 shardB 2 { "status" : { "$minKey" : 1 } } -->> { "status" : "A" } on : shardB Timestamp(3, 0) { "status" : "A" } -->> { "status" : "D" } on : shardA Timestamp(5, 1) jumbo { "status" : "D" } -->> { "status" : "P" } on : shardA Timestamp(4, 2) jumbo { "status" : "P" } -->> { "status" : { "$maxKey" : 1 } } on : shardB Timestamp(5, 0)
优化 test.orders
集合的分片键。
为了解决键 status
的低基数,优化 test.orders
集合的键。例如,将 order_id
和 customer_id
字段作为后缀添加到当前的分片键中;即优化后的分片键将是 { status: 1, order_id: 1, customer_id: 1 }
。
首先,如果索引尚未存在,则创建支持分片键
{ status: 1, order_id: 1, customer_id: 1 }
的索引
。db.orders.createIndex( { status: 1, order_id: 1, customer_id: 1 } ) 有关优化分片键的额外索引考虑事项,请参阅 索引考虑事项。
在
admin
数据库中,运行refineCollectionShardKey
命令将order_id
和customer_id
字段作为后缀添加到现有键中。db.adminCommand( { refineCollectionShardKey: "test.orders", key: { status: 1, order_id: 1, customer_id: 1 } } )
refineCollectionShardKey
命令更新 数据分片 和 区域分片 的范围,以包含新字段而不修改现有键字段的范围值。也就是说,分片键的优化不会立即影响跨分片或区域的块分布。任何未来的块拆分或迁移都作为常规分片操作的组成部分发生。
提示
在优化分片键后,可能不是所有文档都具有后缀字段。要填充缺少的分片键字段,请参阅 缺少的分片键字段。
在优化分片键之前,如果可能的话,确保集合中的所有或大多数文档都具有后缀字段,以避免之后需要填充字段。
手动清除不可分割块段的 jumbo
标志
要手动清除 jumbo
标志,可以使用 clearJumboFlag
命令。如果您清除的块段仍然超过块段大小,MongoDB 在尝试移动块段时将重新标记该块段为 jumbo
。
重要
只有当 首选方法 不可用时才使用此方法。
要手动清除标志,请按照以下步骤操作
找到 jumbo
块段。
运行 sh.status(true)
来找到标签为 jumbo
的数据块。
sh.status(true)
例如,以下从 sh.status(true) 的输出显示,具有分片键范围 { "x" : 2 } -->> { "x" : 3 }
的块段是 jumbo
。
--- Sharding Status --- sharding version: { ... } shards: ... databases: ... test.foo shard key: { "x" : 1 } chunks: shard-b 2 shard-a 2 { "x" : { "$minKey" : 1 } } -->> { "x" : 1 } on : shard-b Timestamp(2, 0) { "x" : 1 } -->> { "x" : 2 } on : shard-a Timestamp(3, 1) { "x" : 2 } -->> { "x" : 3 } on : shard-a Timestamp(2, 2) jumbo { "x" : 3 } -->> { "x" : { "$maxKey" : 1 } } on : shard-b Timestamp(3, 0)
运行 clearJumboFlag
命令。
从 admin
数据库中,运行 clearJumboFlag
,传入分片集合的命名空间以及
分片块的 范围
db.adminCommand( { clearJumboFlag: "test.foo", bounds: [{ "x" : 2 }, { "x" : 3 }] }) 包含在
jumbo
块中的分片键和值的 查找 文档db.adminCommand( { clearJumboFlag: "test.foo", find: { "x" : 2 } }) 注意
如果集合使用散列分片键,请不要使用
find
字段与clearJumboFlag
。对于散列分片键,请使用bounds
字段。