Performance tips for sharded aggregations
All the recommended aggregation optimization outlined in Chapter 3, Optimizing Pipelines for Performance, equally apply to a sharded cluster. In fact, in most cases, these same recommendations, repeated as follows, become even more important when executing aggregations on sharded clusters:
- Sorting – use index sort: When the runtime has to split on a
$sort
stage, the shards part of the split pipeline running on each shard in parallel will avoid an expensive in-memory sort operation. - Sorting – use limit with sort: The runtime has to transfer fewer intermediate records over the network, from each shard performing the shards part of a split pipeline to the location that executes the pipeline's merger part.
- Sorting – reduce records to sort: If you cannot adopt point 1 or 2, moving a
$sort
stage to as late as possible in a pipeline will typically benefit performance in a sharded cluster. Wherever the$sort...