Explain plan can be used on a collection to get an explainable object.
> db.test1.explain().find({name:"hello"}).sort({emp_id:1})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "sample1.test1",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "hello"
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"name" : {
"$eq" : "hello"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"emp_id" : 1
},
"indexName" : "emp_id_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"emp_id" : [ ]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"emp_id" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "localhost.localdomain",
"port" : 27017,
"version" : "4.0.6",
"gitVersion" : "caa42a1f75a56c7643d0b68d3880444375ec42e3"
},
"ok" : 1
}
> db.test1.explain().find({name:"hello"}).sort({emp_id:1})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "sample1.test1",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "hello"
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"name" : {
"$eq" : "hello"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"emp_id" : 1
},
"indexName" : "emp_id_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"emp_id" : [ ]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"emp_id" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "localhost.localdomain",
"port" : 27017,
"version" : "4.0.6",
"gitVersion" : "caa42a1f75a56c7643d0b68d3880444375ec42e3"
},
"ok" : 1
}
In the above example "stage" is "IXSCAN" which means index is used for scanning the collection.If the stage is "COLSCAN" we have to use indexing in the collection to make the find and sort faster.
In the above query the index used is "indexName" : "emp_id_1".
Query planner is the default explain plan used in mongo.We can also use execution stats for the explain plan.
Execution plan:
* Include query planner
*More information will be provided
* We can know the time to execute the query
* We can know the number of documents returned.
* Documents are examined for describing the execution plan.
> exp = db.test1.explain("executionStats")
Explainable(sample1.test1)
> exp.find({"name":"world"})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "sample1.test1",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "world"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "world"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 0,
"totalDocsExamined" : 4,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "world"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 6,
"advanced" : 1,
"needTime" : 4,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 4
}
},
"serverInfo" : {
"host" : "localhost.localdomain",
"port" : 27017,
"version" : "4.0.6",
"gitVersion" : "caa42a1f75a56c7643d0b68d3880444375ec42e3"
},
"ok" : 1
}
All Plans Execution:
* It is lot like execution stats
* Additionally it also runs each available plan and look at stats.
> db.test1.explain("allPlansExecution").find({"name":"world"})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "sample1.test1",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "world"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "world"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 0,
"totalDocsExamined" : 4,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "world"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 6,
"advanced" : 1,
"needTime" : 4,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 4
},
"allPlansExecution" : [ ]
},
"serverInfo" : {
"host" : "localhost.localdomain",
"port" : 27017,
"version" : "4.0.6",
"gitVersion" : "caa42a1f75a56c7643d0b68d3880444375ec42e3"
},
"ok" : 1
}
Comments
Post a Comment