Hướng dẫn mongodb aggregate performance
On this page
Aggregation pipeline operations have an optimization phase which attempts to reshape the pipeline for improved performance. To see how the optimizer transforms a particular aggregation pipeline, include the Optimizations are subject to change between releases. In addition to learning about the aggregation pipeline optimizations performed during the optimization phase, you will also see how to improve aggregation pipeline performance using indexes and document filters. See Improve Performance with Indexes and Document Filters. 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. For an aggregation pipeline that contains a projection stage ( If an aggregation pipeline contains multiple projection and/or Consider a pipeline of the following stages:
The optimizer breaks up the
The The The NoteAfter optimization, the filter When you have a sequence with
During the optimization phase, the optimizer transforms the sequence to the following:
When possible, when the pipeline has the For example, if the pipeline consists of the following stages:
The optimizer can add the same
When you have a sequence with
During the optimization phase, the optimizer transforms the sequence to the following:
When possible, the optimization phase coalesces a pipeline stage into its predecessor. Generally, coalescence occurs after any sequence reordering optimization. Changed in version 4.0. When a For example, if the pipeline consists of the following stages:
During the optimization phase, the optimizer coalesces the sequence to the following:
This allows the sort operation to only maintain the top NoteSequence Optimization with $skipWhen a
Then the second When a
Then the second
When a
Then the second
When a For example, a pipeline contains the following sequence:
The optimizer can coalesce the
MongoDB can use the slot-based query execution engine to execute certain pipeline stages when specific conditions are met. In most cases, the slot-based engine provides improved performance and lower CPU and memory costs compared to the classic query engine. To verify that the slot-based engine is used, run the aggregation with the The following sections describe:
New in version 5.2. Starting in version 5.2, MongoDB uses the slot-based execution query engine to execute
When the slot-based query execution engine is used for
New in version 6.0. Starting in version 6.0, MongoDB can use the slot-based execution query engine to execute
When the slot-based query execution engine is used for
The following sections show how you can improve aggregation performance using indexes and document filters. The query planner analyzes an aggregation pipeline to determine if indexes can be used to improve pipeline performance. The following list shows some pipeline stages that can use indexes: $match stage$match can use an index to filter documents if
$match is the first stage in a pipeline.$sort stage$sort can use an index if
$sort is not preceded by a $project , $unwind , or
$group stage.$group stage
See $geoNear stage$geoNear can use a geospatial index. $geoNear must be the first stage in an aggregation pipeline.Starting in MongoDB 4.2, in some cases, an aggregation pipeline
can use a Note
Indexes can cover queries in an aggregation pipeline. A covered query uses an index to return all of the documents and has high performance. If your aggregation operation requires only a subset of the documents in a collection, filter the documents first:
A pipeline contains a sequence of
The optimizer performs
MongoDB increases the TipSee also: |