liveBook · Manning

Shard-key analyzer

As explained in MongoDB 8.0 in Action, Third Edition

analyzeShardKey is a MongoDB database command that returns metrics describing a candidate shard key's characteristics and read and write distribution.

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 }).

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains sampling, indexing, trial queries, and analysis commands, then shows returned metrics without interpreting every metric.
Examples
Demonstrates routes documents, a frequent routes query, query-sampling configuration, a compound index, trial queries, and analyzer output.
What it assumes
Assumes a MongoDB collection and candidate query pattern, while introducing query sampling and analyzer commands operationally.
What you can do after
Evaluate candidate shard keys with workload-derived metrics before applying sharding decisions.
How it connects
Connects shard-key selection with query sampling, compound indexes, sharding, and workload distribution.

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.