Navigation
This version of the documentation is archived and no longer supported.

Aggregation Pipeline Optimization

Changed in version 2.4.

Aggregation pipeline operations have an optimization phase which attempts to rearrange the pipeline for improved performance.

Pipeline Sequence Optimization

$sort + $skip + $limit Sequence Optimization

When you have a sequence with $sort followed by a $skip followed by a $limit, an optimization occurs that moves the $limit operator before the $skip operator. For example, if the pipeline consists of the following stages:

{ $sort: { age : -1 } },
{ $skip: 10 },
{ $limit: 5 }

During the optimization phase, the optimizer transforms the sequence to the following:

{ $sort: { age : -1 } },
{ $limit: 15 }
{ $skip: 10 }

Note

The $limit value has increased to the sum of the initial value and the $skip value.

The optimized sequence now has $sort immediately preceding the $limit. See $sort for information on the behavior of the $sort operation when it immediately precedes $limit.

$limit + $skip + $limit + $skip Sequence Optimization

When you have a continuous sequence of a $limit pipeline stage followed by a $skip pipeline stage, the optimization phase attempts to arrange the pipeline stages to combine the limits and skips. For example, if the pipeline consists of the following stages:

{ $limit: 100 },
{ $skip: 5 },
{ $limit: 10},
{ $skip: 2 }

During the intermediate step, the optimizer reverses the position of the $skip followed by a $limit to $limit followed by the $skip.

{ $limit: 100 },
{ $limit: 15},
{ $skip: 5 },
{ $skip: 2 }

The $limit value has increased to the sum of the initial value and the $skip value. Then, for the final $limit value, the optimizer selects the minimum between the adjacent $limit values. For the final $skip value, the optimizer adds the adjacent $skip values, to transform the sequence to the following:

{ $limit: 15 },
{ $skip: 7 }

Projection Optimization

The aggregation pipeline can determine if it requires only a subset of the fields in the documents to obtain the results. If so, the pipeline will only use those required fields, reducing the amount of data passing through the pipeline.