-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Optimization
For certain queries against the oplog, we can create a bounded collection scan if the query contains a predicate on the `ts` field since we know that `ts` will be monotonically increasing. The conditions require that the scan direction is forward, however it's fairly common to use a {$sort: {$natural: -1}} combined with limit(1) to find the latest oplog entry which matches some predicate.
For example, the following query
oplog.find({ts: {$gt: <timestamp>}}).sort({$natural: -1}).limit(1)
will scan the entire oplog even though it can stop at timestamp. There may also be an analogous optimization where the predicate is $lt and we can use the timestamp as a starting point for the backwards scan.