mongodb 索引
创建索引
$collection --> 表名 $field --> 字段名
$value --> 值
# 1是正排, -1 是倒排 db.$collection.createIndex({$field:1}); # 建立复合索引 db.$collection.createIndex({$field1:1, $field2:1});
查看索引
db.$collection.getIndexes();
删除索引
db.$collecion.dropIndex({$field:1});
# 删除全部索引
db.$collection.dropIndexes();
查看查询语句性能
db.$collecion.find({$field:$value}).explain(true)
1 /* 1 */ 2 { 3 "queryPlanner" : { 4 "plannerVersion" : 1, 5 "namespace" : "hdb_broker.hdb_dr_recommend", //该值返回的是该query所查询的表 6 "indexFilterSet" : false, //该query是否有indexfilter 7 "parsedQuery" : { 8 "$and" : [ 9 { 10 "cntInfo.cntName" : { 11 "$eq" : "张三0" 12 } 13 }, 14 { 15 "cntInfo.cntPhone" : { 16 "$eq" : "13784647210" 17 } 18 } 19 ] 20 }, 21 "queryHash" : "4159AA83", 22 "planCacheKey" : "BDF6F710", 23 "winningPlan" : { //查询优化器针对该query所返回的最优执行计划的详细内容 24 "stage" : "FETCH", //最优执行计划的stage,这里返回是FETCH,可以理解为通过返回的index位置去检索具体的文档 25 "filter" : { 26 "cntInfo.cntPhone" : { 27 "$eq" : "13784647210" 28 } 29 }, 30 "inputStage" : { 31 "stage" : "IXSCAN", 32 "keyPattern" : { 33 "cntInfo.cntName" : 1.0 34 }, 35 "indexName" : "cntInfo.cntName_1", 36 "isMultiKey" : false, 37 "multiKeyPaths" : { 38 "cntInfo.cntName" : [] 39 }, 40 "isUnique" : false, 41 "isSparse" : false, 42 "isPartial" : false, 43 "indexVersion" : 2, 44 "direction" : "forward", 45 "indexBounds" : { 46 "cntInfo.cntName" : [ 47 "[\"张三0\", \"张三0\"]" 48 ] 49 } 50 } 51 }, 52 "rejectedPlans" : [ 53 { 54 "stage" : "FETCH", 55 "filter" : { 56 "cntInfo.cntName" : { 57 "$eq" : "张三0" 58 } 59 }, 60 "inputStage" : { 61 "stage" : "IXSCAN", 62 "keyPattern" : { 63 "cntInfo.cntPhone" : 1.0 64 }, 65 "indexName" : "cntInfo.cntPhone_1", 66 "isMultiKey" : false, 67 "multiKeyPaths" : { 68 "cntInfo.cntPhone" : [] 69 }, 70 "isUnique" : false, 71 "isSparse" : false, 72 "isPartial" : false, 73 "indexVersion" : 2, 74 "direction" : "forward", 75 "indexBounds" : { 76 "cntInfo.cntPhone" : [ 77 "[\"13784647210\", \"13784647210\"]" 78 ] 79 } 80 } 81 }, 82 { 83 "stage" : "FETCH", 84 "filter" : { 85 "$and" : [ 86 { 87 "cntInfo.cntName" : { 88 "$eq" : "张三0" 89 } 90 }, 91 { 92 "cntInfo.cntPhone" : { 93 "$eq" : "13784647210" 94 } 95 } 96 ] 97 }, 98 "inputStage" : { 99 "stage" : "AND_SORTED", 100 "inputStages" : [ 101 { 102 "stage" : "IXSCAN", 103 "keyPattern" : { 104 "cntInfo.cntName" : 1.0 105 }, 106 "indexName" : "cntInfo.cntName_1", 107 "isMultiKey" : false, 108 "multiKeyPaths" : { 109 "cntInfo.cntName" : [] 110 }, 111 "isUnique" : false, 112 "isSparse" : false, 113 "isPartial" : false, 114 "indexVersion" : 2, 115 "direction" : "forward", 116 "indexBounds" : { 117 "cntInfo.cntName" : [ 118 "[\"张三0\", \"张三0\"]" 119 ] 120 } 121 }, 122 { 123 "stage" : "IXSCAN", 124 "keyPattern" : { 125 "cntInfo.cntPhone" : 1.0 126 }, 127 "indexName" : "cntInfo.cntPhone_1", 128 "isMultiKey" : false, 129 "multiKeyPaths" : { 130 "cntInfo.cntPhone" : [] 131 }, 132 "isUnique" : false, 133 "isSparse" : false, 134 "isPartial" : false, 135 "indexVersion" : 2, 136 "direction" : "forward", 137 "indexBounds" : { 138 "cntInfo.cntPhone" : [ 139 "[\"13784647210\", \"13784647210\"]" 140 ] 141 } 142 } 143 ] 144 } 145 } 146 ] 147 }, 148 "serverInfo" : { 149 "host" : "hdb-dev-db-02", 150 "port" : 27017, 151 "version" : "4.2.5", 152 "gitVersion" : "2261279b51ea13df08ae708ff278f0679c59dc32" 153 }, 154 "ok" : 1.0 155 }

浙公网安备 33010602011771号