Duplicate $limit across $unionWith branch

XMLWordPrintableJSON

    • Query Optimization
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      $limit stages can be duplicated:

      [  
        { $unionWith: {   
            coll: "oldOrders",   
            pipeline: []
        }},
        {$limit: 2},
      ]
      

      Can become:

      [  
        {$limit: 2},
        { $unionWith: {   
            coll: "oldOrders",   
            pipeline: [{$limit: 2}]
        }},
        {$limit: 2},
      ]
      

      This can avoid reading a larger initial batch from both pipelines.

      Example:

      db.orders.drop();
      db.oldOrders.drop();
      for (var i = 0; i < 100; i++) {
        db.orders.insertOne({order: i});
        db.oldOrders.insertOne({oldOrder: i});
      }
      db.orders.explain("executionStats").aggregate([  
        { $unionWith: {   
            coll: "oldOrders",   
            pipeline: []
        }},
        {$limit: 2},
      ]) 

            Assignee:
            Unassigned
            Reporter:
            Vesko Karaganev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: