-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Optimization
Right-unbounded windows can use a lot of space. For example:
{$setWindowFields: { sortBy: {time: 1}, output: { s: {$sum: "$x", window: {documents: ['current', 'unbounded']}}, } }}
The first output document has to read until the end of the partition to compute the sum.
Flipping the sortBy and bounds would compute the same result, but using less space:
{$setWindowFields: { sortBy: {time: -1}, output: { s: {$sum: "$x", window: {documents: ['unbounded', 'current']}}, } }}
If there is more than one window function in the stage, we would have to either flip all of them, or split up the stage.
Some window functions, like $rank or $expMovingAvg, can't be flipped. Internally they use a window of [unbounded, current], so flipping them would mean using a window of [current, unbounded], which is what we want to avoid.
Internally, we have to deal with the fact that $_internalSetWindowFields depends on a separate $sort stage. For example, after parsing we have:
{$sort: {time: 1}}, {$_internalSetWindowFields: { sortBy: {time: 1}, ... }}
We could have $_internalSetWindowFields look for a $sort stage before it. Or, it could emit a new $sort stage and let the $sort+$sort optimization clean it up:
{$sort: {time: 1}}, {$sort: {time: -1}}, {$_internalSetWindowFields: { sortBy: {time: -1}, ... }}
{$sort: {time: -1}}, {$_internalSetWindowFields: { sortBy: {time: -1}, ... }}
- related to
-
SERVER-56450 Allow range-based windows with descending sortBy
- Backlog
-
SERVER-56574 Coalesce $setWindowFields stages with compatible sortBy arguments and window functions
- Backlog