-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Optimization
When two $setWindowFields stages appear in a row, it may be beneficial to combine them into one stage.
For example, this query:
{$setWindowFields: { partitionBy: "$zip", output: {total: {$sum: "$x"}}, }}, {$setWindowFields: { partitionBy: "$zip", output: {avg: {$avg: "$x"}}, }},
has the same result as this one:
{$setWindowFields: { partitionBy: "$zip", output: { total: {$sum: "$x"}, avg: {$avg: "$x"}, }, }},
After desugaring and dropping a redundant $sort (SERVER-55464), the original query is:
{$sort: {zip: 1}}, {$_internalSetWindowFields: { partitionBy: "$zip", output: {total: {$sum: "$x"}}, }}, {$sort: {zip: 1}}, {$_internalSetWindowFields: { partitionBy: "$zip", output: {avg: {$avg: "$x"}}, }},
I think a valid set of criteria for combining the two _internalSetWindowFields stages would be:
- The second stage does not read from any 'output' field of the first.
- The two stages don't have any 'output' field name in common.
- The two stages have identical partitionBy and sortBy arguments.
- is related to
-
SERVER-56574 Coalesce $setWindowFields stages with compatible sortBy arguments and window functions
- Backlog