首页 > 代码库 > Mongodb查看执行计划和强制索引策略
Mongodb查看执行计划和强制索引策略
查看执行计划的方法: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
- }
Mongodb查看执行计划和强制索引策略
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。