MongoDB数据库索引用法的相关知识

一.索引详讲

索引是什么,索引就好比一本书的目录,当我们想找某一章节的时候,通过书籍的目录可以很快的找到,所以适当的加入索引可以提高我们查询的数据的速度。

准备工作,向MongoDB中插入20000条记录,没条记录都有number和name

> for(var i = 0 ; i<200000 ;i++){
… db.books.insert({number:i,name:”book”+i})
… }
WriteResult({ “nInserted” : 1 })
> db.books.find({},{_id:0})
{ “number” : 0, “name” : “book0” }
{ “number” : 1, “name” : “book1” }
{ “number” : 2, “name” : “book2” }
{ “number” : 3, “name” : “book3” }
{ “number” : 4, “name” : “book4” }
{ “number” : 5, “name” : “book5” }
{ “number” : 6, “name” : “book6” }
{ “number” : 7, “name” : “book7” }
……
>

1.对比加入索引和不加入索引的查询效率

例:查询number为65535的name

不使用索引的情况下,查询时间请看millis

> db.books.find({number:65535},{_id:0,name:1}).explain()
{
“cursor” : “BasicCursor”,
“isMultiKey” : false,
“n” : 1,
“nscannedObjects” : 200000,
“nscanned” : 200000,
“nscannedObjectsAllPlans” : 200000,
“nscannedAllPlans” : 200000,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 1562,
“nChunkSkips” : 0,
“millis” : 172,
“server” : “G08FNSTD131598:27017”,
“filterSet” : false
}
>

使用索引的情况下,先创建一个简单索引,用number建立一个索引

db.books.ensureIndex({number:1})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
> db.books.find({number:65535},{_id:0,name:1}).explain()
{
“cursor” : “BtreeCursor number_1”,
“isMultiKey” : false,
“n” : 1,
“nscannedObjects” : 1,
“nscanned” : 1,
“nscannedObjectsAllPlans” : 1,
“nscannedAllPlans” : 1,
“scanAndOrder” : false,
“indexOnly” : false,
“nYields” : 0,
“nChunkSkips” : 0,
“millis” : 0,
“indexBounds” : {
“number” : [
[
65535,
65535
]
]
},
“server” : “G08FNSTD131598:27017”,
“filterSet” : false
}
>

从上面可以看到,查询的时间上带索引的情况要有明显的缩短

2.从插入的数据的时间上进行对比

准备工作,删除刚刚建立的books文档

定义一个函数,来完成记录时间和插入数据的操作

> var time = function(){
… var start = new Date();
… for(var i = 0;i < 200000 ; i++){
… db.books.insert({number:i,name:”book”+i});
… }
… var end = new Date();
… return end – start;
… }

不进行添加索引的时候:

> var x = time();
> x
63057

创建索引

> db.books.ensureIndex({number:1})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}

存在索引的时候的插入数据所用的时间

> var x = time();
> x
67223

可以看到不存在索引的时候,插入的数据所用的时间较短

综上:当我们对一个文档需要进行频繁的插入操作的时候,建立不巧当的索引会导致插入效率的降低。

3.建立索引需要注意的地方

创建索引的时候注意1是正序创建索引-1是倒序创建索引

索引的创建在提高查询性能的同事会影响插入的性能

对于经常查询少插入的文档可以考虑用索引

符合索引要注意索引的先后顺序

每个键全建立索引不一定就能提高性能呢,索引不是万能的

在做排序工作的时候如果是超大数据量也可以考虑加上索引用来提高排序的性能

4.详细介绍索引的创建

①在创建索引的时候,使用了ensureIndex()这个方法,使用它会创建索引,名字就是键的名字加上一个数字,例如number_1或者number_-1,其中1代表是正序索引,-1代表逆序索引

②如果觉得1或-1比较不容易记,还可以使用自定义名字来创建索引

③一个文档建立了多个索引,但是我又想强制使用其中的一个索引,怎么办

例如,我在上面的文档中对number建立了逆序索引,对name建立了正序索引,现在我想查找的时候用name进行索引,我应该这么写:

如果使用了没有创建的索引,那么会返回一个“bad hint”的错误。

④查看所用的索引和查询数据状态信息,可以使用explain()方法

上面看到,我们的索引的名字是bookNameIndex,并且millis是0,nscanned是查到了几个文档

⑤在关系型数据库中尝尝会有约束条件,比较常用的就是唯一性,在MongoDB中也可以指定唯一

建立唯一索引:db.books.ensureIndex({name:-1},{unique:true})

上面我通过有索引和无索引插入了两组完全一样的数据,此时如果去建立唯一的索引,那么就会出错

> db.books.ensureIndex({name:1},{unique:true})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“ok” : 0,
“errmsg” : “E11000 duplicate key error index: mongoDBTest.books.$name_1
dup key: { : \”book0\” }”,
“code” : 11000
}

此时可以通过dropDups:true属性来进行删除重复的数据

> db.books.ensureIndex({name:1},{unique:true,dropDups:true})
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
>

删除重复之后,再去加入一个相同名字的数据,就会出现下面的情况

⑥删除索引

指定要删除的索引

db.runCommand({dropIndexes : ”books” , index:”name_-1”})

删除所有的索引

db.runCommand({dropIndexes : ”books” , index:”*”})

注意:索引的创建时同步的,所以如果想指定异步的去创建索引,就要指定在后台去创建

db.books.ensureIndex({name:-1},{background:true})

二.空间索引

2D索引,举例在一片区域中建立坐标系,那么很多地点可以看做是一个个的坐标,此时2d索引就可以帮助我们进行快速的查询某一个范围的地点了。

例:我在MongoDB中建立一个拥有很多坐标点的文档

> db.map.find({},{_id:0})
{ “gis” : { “x” : 185, “y” : 150 } }
{ “gis” : { “x” : 70, “y” : 180 } }
{ “gis” : { “x” : 75, “y” : 180 } }
{ “gis” : { “x” : 185, “y” : 185 } }
{ “gis” : { “x” : 65, “y” : 185 } }
{ “gis” : { “x” : 50, “y” : 50 } }
{ “gis” : { “x” : 50, “y” : 100 } }
{ “gis” : { “x” : 60, “y” : 55 } }
{ “gis” : { “x” : 65, “y” : 80 } }
{ “gis” : { “x” : 55, “y” : 80 } }
{ “gis” : { “x” : 0, “y” : 0 } }
{ “gis” : { “x” : 0, “y” : 200 } }
{ “gis” : { “x” : 200, “y” : 0 } }
{ “gis” : { “x” : 200, “y” : 200 } }
>

添加一个2D索引

db.map.ensureIndex({"gis":"2d"},{min:-1,max:201})

默认会建立一个[-180,180]之间的2D索引

例子:

①查询点(70,180)最近的3个点

②查询以点(50,50)和点(190,190)为对角线的正方形中的所有的点

> db.map.find({gis:{$within:{$box:[[50,50],[190,190]]}}},{_id:0,gis:1})
{ “gis” : { “x” : 185, “y” : 150 } }
{ “gis” : { “x” : 75, “y” : 180 } }
{ “gis” : { “x” : 70, “y” : 180 } }
{ “gis” : { “x” : 65, “y” : 185 } }
{ “gis” : { “x” : 50, “y” : 100 } }
{ “gis” : { “x” : 65, “y” : 80 } }
{ “gis” : { “x” : 55, “y” : 80 } }
{ “gis” : { “x” : 60, “y” : 55 } }
{ “gis” : { “x” : 50, “y” : 50 } }
{ “gis” : { “x” : 185, “y” : 185 } }
>

③查询出以圆心为(56,80)半径为50规则下的圆心面积中的点

> db.map.find({gis:{$within:{$center:[[56,80],50]}}},{_id:0,gis:1})
{ “gis” : { “x” : 55, “y” : 80 } }
{ “gis” : { “x” : 50, “y” : 100 } }
{ “gis” : { “x” : 50, “y” : 50 } }
{ “gis” : { “x” : 60, “y” : 55 } }
{ “gis” : { “x” : 65, “y” : 80 } }

到此这篇关于MongoDB数据库索引用法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。


数据运维技术 » MongoDB数据库索引用法的相关知识