首先插入十万个数据
1 for(var i=0;i<100000;i++){ 2 var rand = parseInt(i*Math.random()); 3 db.person_test.insert({"name":"hxc"+i,"age":i}) 4 }
使用性能分析函数(explain)分析查询速度。使用方法详见:http://blog.csdn.net/leshami/article/details/53521990
db.person.find({"name":"hxc"+99999}).explain("executionStats")
执行详细结果
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "thirdparty.person_test", "indexFilterSet" : false, "parsedQuery" : { "name" : { "$eq" : "hxc99999" } }, "winningPlan" : { "stage" : "COLLSCAN", "filter" : { "name" : { "$eq" : "hxc99999" } }, "direction" : "forward" }, "rejectedPlans" : [ ] }, "executionStats" : { //执行计划相关统计信息 "executionSuccess" : true, //执行成功的状态 "nReturned" : 1, //返回结果集数目 "executionTimeMillis" : 71, //执行所需的时间,毫秒 "totalKeysExamined" : 0, //索引检查的时间 "totalDocsExamined" : 100000, //检查文档总数 "executionStages" : { "stage" : "COLLSCAN", //使用集合扫描方式 "filter" : { //过滤条件 "name" : { "$eq" : "hxc99999" } }, "nReturned" : 1, //返回结果集数目 "executionTimeMillisEstimate" : 60, //预估的执行时间,毫秒 "works" : 100002, //工作单元数,一个查询会被派生为一些小的工作单元 "advanced" : 1, //优先返回的结果数目 "needTime" : 100000, "needFetch" : 0, "saveState" : 781, "restoreState" : 781, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", //方向 "docsExamined" : 100000 //文档检查数目 } }, "serverInfo" : { "host" : "x-integration1", "port" : 27014, "version" : "3.0.6", "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2" }, "ok" : 1 }
可以看到执行查询所用的时间是71ms,一共检查了100000个文档。
这时的查询速度不是很理想,那么如何优化查询速度呢?就要使用索引查询了。
建立索引查询
db.person_test.ensureIndex({"name":1}); db.person_test.find({"name":"hxc"+99999}).explain("executionStats");
执行结果
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "thirdparty.person_test", "indexFilterSet" : false, "parsedQuery" : { "name" : { "$eq" : "hxc99999" } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "name" : 1 }, "indexName" : "name_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "name" : [ "[\"hxc99999\", \"hxc99999\"]" ] } } }, "rejectedPlans" : [ ] }, "executionStats" : { //执行计划相关统计信息 "executionSuccess" : true, //执行成功的状态 "nReturned" : 1, //返回结果集数目 "executionTimeMillis" : 0, //执行所需的时间,毫秒 "totalKeysExamined" : 1, //索引检查的时间 "totalDocsExamined" : 1, //检查文档总数 "executionStages" : { "stage" : "FETCH", //使用游标扫描方式 "nReturned" : 1, //过滤条件 "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 1, "needTime" : 0, "needFetch" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "docsExamined" : 1, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 1, //返回结果集数目 "executionTimeMillisEstimate" : 0,//预估的执行时间,毫秒 "works" : 1, //工作单元数,一个查询会被派生为一些小的工作单元 "advanced" : 1, //优先返回的结果数目 "needTime" : 0, "needFetch" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "keyPattern" : { "name" : 1 }, "indexName" : "name_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "name" : [ "[\"hxc99999\", \"hxc99999\"]" ] }, "keysExamined" : 1, "dupsTested" : 0, "dupsDropped" : 0, "seenInvalidated" : 0, "matchTested" : 0 } } }, "serverInfo" : { "host" : "x-integration1", "port" : 27014, "version" : "3.0.6", "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2" }, "ok" : 1 }
可以看到查询时间小于1ms!只需查询一个文档就可以了!这就大大优化了查询的速度。