Check shard key choice and data distribution (getShardDistribution, analyzeShardKey)
MongoDBShardingCommand
// 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" })What it does
Shows the percentage of a collection's data on each shard; analyzeShardKey reports the cardinality, most frequent values and monotonicity of the current or candidate shard key, and whether sampled queries target a single shard or all shards (scatter-gather).
When to use it
Before sharding a new collection, when a hot shard or uneven growth is noticed, and before deciding on reshardCollection.
Caution
analyzeShardKey needs an index that supports the key and samples documents on large collections; it adds read load at peak time. While query sampling is on, samples are written to config.sampledQueries; turn it off when the analysis is done. A monotonically increasing key (ObjectId, timestamp) piles all inserts onto one shard.