Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-56581

Flip window-function sortBy and bounds to save space

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      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}, ... }}
      

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            david.percy@mongodb.com David Percy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: