[SERVER-61656] Last point TS Opt M6: $topn(1) and/or $top Created: 19/Nov/21  Updated: 29/Oct/23  Resolved: 13/Apr/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.0.0-rc0

Type: New Feature Priority: Major - P3
Reporter: Ruslan Abdulkhalikov (Inactive) Assignee: Alya Berciu
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-61659 TS Last Point opt: final test plan re... Closed
Backwards Compatibility: Fully Compatible
Participants:

 Description   

A query like this:

 

db.coll.aggregate([
  {$group: {
    _id: "$meta.a",
     mostRecent: {$topN: {
       n: 1,
       sortBy: {ts: -1},
       output: {
         ts: "$ts",
         measureA: "$measureA",
         measureB: "$measureB"
       }
     }}
  }}
])

or

db.coll.aggregate([
  {$group: {
    _id: "$meta.a",
     mostRecent: {$top: {
       sortBy: {ts: -1},
       output: {
         ts: "$ts",
         measureA: "$measureA",
         measureB: "$measureB"
       }
     }}
  }}
])

Will be rewritten to something like this to get the last point optimization:

db.measurements.aggregate([
   {$sort: {"meta.a": 1, "ts": -1}},
   {$group: {
     _id: "$sensorId”,
     ts: {$first: "$ts”},
     measureA: {$first: "$measureA"},
     measureB: {$first: "$measureB"}
   }}
])

 



 Comments   
Comment by Githook User [ 13/Apr/22 ]

Author:

{'name': 'Alya Berciu', 'email': 'alya.berciu@mongodb.com', 'username': 'alyacb'}

Message: SERVER-61656 Extend lastpoint rewrite to $top, $bottom, $topN, $bottomN
Branch: master
https://github.com/mongodb/mongo/commit/3956cbffd669f9ceeeb0733730aa0b643e3bcc35

Comment by Steve Tarzia [ 15/Mar/22 ]

kyle.suarez , to answer your Jan 14 question, no, it's rewriting certain "last point" $topN queries into the format that will take advantage of a DISTINCT_SCAN.

Note that there's a slack thread open right now to clarify whether we want to prioritize $top or $topN (or do both).

Comment by Kyle Suarez [ 14/Jan/22 ]

Does this ticket involve optimizing the new $topN accumulator specifically?

Generated at Thu Feb 08 05:52:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.