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

Rewrite adjacent $sort + $group with $first/$last to $group with $top/$bottom for timeseries

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Integration
    • Fully Compatible
    • QI 2024-03-18, QI 2024-04-01
    • 158

      Details can be found at SERVER-85213.

      This ticket includes any necessary performance investigation and adding additional perf workloads.

      Rough idea:

      1. In DocumentSourceInternalUnpackBucket::doOptimizeAt(), if it can find $sort followed by $group, then try to optimize
      2. If so, then rewrite the $first and/or $last into $top and/or $bottom with the same sort key(s) as the $sort stage. We can use GroupProcessorBase's getMutableAccumulationStatements() to manipulate AccumulationStatement
      3. After rewriting, remove the previous $sort stage

      This optimization should not be applied when $sort has a limit.

      Update:

      1. To limit the scope of this ticket and perf impact further, we will apply this optimization only to cases where $_internalUnpackBucket followed by $sort followed by $group.
      2. This optimization isn't applied to lastpoint optimizable queries to not regress perf of them.
      3. Limit the number of $first and $last only to 2 as a safe limit as overhead of generating the same sort key multiple times for multiple rewritten $top/$bottom could be big if there are too many $first/$last.

            Assignee:
            yoonsoo.kim@mongodb.com Yoon Soo Kim (Inactive)
            Reporter:
            yoonsoo.kim@mongodb.com Yoon Soo Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: