Difference in Performance with change in stages [Solved]

Hi,
@DHz

db.coll.aggregate([{$skip:10}, {$limit: 5}]) < Option 1
db.coll.aggregate([{$limit: 15}, {$skip: 10}]) < Option 2
How’s the second more efficient than the first if,
result is a 100 records, and the 1’st stage skips 10 so it’s left with 90 and then limits it to 5
second stage it limits to 15 but from the 100 records obtained and then skips 10 from the 15.
So exactly how is the efficiency achieved by changing stages in the second option?

Thanks.

My explanation is that the input sizes for each stage are larger in the first case than in the second. That is, in the first case, let say We have an input size of 10000 elements, $skip works with 10000 elements and $limit with 9990 elements. In the second case, $limit receives 10000 elements, but $skip just gets 15 elements to work with. Does that seem sensible?

Regards.

2 Likes

Absolutely. Wonder how I missed that.

Thanks a tonne.

1 Like