文档菜单
文档首页
/
MongoDB 手册
/ /

清除标志

本页内容

  • 过程

如果 MongoDB 无法分割超过指定的范围大小,MongoDB 将数据块标记为 巨型。

以下步骤概述了手动清除 巨型 标志的过程。

从数据块中清除 巨型 标志的推荐手动方法是尝试分割数据块。如果数据块可以分割,MongoDB 将在成功分割数据块后移除该标志。

1

连接mongosh 到一个 mongos.

2

运行 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)
3

使用 sh.splitAt()sh.splitFind() 来拆分 jumbo 数据块。

sh.splitAt( "test.foo", { x: 3 })

MongoDB 在成功拆分数据块后会移除 jumbo 标志。

在某些情况下,MongoDB 不能拆分不再是 jumbo 的数据块,例如具有单个分片键值的范围的数据块。因此,您不能拆分数据块以清除标志。

在这种情况下,您可以选择更改分片键,以便数据块可以分割,或者手动清除标志。

MongoDB提供了refineCollectionShardKey命令。使用refineCollectionShardKey命令,您可以通过向现有键添加后缀字段或字段来优化集合的分片键。通过向分片键添加新字段,不可分割的大块可以变为可分割的。

1

连接mongoshmongos

2

运行 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)
3

为了解决键 status 的低基数,优化 test.orders 集合的键。例如,将 order_idcustomer_id 字段作为后缀添加到当前的分片键中;即优化后的分片键将是 { status: 1, order_id: 1, customer_id: 1 }

  1. 首先,如果索引尚未存在,则创建支持分片键 { status: 1, order_id: 1, customer_id: 1 }索引

    db.orders.createIndex( { status: 1, order_id: 1, customer_id: 1 } )

    有关优化分片键的额外索引考虑事项,请参阅 索引考虑事项。

  2. admin 数据库中,运行 refineCollectionShardKey 命令将 order_idcustomer_id 字段作为后缀添加到现有键中。

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

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

提示

在优化分片键后,可能不是所有文档都具有后缀字段。要填充缺少的分片键字段,请参阅 缺少的分片键字段。

在优化分片键之前,如果可能的话,确保集合中的所有或大多数文档都具有后缀字段,以避免之后需要填充字段。

要手动清除 jumbo 标志,可以使用 clearJumboFlag 命令。如果您清除的块段仍然超过块段大小,MongoDB 在尝试移动块段时将重新标记该块段为 jumbo

重要

只有当 首选方法 不可用时才使用此方法。

要手动清除标志,请按照以下步骤操作

1

连接mongoshmongos

2

运行 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)
3

admin 数据库中,运行 clearJumboFlag,传入分片集合的命名空间以及

  • 分片块的 范围

    db.adminCommand( {
    clearJumboFlag: "test.foo",
    bounds: [{ "x" : 2 }, { "x" : 3 }]
    })
  • 包含在 jumbo 块中的分片键和值的 查找 文档

    db.adminCommand( {
    clearJumboFlag: "test.foo",
    find: { "x" : 2 }
    })

    注意

    如果集合使用散列分片键,请不要使用 find 字段与 clearJumboFlag。对于散列分片键,请使用 bounds 字段。

提示

另请参阅

返回

停止拆分集合

本页内容