샤드 키 선택과 데이터 분포 점검 (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) 보고한다.
사용 시점
새 컬렉션을 샤딩하기 전, hot shard나 불균등한 증가가 발견될 때, reshardCollection을 결정하기 전.
주의
analyzeShardKey는 key를 지원하는 인덱스가 필요하고 큰 컬렉션에서는 문서를 샘플링하므로 피크 시간에 읽기 부하를 추가한다. query sampling이 켜져 있는 동안 샘플은 config.sampledQueries에 기록되므로 분석이 끝나면 끈다. 단조 증가 key(ObjectId, timestamp)는 모든 insert를 한 샤드에 몰리게 한다.