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

Last point TS Opt M3: Query rewrite for two {+-meta, +ts} cases

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.0.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Fully Compatible
    • 178

      Implement the appropriate query rewrite the 2 remaining cases with {+-meta, +ts}.  Assuming that we extend the indexes to both (min,max) and (max,min) the rewrites become:

      For reference, here's the case we already did in Milestone :2 

      index({meta.sensorId: 1, meta.sensorId: -1})
      
      db.telemetry.aggregate([
          {$sort: {"metadata.sensorId": 1, "timestamp": -1}},
          {$group: {
            _id: "$metadata.sensorId",
            ts: {$first: "$timestamp"},
            temp: {$first: "$temp"}
          }}
       ])

      translates to:

      db.system.buckets.telemetry.explain("executionStats").aggregate([
         {$sort: {"meta.sensorId": 1, "control.max.timestamp": -1}},
         {$group: {
            _id: "$meta.sensorId",
            bucket: {$first: "$_id"},
         }},
         {$lookup: {
            from: "system.buckets.telemetry",
            foreignField: "_id",
            localField: "bucket",
            as: "bucket_data",
            pipeline:[
               {$_internalUnpackBucket: {
                  timeField:"timestamp",
                  metaField:"tags",
                  bucketMaxSpanSeconds:NumberInt("60")
               }},
               {$sort: {"timestamp": -1}},
               {$limit:1}
            ]
         }},
         {$unwind: "$bucket_data"},
         {$replaceWith:{
            _id: "$_id",
            timestamp: "$bucket_data.timestamp",
            temp: "$bucket_data.temp"
         }}
      ]);

      This is adapted as follows:

       

      milestone user index $sort $group accumulator
      2 (done) {meta:1, ts:-1} {meta:1, control.max.timestamp:-1} $first
      3 {meta:-1, ts:-1} {meta:-1, control.max.timestamp:-1} $first
      3 {meta:1, ts:1} {meta:1, control.max.timestamp:1} $last
      3 {meta:-1, ts:1} {meta:-1, control.max.timestamp:1} $last

       

      This tickets covers the last two rows above.

       

            Assignee:
            alya.berciu@mongodb.com Alya Berciu
            Reporter:
            steve.tarzia@mongodb.com Steve Tarzia
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: