查看执行计划的方法:explain ()
winningPlan是最终的执行计划 其中的indexBounds索引计划
- 在age上建立索引
- > db.person.ensureIndex({age:1});
- {
- "createdCollectionAutomatically" : false,
- "numIndexesBefore" : 1,
- "numIndexesAfter" : 2,
- "ok" : 1
- }
- > db.person.getIndexes();
- [
- {
- "v" : 1,
- "key" : {
- "_id" : 1
- },
- "name" : "_id_",
- "ns" : "mydb.person"
- },
- {
- "v" : 1,
- "key" : {
- "age" : 1
- },
- "name" : "age_1",
- "ns" : "mydb.person"
- }
- ]
- 查看执行计划 indexBounds可以看到走了age的索引
- > db.person.find({age:30}).explain();
- {
- "queryPlanner" : {
- "plannerVersion" : 1,
- "namespace" : "mydb.person",
- "indexFilterSet" : false,
- "parsedQuery" : {
- "age" : {
- "$eq" : 30
- }
- },
- "winningPlan" : {
- "stage" : "FETCH",
- "inputStage" : {
- "stage" : "IXSCAN",
- "keyPattern" : {
- "age" : 1
- },
- "indexName" : "age_1",
- "isMultiKey" : false,
- "direction" : "forward",
- "indexBounds" : {
- "age" : [
- "[30.0, 30.0]"
- ]
- }
- }
- },
- "rejectedPlans" : [ ]
- },
- "serverInfo" : {
- "host" : "localhost.localdomain",
- "port" : 27017,
- "version" : "3.0.6",
- "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
- },
- "ok" : 1
- }
- 没有走索引的计划是这样的
- > db.person.find({name:‘zjf‘}).explain();
- {
- "queryPlanner" : {
- "plannerVersion" : 1,
- "namespace" : "mydb.person",
- "indexFilterSet" : false,
- "parsedQuery" : {
- "name" : {
- "$eq" : "zjf"
- }
- },
- "winningPlan" : {
- "stage" : "COLLSCAN",
- "filter" : {
- "name" : {
- "$eq" : "zjf"
- }
- },
- "direction" : "forward"
- },
- "rejectedPlans" : [ ]
- },
- "serverInfo" : {
- "host" : "localhost.localdomain",
- "port" : 27017,
- "version" : "3.0.6",
- "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
- },
- "ok" : 1
- }
强制索引 hint()方法:
- > db.person.ensureIndex({name:1,age:1});
- {
- "createdCollectionAutomatically" : false,
- "numIndexesBefore" : 3,
- "numIndexesAfter" : 4,
- "ok" : 1
- }
- > db.person.getIndexes();
- [
- {
- "v" : 1,
- "key" : {
- "_id" : 1
- },
- "name" : "_id_",
- "ns" : "mydb.person"
- },
- {
- "v" : 1,
- "key" : {
- "age" : 1
- },
- "name" : "age_1",
- "ns" : "mydb.person"
- },
- {
- "v" : 1,
- "key" : {
- "name" : 1
- },
- "name" : "name_1",
- "ns" : "mydb.person"
- },
- {
- "v" : 1,
- "key" : {
- "name" : 1,
- "age" : 1
- },
- "name" : "name_1_age_1",
- "ns" : "mydb.person"
- }
- ]
- //如果不加hint 默认走age上的索引。
- > db.person.find({age:{$gt:0}}).explain();
- {
- "queryPlanner" : {
- "plannerVersion" : 1,
- "namespace" : "mydb.person",
- "indexFilterSet" : false,
- "parsedQuery" : {
- "age" : {
- "$gt" : 0
- }
- },
- "winningPlan" : {
- "stage" : "FETCH",
- "inputStage" : {
- "stage" : "IXSCAN",
- "keyPattern" : {
- "age" : 1
- },
- "indexName" : "age_1",
- "isMultiKey" : false,
- "direction" : "forward",
- "indexBounds" : {
- "age" : [
- "(0.0, inf.0]"
- ]
- }
- }
- },
- "rejectedPlans" : [ ]
- },
- "serverInfo" : {
- "host" : "localhost.localdomain",
- "port" : 27017,
- "version" : "3.0.6",
- "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
- },
- "ok" : 1
- }
- //使用hint
- > db.person.find({age:{$gt:0}}).hint({name:1,age:1}).explain();
- {
- "queryPlanner" : {
- "plannerVersion" : 1,
- "namespace" : "mydb.person",
- "indexFilterSet" : false,
- "parsedQuery" : {
- "age" : {
- "$gt" : 0
- }
- },
- "winningPlan" : {
- "stage" : "KEEP_MUTATIONS",
- "inputStage" : {
- "stage" : "FETCH",
- "filter" : {
- "age" : {
- "$gt" : 0
- }
- },
- "inputStage" : {
- "stage" : "IXSCAN",
- "keyPattern" : {
- "name" : 1,
- "age" : 1
- },
- "indexName" : "name_1_age_1",
- "isMultiKey" : false,
- "direction" : "forward",
- "indexBounds" : {
- "name" : [
- "[MinKey, MaxKey]"
- ],
- "age" : [
- "[MinKey, MaxKey]"
- ]
- }
- }
- }
- },
- "rejectedPlans" : [ ]
- },
- "serverInfo" : {
- "host" : "localhost.localdomain",
- "port" : 27017,
- "version" : "3.0.6",
- "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
- },
- "ok" : 1
- }
时间: 2024-10-11 16:11:11