mongoDB 索引(四)
来源:互联网 发布:黄一琳为什么开淘宝 编辑:程序博客网 时间:2024/05/29 14:29
索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录。
这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可以要花费几十秒甚至几分钟,这对网站的性能是非常致命的。
索引是特殊的数据结构,索引存储在一个易于遍历读取的数据集合中,索引是对数据库表中一列或多列的值进行排序的一种结构
1.ensureIndex() 方法
MongoDB使用 ensureIndex() 方法来创建索引。
语法
ensureIndex()方法基本语法格式如下所示:
>db.COLLECTION_NAME.ensureIndex({KEY:1})
语法中 Key 值为你要创建的索引字段,1为指定按升序创建索引,如果你想按降序来创建索引指定为-1即可。
实例
数据准备index.js,里面有
for(var i = 0 ; i<200000 ;i++){ db.books.insert({number:i,name:i+"book"})}
1.先检验一下查询性能使用load加载js文件> load("D:/install/MongoDB/test/index.js")true> var start = new Date()> db.books.find({number:65871}){ "_id" : ObjectId("5a3a1b3290cf1481126d353e"), "number" : 65871, "name" : "65871book" }> var end = new Date()> end - start74结果74
db.books.ensureIndex({number:1})3.再执行第一部的代码可以看出有数量级的性能提升
> var start = new Date()> db.books.find({number:65871}){ "_id" : ObjectId("5a3a1b3290cf1481126d353e"), "number" : 65871, "name" : "65871book" }> var end = new Date()> end - start6
ensureIndex() 方法中你也可以设置使用多个字段创建索引(关系型数据库中称作复合索引)
db.col.ensureIndex({"title":1,"description":-1})
ensureIndex() 接收可选参数,可选参数列表如下:
实例
1.1在后台创建索引:
db.values.ensureIndex({open: 1, close: 1}, {background: true})
通过在创建索引时加background:true 的选项,让创建工作在后台执行
1.2创建索引同时指定索引的名字
db.books.ensureIndex({name:-1},{name:”bookname”})
1.3 建立唯一索引,解决文档books不能插入重复的数值
db.books.ensureIndex({name:-1},{unique:true})
试验
> db.books.ensureIndex({name:1},{unique:true}){ "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1}> db.books.insert({name:"1book"})WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: admin.books index: name_1 dup key: { : \"1book\" }" }})
1.4.踢出重复值
建立唯一索引之前已经有重复数值如何处理
db.books.ensureIndex({name:-1},{unique:true,dropDups:true})
1.5.强制查询使用指定的索引Hint
db.books.find({name:"1book",number:1}).hint({name:-1})
1.6 查看本次查询使用那个索引和查询数据的状态信息 Expain
> db.books.find({name:"1book"}).explain(){ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "admin.books", "indexFilterSet" : false, "parsedQuery" : { "name" : { "$eq" : "1book" } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "name" : 1 }, "indexName" : "name_1", "isMultiKey" : false, "multiKeyPaths" : { "name" : [ ] }, "isUnique" : true, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "name" : [ "[\"1book\", \"1book\"]" ] } } }, "rejectedPlans" : [ ] }, "serverInfo" : { "host" : "DESKTOP-VKU59R5", "port" : 27017, "version" : "3.4.10", "gitVersion" : "078f28920cb24de0dd479b5ea6c66c644f6326e9" }, "ok" : 1}“cursor” : “BtreeCursor name_-1“ 使用索引“nscanned” : 1 查到几个文档“millis” : 0 查询时间0是很不错的性能
1.7 查看索引
mongoDB提供了查看索引信息的方法:
getIndexes()方法可以用来查看集合的所有索引,
> db.books.getIndexes()[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "admin.books" }, { "v" : 2, "key" : { "number" : 1 }, "name" : "number_1", "ns" : "admin.books" }, { "v" : 2, "unique" : true, "key" : { "name" : 1 }, "name" : "name_1", "ns" : "admin.books" }]
totalIndexSize()查看集合索引的总大小,
> db.books.totalIndexSize()4321280db.system.indexes.find()查看数据库中所有索引信息。
1.8. 删除索引
不在需要的索引,我们可以将其删除。删除索引时,可以删除集合中的某一索引,可以删除全部索引。
删除指定的索引dropIndex()
db.COLLECTION_NAME.dropIndex("INDEX-NAME")
如,删除集合sites
中名为"name_1_domain_-1"的索引:
> db.sites.dropIndex("name_1_domain_-1"){ "nIndexesWas" : 2, "ok" : 1 }
删除所有索引dropIndexes()
db.COLLECTION_NAME.dropIndexes()
如,删除集合sites
中所有的索引:
> db.sites.dropIndexes(){ "nIndexesWas" : 1, "msg" : "non-_id indexes dropped for collection", "ok" : 1}
2.空间索引
mongoDB提供强大的空间索引可以查询出一定范围的地理坐标.看例子, 准备数据map.json
> load("D:/install/MongoDB/test/map.json")true
1.查询出距离点(70,180)最近的3个点
添加2D索引
> db.map.ensureIndex({"gis":"2d"},{min:-1,max:204}){ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1}
默认会建立一个[-180,180]之间的2D索引
查询点(70,180)最近的3个点
> db.map.find({"gis":{$near:[70,180]}},{gis:1,_id:0}).limit(3){ "gis" : { "x" : 70, "y" : 180 } }{ "gis" : { "x" : 75, "y" : 180 } }{ "gis" : { "x" : 65, "y" : 185 } }
2.查询以点(50,50)和点(190,190)为对角线的正方形中的所有的点
> db.map.find({gis:{"$within":{$box:[[50,50],[190,190]]}}},{_id:0,gis:1}){ "gis" : { "x" : 50, "y" : 50 } }{ "gis" : { "x" : 50, "y" : 50 } }{ "gis" : { "x" : 60, "y" : 55 } }{ "gis" : { "x" : 55, "y" : 80 } }{ "gis" : { "x" : 65, "y" : 80 } }{ "gis" : { "x" : 65, "y" : 185 } }{ "gis" : { "x" : 70, "y" : 180 } }{ "gis" : { "x" : 75, "y" : 180 } }{ "gis" : { "x" : 185, "y" : 150 } }{ "gis" : { "x" : 185, "y" : 185 } }
3.查询出以圆心为(56,80)半径为50规则下的圆心面积中的点
> db.map.find({gis:{$within:{$center:[[56,80],50]}}},{_id:0,gis:1}){ "gis" : { "x" : 50, "y" : 50 } }{ "gis" : { "x" : 50, "y" : 50 } }{ "gis" : { "x" : 60, "y" : 55 } }{ "gis" : { "x" : 55, "y" : 80 } }{ "gis" : { "x" : 65, "y" : 80 } }
3.索引使用需要注意的地方
1.创建索引的时候注意1是正序创建索引-1是倒序创建索引
2.索引的创建在提高查询性能的同事会影响插入的性能, 对于经常查询少插入的文档可以考虑用索引
3.符合索引要注意索引的先后顺序
4.每个键全建立索引不一定就能提高性能呢, 索引不是万能的
5.在做排序工作的时候如果是超大数据量也可以考虑加上索引, 用来提高排序的性能
参考:http://www.mongodb.org.cn/tutorial/18.html
- mongoDB 索引(四)
- MongoDB的索引(四)
- MongoDB 学习笔记(四):索引
- 四、MongoDB 索引
- MongoDB (四):索引
- MongoDB笔记四之索引
- MongoDB入门学习(四):MongoDB的索引
- mongodb指南(二十) - developer zone - 索引(四)地理信息索引
- mongodb指南(二十) - developer zone - 索引(四)地理信息索引
- mongoDB---索引(转)
- MongoDB七(索引)
- MongoDB - 索引 (二)使用
- MongoDB学习(4)索引
- MongoDB的索引(三)
- MongoDB学习之四----文本索引的简单使用
- 四天学会mongoDB (第四天 索引操作)
- (四)索引
- 学习MongoDB 四: MongoDB查询(一)
- java字符串(二)-StringBuilder和StringBuffer
- 生成随机数字字母组合参数
- Unity计时器(一)
- 桌面小部件的简单开发步骤
- Scala方法和函数的区别
- mongoDB 索引(四)
- 12.04 Day10
- C语言操作符
- ucosii操作系统下,exit中断跑飞
- Retrofit2简单使用
- 数据结构实验之排序二:交换排序
- Java中AyyarList类的基本实现
- Apache Core实现HTTPServer
- Python171220-引入Python标准库和创建库