The shard-key analyzer uses sampled query statistics to evaluate a candidate shard key for an unsharded or sharded collection. It supports data-driven decisions about distribution and query behavior, beginning with MongoDB 7.0. section 9.3.5
Use the analyzer through this practical workflow:
- Identify a frequent query pattern and express its fields as a proposed compound shard key. section 9.3.5
- Enable full query sampling on the target collection, choosing an appropriate samples-per-second rate. section 9.3.5
- Create a supporting compound index, then run trial queries against real data to generate statistics. section 9.3.5
- Run analyzeShardKey with the proposed key and inspect metrics such as document counts, sampled documents, uniqueness, and distinct values. section 9.3.5
Analyzing a routes shard key
For sample_training.routes, sample queries use src_airport, dst_airport, and airline.name. Enable sampling with db.routes.configureQueryAnalyzer({ mode: "full", samplesPerSecond: 1 }), create the matching compound index, run representative queries, and execute db.routes.analyzeShardKey({ src_airport: 1, dst_airport: 1, "airline.name": 1 }).
