The 2.4 query engine had a heuristic in which it would look for an "impossible match", i.e. query predicates that could not possibly match any documents. If such a query was identified, then query execution could be short-circuited. Impossible match analysis no longer exists in 2.6.x and 3.0.x versions, which can lead to less efficient queries in these newer versions (see repro steps above). The most obvious example of an "impossible match" is an empty $in predicate.
Note that if the index bounds are empty, the 2.6 engine will properly handle this and realize that it does not need to scan the index:
> db.version() 2.6.9 > t.drop() > t.insert({a: 1}) // Unindexed query has to do the collection scan. > t.find({a: {$in: []}}).explain().nscannedObjects 1 // Indexed query doesn't need to look at any index keys or docs. > t.ensureIndex({a: 1}) > t.find({a: {$in: []}}).explain().nscannedObjects 0
In 2.6 this behavior for indexed queries falls out of the regular query execution pipeline. In 2.4, however, the "impossible match" heuristic makes it so that empty bounds on even an unindexed field do not access any indices or data:
> db.version()
2.4.8
> t.drop()
> t.insert({a: 1})
// Unindexed query scans nothing due to "impossible match".
> t.find({a: {$in: []}}).explain().nscannedObjects
0
- is related to
-
SERVER-23732 Aggregation should optimize an irrelevant $sort preceding a $group
- Backlog