mongodb创建索引
2017-05-26 17:08
148 查看
1. mongodb中如果查找在1000000数据中,查找某一个数据,如果查找userId为1的我们来看看耗时:
好了,时间差别很大,因为mongodb从开始一个个查找,所以消耗时间过长
2.创建索引再看看db.visitLog.ensureIndex({"userId":1}) //创建索引
db.visitLog.find({"userId" : 9}).explain()
"executionStats" : { "executionSuccess" : true, "nReturned" : 2, "executionTimeMillis" : 406, "totalKeysExamined" : 0, "totalDocsExamined" : 1000008, "executionStages" : { "stage" : "COLLSCAN", "filter" : { "userId" : { "$eq" : 9 } }, "nReturned" : 2, "executionTimeMillisEstimate" : 354, "works" : 1000010, "advanced" : 2, "needTime" : 1000007, "needYield" : 0, "saveState" : 7817, "restoreState" : 7817, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 1000008 }执行时间406毫秒,注意这里userId为9,也就是数据开始的第九个
"executionStats" : { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 54700, "totalKeysExamined" : 0, "totalDocsExamined" : 1000008, "executionStages" : { "stage" : "COLLSCAN", "filter" : { "userId" : { "$eq" : 999999 } }, "nReturned" : 1, "executionTimeMillisEstimate" : 375, "works" : 1000010, "advanced" : 1, "needTime" : 1000008, "needYield" : 0, "saveState" : 7819, "restoreState" : 7819, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 1000008 }, "allPlansExecution" : [] }
好了,时间差别很大,因为mongodb从开始一个个查找,所以消耗时间过长
2.创建索引再看看db.visitLog.ensureIndex({"userId":1}) //创建索引
"executionStats" : { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 0, "totalKeysExamined" : 1, "totalDocsExamined" : 1, },查询时间明显的提高了很多。
相关文章推荐
- 关于MongoDB索引管理-索引的创建、查看、删除操作详解
- mongodb-创建索引
- MongoDB中索引的创建和使用详解
- MongoDB创建过期索引的三种方法
- mongodb 索引创建、查询分析
- MongoDB创建索引(不锁库方法)
- MongoDB 创建索引导致锁库的解决方案
- mongodb创建2D索引
- Mongodb 创建索引
- pymongo给mongodb创建索引的简单实现方法
- mongodb创建索引
- mongodb 索引的创建
- 使用mongodb作为数据源搭建solr搜索引擎之创建同步索引实现
- mongodb 创建局部索引【Partial Indexes】
- java mongodb 基础系列---查询,排序,limit,$in,$or,输出为list,创建索引,$ne 非操作
- MongoDB性能篇 -创建索引,组合索引,唯一索引,删除索引和explain执行计划
- MongoDB 创建地图索引及根据位置坐标查询地点小例子
- pymongo给mongodb创建索引的简单实现方法
- MongoDB中创建索引需要注意的事项
- mongodb创建索引