Due to auto-generation of queries or user error, it is possible to issue a query to the server which cannot possibly return any results. One such query is
db.foo.find({$expr:{$eq:["a","b"]}});
as described by related ticket SERVER-33642. Since the constant string "a" is never equal to the constant string "b", this query can never match any documents. In fact, the server optimizes this expression to be a constant of false. However, it still runs a collection scan, filtering out each of the documents that do not match one by one:
> db.foo.drop(); > db.foo.insert([{}, {}, {}]); > db.foo.find({$expr:{$eq:["a","b"]}}).explain("executionStats"); { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.foo", "indexFilterSet" : false, "parsedQuery" : { "$expr" : { "$const" : false } }, "winningPlan" : { "stage" : "COLLSCAN", "filter" : { "$expr" : { "$const" : false } }, "direction" : "forward" }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 0, "executionTimeMillis" : 0, "totalKeysExamined" : 0, "totalDocsExamined" : 3, "executionStages" : { "stage" : "COLLSCAN", "filter" : { "$expr" : { "$const" : false } }, "nReturned" : 0, "executionTimeMillisEstimate" : 0, "works" : 5, "advanced" : 0, "needTime" : 4, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 3 } }, "serverInfo" : { "host" : "storchbox", "port" : 27017, "version" : "0.0.0", "gitVersion" : "unknown" }, "ok" : 1 }
Instead, the planner could notice that the plan is guaranteed to return no documents, and replace the entire execution tree with an EOF plan. EOF plans simply immediately return end-of-stream, without doing any wasteful query execution work.
- is related to
-
SERVER-33642 Alert client when parsing a query expression that will never be true
- Closed
-
SERVER-65049 Optimize away { $expr : true } in $match clauses
- Closed
-
SERVER-81863 Avoid a scan of either IDX or a collection if the filter is $alwaysFalse:
- Closed