MongoDB:索引操作

时间:2023-11-30 15:55:38

首先插入十万个数据

 for(var i=0;i<100000;i++){
var rand = parseInt(i*Math.random());
db.person_test.insert({"name":"hxc"+i,"age":i})
}

使用性能分析函数(explain)分析查询速度。使用方法详见:http://blog.****.net/leshami/article/details/53521990

db.person.find({"name":"hxc"+99999}).explain("executionStats")

执行详细结果

{
"queryPlanner" : {
"plannerVersion" : ,
"namespace" : "thirdparty.person_test",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "hxc99999"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "hxc99999"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"executionStats" : {   //执行计划相关统计信息
"executionSuccess" : true,    //执行成功的状态
"nReturned" : ,    //返回结果集数目
"executionTimeMillis" : ,       //执行所需的时间,毫秒
"totalKeysExamined" : ,       //索引检查的时间
"totalDocsExamined" : ,      //检查文档总数
"executionStages" : {
"stage" : "COLLSCAN",        //使用集合扫描方式
"filter" : { //过滤条件
"name" : {
"$eq" : "hxc99999"
}
},
"nReturned" : ,       //返回结果集数目
"executionTimeMillisEstimate" : , //预估的执行时间,毫秒
"works" : ,           //工作单元数,一个查询会被派生为一些小的工作单元
"advanced" : ,          //优先返回的结果数目
"needTime" : ,
"needFetch" : ,
"saveState" : ,
"restoreState" : ,
"isEOF" : ,
"invalidates" : ,
"direction" : "forward",       //方向
"docsExamined" :        //文档检查数目
}
},
"serverInfo" : {
"host" : "x-integration1",
"port" : ,
"version" : "3.0.6",
"gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
},
"ok" :
}

可以看到执行查询所用的时间是71ms,一共检查了100000个文档。

这时的查询速度不是很理想,那么如何优化查询速度呢?就要使用索引查询了。

建立索引查询

db.person_test.ensureIndex({"name":});
db.person_test.find({"name":"hxc"+}).explain("executionStats");

执行结果

{
"queryPlanner" : {
"plannerVersion" : ,
"namespace" : "thirdparty.person_test",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "hxc99999"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"name" :
},
"indexName" : "name_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"name" : [
"[\"hxc99999\", \"hxc99999\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {               //执行计划相关统计信息
"executionSuccess" : true,         //执行成功的状态
"nReturned" : ,               //返回结果集数目
"executionTimeMillis" : ,         //执行所需的时间,毫秒
"totalKeysExamined" : ,          //索引检查的时间
"totalDocsExamined" : ,          //检查文档总数
"executionStages" : {
"stage" : "FETCH",           //使用游标扫描方式
"nReturned" : ,            //过滤条件
"executionTimeMillisEstimate" : ,
"works" : ,
"advanced" : ,
"needTime" : ,
"needFetch" : ,
"saveState" : ,
"restoreState" : ,
"isEOF" : ,
"invalidates" : ,
"docsExamined" : ,
"alreadyHasObj" : ,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : ,          //返回结果集数目
"executionTimeMillisEstimate" : ,//预估的执行时间,毫秒
"works" : ,             //工作单元数,一个查询会被派生为一些小的工作单元
"advanced" : ,           //优先返回的结果数目
"needTime" : ,
"needFetch" : ,
"saveState" : ,
"restoreState" : ,
"isEOF" : ,
"invalidates" : ,
"keyPattern" : {
"name" :
},
"indexName" : "name_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"name" : [
"[\"hxc99999\", \"hxc99999\"]"
]
},
"keysExamined" : ,
"dupsTested" : ,
"dupsDropped" : ,
"seenInvalidated" : ,
"matchTested" :
}
}
},
"serverInfo" : {
"host" : "x-integration1",
"port" : ,
"version" : "3.0.6",
"gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
},
"ok" :
}

可以看到查询时间小于1ms!只需查询一个文档就可以了!这就大大优化了查询的速度。