[SERVER-28592] Move $sample earlier in the aggregation pipeline Created: 03/Apr/17  Updated: 08/Nov/18  Resolved: 07/Nov/18

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 4.1.5

Type: New Feature Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Jacob Evans
Resolution: Done Votes: 5
Labels: QFB, asya, bi-performance, optimization
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Problem/Incident
Backwards Compatibility: Fully Compatible
Sprint: Query 2018-11-05, Query 2018-11-19
Participants:
Case:
Linked BF Score: 0

 Description   

When $sample is the first stage of an aggregation pipeline, the executor optimizes it with storage-engine support. This optimization could be extended to more use cases by moving $sample prior to stages that don't alter the number of documents that pass through it, including

  • $project
  • $addFields
  • $lookup
  • $sort

While most applications can ensure the $sample is first in the pipeline, there is one important use case where this is not possible: views. It's common for our own tools (mongodrdl, compass) to sample views, and often those views contain a pipeline consisting of only the above stages, and MongoDB will tack the $sample to the end of the view's pipeline. This optimization will improve the performance of that $sample by pushing it down to the storage engine.



 Comments   
Comment by Githook User [ 07/Nov/18 ]

Author:

{'name': 'Jacob Evans', 'email': 'jacob.evans@10gen.com'}

Message: SERVER-28592 Fixed line-wrapping comment
Branch: master
https://github.com/mongodb/mongo/commit/1f628d2069bda8ccc444c3331648618937f97bd8

Comment by Githook User [ 07/Nov/18 ]

Author:

{'name': 'Jacob Evans', 'email': 'jacob.evans@10gen.com'}

Message: SERVER-28592 Move $sample earlier in the aggregation pipeline
Branch: master
https://github.com/mongodb/mongo/commit/7fadb5dc1ddecb37617b2d927960cd15da8172b2

Generated at Thu Feb 08 04:18:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.