DBAtlas · 数据库运维图谱

检查分片键选择和数据分布(getShardDistribution、analyzeShardKey)

MongoDB分片

命令

// Data and document distribution of the collection across shards:
db.getSiblingDB("shop").musteri.getShardDistribution()
// Cardinality, frequency and monotonicity analysis of the current or candidate shard key:
db.adminCommand({ analyzeShardKey: "shop.musteri", key: { musteriId: 1 }, keyCharacteristics: true, readWriteDistribution: false })
// For read/write distribution, enable query sampling first (10 samples per second):
db.adminCommand({ configureQueryAnalyzer: "shop.musteri", mode: "full", samplesPerSecond: 10 })
db.adminCommand({ analyzeShardKey: "shop.musteri", key: { musteriId: 1 }, keyCharacteristics: false, readWriteDistribution: true })
// Turn sampling off when done:
db.adminCommand({ configureQueryAnalyzer: "shop.musteri", mode: "off" })

作用

显示集合数据在每个分片上的百分比;analyzeShardKey 报告当前或候选分片键的基数、最常见值和单调性,以及采样查询是定向到单个分片还是所有分片(scatter-gather)。

适用场景

对新集合分片之前、发现热点分片或增长不均时,以及决定是否执行 reshardCollection 之前。

注意

analyzeShardKey 需要支持该键的索引,并在大集合上对文档采样;高峰时段会增加读取负载。查询采样开启期间,样本会写入 config.sampledQueries;分析完成后将其关闭。单调递增的键(ObjectId、时间戳)会把所有插入堆到一个分片上。

在 DBAtlas 中打开 →

相关内容