Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-33925

Queries that are known to return no results at plan time should optimize to EOF plan

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Query Optimization

      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.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            ian@mongodb.com Ian Whalen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: