-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
Query Optimization
Currently, the cost of the stand-alone `LIMIT` does not take into account the fact that:
- the input stage will be prematurely terminated on LIMIT values that are smaller than collection size
- limit values larger than the collection size should be disregarded
Enterprise test> db.foo.find({a: {$gte: 0}}).hint({$natural: 1}).limit(1).explain().queryPlanner.winningPlan; { isCached: false, stage: 'LIMIT', costEstimate: 0.6246551 <- Fixed value that is incorrect estimatesMetadata: { ceSource: 'Metadata' }, limitAmount: 1, inputStage: { stage: 'COLLSCAN', costEstimate: 4.3291855, cardinalityEstimate: 10000, numDocsEstimate: 10000, estimatesMetadata: { ceSource: 'Histogram' }, filter: { a: { '$gte': 0 } }, direction: 'forward' } Enterprise test> db.foo.find({a: {$gte: 0}}).hint({$natural: 1}).limit(10000000000000000000000).explain().queryPlanner.winningPlan; { isCached: false, stage: 'LIMIT', costEstimate: 0.6246551, cardinalityEstimate: 10000, estimatesMetadata: { ceSource: 'Histogram' }, limitAmount: Long('9223372036854775807'), inputStage: { stage: 'COLLSCAN', costEstimate: 4.3291855, cardinalityEstimate: 10000, numDocsEstimate: 10000, estimatesMetadata: { ceSource: 'Histogram' }, filter: { a: { '$gte': 0 } }, direction: 'forward' } }