mongodb查看执行计划

时间:2021-08-22 16:47:52

mongodb中的集合是非结构化的,所以列的个数,类型之类的没有什么限制,很随意,构建一个测试集合

db.test.find()
{ “_id” : ObjectId(“57bd09252e819a8d920a0bbc”), “x” : 1 }
{ “_id” : ObjectId(“57bd09652e819a8d920a0bbd”), “x” : 1, “y” : 2 }
{ “_id” : ObjectId(“57bd09812e819a8d920a0bbe”), “x” : 1, “y” : 2, “z” : 3 }
{ “_id” : ObjectId(“57bd0a532e819a8d920a0bbf”), “x” : “aa”, “y” : 2, “z” : 3 }
{ “_id” : ObjectId(“57bd0a6a2e819a8d920a0bc0”), “x” : “aa”, “y” : 2, “z” : 3.3 }

查看x=1的记录的执行计划

db.test.find({x:1}).explain(“executionStats”)
{
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “baixyu.test”,
“indexFilterSet” : false,
“parsedQuery” : {
“x” : {
eq” : 1  
                          }  
                  },  
                  “winningPlan” : {  
                          “stage” : “COLLSCAN”,  
                          “filter” : {  
                                  “x” : {  
                                          “
eq” : 1
}
},
“direction” : “forward”
},
“rejectedPlans” : [ ]
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 3,
“executionTimeMillis” : 9,
“totalKeysExamined” : 0,
“totalDocsExamined” : 5,
“executionStages” : {
“stage” : “COLLSCAN”,
“filter” : {
“x” : {
“$eq” : 1
}
},
“nReturned” : 3,
“executionTimeMillisEstimate” : 0,
“works” : 7,
“advanced” : 3,
“needTime” : 3,
“needYield” : 0,
“saveState” : 0,
“restoreState” : 0,
“isEOF” : 1,
“invalidates” : 0,
“direction” : “forward”,
“docsExamined” : 5
}
},
“serverInfo” : {
“host” : “Dmall-PC”,
“port” : 27017,
“version” : “3.2.8”,
“gitVersion” : “ed70e33130c977bda0024c125b56d159573dbaf0”
},
“ok” : 1
}
添加并查看索引
db.test.createIndex({x:1})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
db.test.getIndexes();
[
{
“v” : 1,
“key” : {
“_id” : 1
},
“name” : “id“,
“ns” : “baixyu.test”
},
{
“v” : 1,
“key” : {
“x” : 1
},
“name” : “x_1”,
“ns” : “baixyu.test”
}
]
添加索引后再次查看执行计划
db.test.find({x:1}).explain(“executionStats”)
{
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “baixyu.test”,
“indexFilterSet” : false,
“parsedQuery” : {
“x” : {
“$eq” : 1
}
},
“winningPlan” : {
“stage” : “FETCH”,
“inputStage” : {
“stage” : “IXSCAN”,
“keyPattern” : {
“x” : 1
},
“indexName” : “x_1”,
“isMultiKey” : false,
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 1,
“direction” : “forward”,
“indexBounds” : {
“x” : [
“[1.0, 1.0]”
]
}
}
},
“rejectedPlans” : [ ]
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 3,
“executionTimeMillis” : 49,
“totalKeysExamined” : 3,
“totalDocsExamined” : 3,
“executionStages” : {
“stage” : “FETCH”,
“nReturned” : 3,
“executionTimeMillisEstimate” : 20,
“works” : 4,
“advanced” : 3,
“needTime” : 0,
“needYield” : 0,
“saveState” : 1,
“restoreState” : 1,
“isEOF” : 1,
“invalidates” : 0,
“docsExamined” : 3,
“alreadyHasObj” : 0,
“inputStage” : {
“stage” : “IXSCAN”,
“nReturned” : 3,
“executionTimeMillisEstimate” : 20,
“works” : 4,
“advanced” : 3,
“needTime” : 0,
“needYield” : 0,
“saveState” : 1,
“restoreState” : 1,
“isEOF” : 1,
“invalidates” : 0,
“keyPattern” : {
“x” : 1
},
“indexName” : “x_1”,
“isMultiKey” : false,
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 1,
“direction” : “forward”,
“indexBounds” : {
“x” : [
“[1.0, 1.0]”
]
},
“keysExamined” : 3,
“dupsTested” : 0,
“dupsDropped” : 0,
“seenInvalidated” : 0
}
}
},
“serverInfo” : {
“host” : “Dmall-PC”,
“port” : 27017,
“version” : “3.2.8”,
“gitVersion” : “ed70e33130c977bda0024c125b56d159573dbaf0”
},
“ok” : 1
}
看到是使用了索引,在数据量小的情况下也用索引,看来是订的规则,
A document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.
我们上面创建的是个升序的索引
MongoDB supports several different index types including text, geospatial, and hashed indexes