MongoDB索引

数据库中的索引就是用来提升查询操做的性能,可是会影响插入、更新和删除的效率,由于数据库不只要执行这些操做,还要负责索引的更新。shell

经过创建索引,影响一部分插入、更新和删除的效率,可是能大大挺高查询的效率,这个仍是很值得的。数据库

为了开始后面的操做,首先经过MongoDB shell插入一些测试数据。dom

 1 for(var i=0;i<10;i++){
 2   var randAge = parseInt(5*Math.random()) + 20;
 3   var gender = (randAge%2)?"Male":"Female";
 4   db.school.students.insert({"name":"Will"+i, "gender": gender, "age": randAge});
 5 }
 6 
 7  
 8 /*    个人数据,如下测试都是基于这个测试,因为数据是随机生成,因此测试每次都会不一样
 9 { "name" : "Will0", "gender" : "Female", "age" : 22 },
10 { "name" : "Will1", "gender" : "Female", "age" : 20 },
11 { "name" : "Will2", "gender" : "Male", "age" : 24 },
12 { "name" : "Will3", "gender" : "Male", "age" : 23 },
13 { "name" : "Will4", "gender" : "Male", "age" : 21 },
14 { "name" : "Will5", "gender" : "Male", "age" : 20 },
15 { "name" : "Will6", "gender" : "Female", "age" : 20 },
16 { "name" : "Will7", "gender" : "Female", "age" : 24 },
17 { "name" : "Will8", "gender" : "Male", "age" : 21 },
18 { "name" : "Will9", "gender" : "Female", "age" : 24 },
19 */

 

索引的操做

建立索引:在MongoDB shell中,能够经过ensureIndex()来建立因此,第一个参数是指定要建立因此的键。性能

经过unique参数能够建立惟一索引。测试

1 > db.school.students.ensureIndex({"name": 1}, {"unique": true})
2 >

 查看索引:优化

 1 > db.school.students.getIndexes()
 2 [
 3         {
 4                 "v" : 1,
 5                 "key" : {
 6                         "_id" : 1
 7                 },
 8                 "ns" : "test.school.students",
 9                 "name" : "_id_"
10         },
11         {
12                 "v" : 1,
13                 "key" : {
14                         "name" : 1
15                 },
16                 "unique" : true,
17                 "ns" : "test.school.students",
18                 "name" : "name_1"
19         }
20 ]
21 >

 

删除索引:spa

1 > db.school.students.dropIndex("name_1")
2 { "nIndexesWas" : 2, "ok" : 1 }
3 >

 

索引名称:默认状况下,索引的名称是"键_值_键_值…"的形式,当键的数量不少的时候,索引的名字就会很长。code

因此,在建立索引的时候,能够经过"name"参数自定义索引的名字。server

1 > db.school.students.ensureIndex({"name": 1}, {"name": "myIndex"})
2 >

 

 

explain()和hint()

经过explain()能够获得不少跟find相关的信息,对索引的分析颇有帮助。blog

当有多个可使用的索引时,MongoDB会自动选择最优索引,可是咱们能够经过hint()操做选择咱们想要使用的索引。

下面来看看没有索引时explain()的输出:

 1 > db.school.students.find({"name": "Will5"}).explain()
 2 {
 3         "cursor" : "BasicCursor",
 4         "isMultiKey" : false,
 5         "n" : 1,
 6         "nscannedObjects" : 6,
 7         "nscanned" : 6,
 8         "nscannedObjectsAllPlans" : 6,
 9         "nscannedAllPlans" : 6,
10         "scanAndOrder" : false,
11         "indexOnly" : false,
12         "nYields" : 0,
13         "nChunkSkips" : 0,
14         "millis" : 0,
15         "indexBounds" : {
16 
17         },
18         "server" : "××××:27017"
19 }
20 >

 

分析:下面选择了几个咱们比较关心的字段

  • cursor:BasicCursor表示是full Collection scan,即没有索引的全表扫描
  • n:知足查询条件的文档数量
  • nscannedObjects:总共扫描的文档的数量
  • nscanned:总共扫描的索引节点的数量
  • scanAndOrder:false表示,MongoDB现有索引下文档的顺序来返回排序结果;true表示,MongoDB须要在获得查询结果后从新排序
  • millis:完成查询须要的毫秒数

添加索引,再次检查explain()的输出:

 1 > db.school.students.ensureIndex({"name": 1}, {"unique": true})
 2 > db.school.students.find({"name": "Will5"}).explain()
 3 {
 4         "cursor" : "BtreeCursor name_1",
 5         "isMultiKey" : false,
 6         "n" : 1,
 7         "nscannedObjects" : 1,
 8         "nscanned" : 1,
 9         "nscannedObjectsAllPlans" : 1,
10         "nscannedAllPlans" : 1,
11         "scanAndOrder" : false,
12         "indexOnly" : false,
13         "nYields" : 0,
14         "nChunkSkips" : 0,
15         "millis" : 0,
16         "indexBounds" : {
17                 "name" : [
18                         [
19                                 "Will5",
20                                 "Will5"
21                         ]
22                 ]
23         },
24         "server" : "××××:27017"
25 }
26 >

 

 

组合索引

单键索引仍是比较简单的,当使用组合索引的时候,就要多考虑一些了。本身也不肯定可否总结的很好,若是错误,但愿你们指出、讨论。

索引创建可能有多种方式,咱们的目标就是减小"nscanned"(固然也有特例,请参照"索引和排序")。

下面分析基于前面生成的数据来分析一下组合索引,假设咱们要查询年龄大于等于23的女学生。

  • 使用"age_1"索引的输出以下
     1 > db.school.students.find({"age":{"$gte":23}, "gender":"Female"}).hint("age_1").explain()
     2 {
     3         "cursor" : "BtreeCursor age_1",
     4         "isMultiKey" : false,
     5         "n" : 2,
     6         "nscannedObjects" : 4,
     7         "nscanned" : 4,
     8         "nscannedObjectsAllPlans" : 4,
     9         "nscannedAllPlans" : 4,
    10         "scanAndOrder" : false,
    11         "indexOnly" : false,
    12         "nYields" : 0,
    13         "nChunkSkips" : 0,
    14         "millis" : 0,
    15         "indexBounds" : {
    16                 "age" : [
    17                         [
    18                                 23,
    19                                 1.7976931348623157e+308
    20                         ]
    21                 ]
    22         },
    23         "server" : "××××:27017"
    24 }
    25 >

     

    索引的分析:

Index

Documents

Result

age:20

{ "name" : "Will1", "gender" : "Female", "age" : 20 }

"n" : 2

age:20

{ "name" : "Will5", "gender" : "Male", "age" : 20 }

"nscannedObjects" : 4

age:20

{ "name" : "Will6", "gender" : "Female", "age" : 20 }

"nscanned" : 4

age:21

{ "name" : "Will4", "gender" : "Male", "age" : 21 }

 

age:21

{ "name" : "Will8", "gender" : "Male", "age" : 21 }

 

age:22

{ "name" : "Will0", "gender" : "Female", "age" : 22 }

 

age:23

{ "name" : "Will3", "gender" : "Male", "age" : 23 }

 

age:24

{ "name" : "Will2", "gender" : "Male", "age" : 24 }

 

age:24

{ "name" : "Will7", "gender" : "Female", "age" : 24 }

 

age:24

{ "name" : "Will9", "gender" : "Female", "age" : 24 }

 

 

 

  • 使用"age_1_gender_1"索引的输出以下
     1 > db.school.students.find({"age":{"$gte":23}, "gender":"Female"}).hint("age_1_gender_1").explain()
     2 {
     3         "cursor" : "BtreeCursor age_1_gender_1",
     4         "isMultiKey" : false,
     5         "n" : 2,
     6         "nscannedObjects" : 2,
     7         "nscanned" : 4,
     8         "nscannedObjectsAllPlans" : 2,
     9         "nscannedAllPlans" : 4,
    10         "scanAndOrder" : false,
    11         "indexOnly" : false,
    12         "nYields" : 0,
    13         "nChunkSkips" : 0,
    14         "millis" : 0,
    15         "indexBounds" : {
    16                 "age" : [
    17                         [
    18                                 23,
    19                                 1.7976931348623157e+308
    20                         ]
    21                 ],
    22                 "gender" : [
    23                         [
    24                                 "Female",
    25                                 "Female"
    26                         ]
    27                 ]
    28         },
    29         "server" : "××××:27017"
    30 }
    31 >

     

    索引的分析:

Index

Documents

Result

age:20, gender:Female

{ "name" : "Will1", "gender" : "Female", "age" : 20 }

"n" : 2

age:20, gender:Female

{ "name" : "Will6", "gender" : "Female", "age" : 20 }

"nscannedObjects" : 2

age:20, gender:Male

{ "name" : "Will5", "gender" : "Male", "age" : 20 }

"nscanned" : 4

age:21, gender:Male

{ "name" : "Will4", "gender" : "Male", "age" : 21 }

 

age:21, gender:Male

{ "name" : "Will8", "gender" : "Male", "age" : 21 }

 

age:22, gender:Female

{ "name" : "Will0", "gender" : "Female", "age" : 22}

 

age:23, gender:Male

{ "name" : "Will3", "gender" : "Male", "age" : 23 }

 

age:24, gender:Female

{ "name" : "Will7", "gender" : "Female", "age" : 24 }

 

age:24, gender:Female

{ "name" : "Will9", "gender" : "Female", "age" : 24 }

 

age:24, gender:Male

{ "name" : "Will2", "gender" : "Male", "age" : 24 }

 

 

  • 使用"gender_1_age_1"索引的输出以下
     1 > db.school.students.find({"age":{"$gte":23}, "gender":"Female"}).hint("gender_1_age_1").explain()
     2 {
     3         "cursor" : "BtreeCursor gender_1_age_1",
     4         "isMultiKey" : false,
     5         "n" : 2,
     6         "nscannedObjects" : 2,
     7         "nscanned" : 2,
     8         "nscannedObjectsAllPlans" : 2,
     9         "nscannedAllPlans" : 2,
    10         "scanAndOrder" : false,
    11         "indexOnly" : false,
    12         "nYields" : 0,
    13         "nChunkSkips" : 0,
    14         "millis" : 0,
    15         "indexBounds" : {
    16                 "gender" : [
    17                         [
    18                                 "Female",
    19                                 "Female"
    20                         ]
    21                 ],
    22                 "age" : [
    23                         [
    24                                 23,
    25                                 1.7976931348623157e+308
    26                         ]
    27                 ]
    28         },
    29         "server" : "××××:27017"
    30 }
    31 >

     

    索引的分析:

Index

Documents

Result

gender:Female, age:20

{ "name" : "Will1", "gender" : "Female", "age" : 20 }

"n" : 2

gender:Female, age:20

{ "name" : "Will6", "gender" : "Female", "age" : 20 }

"nscannedObjects" : 2

gender:Female, age:22

{ "name" : "Will0", "gender" : "Female", "age" : 22 }

"nscanned" : 2

gender:Female, age:24

{ "name" : "Will7", "gender" : "Female", "age" : 24 }

 

gender:Female, age:24

{ "name" : "Will9", "gender" : "Female", "age" : 24 }

 

gender:Male, age:20

{ "name" : "Will5", "gender" : "Male", "age" : 20 }

 

gender:Male, age:21

{ "name" : "Will4", "gender" : "Male", "age" : 21 }

 

gender:Male, age:21

{ "name" : "Will8", "gender" : "Male", "age" : 21 }

 

gender:Male, age:23

{ "name" : "Will3", "gender" : "Male", "age" : 23 }

 

gender:Male, age:24

{ "name" : "Will2", "gender" : "Male", "age" : 24 }

 

 

经过上面的例子能够看出,在使用组合索引的时候仍是要考虑不少东西的,因此能够结合explain()来进行分析。

 

索引选择机制

因为咱们前面建立了三个索引,下面咱们直接使用默认查询。

 1 > db.school.students.find({"age":{"$gte":23}, "gender":"Female"}).explain()
 2 {
 3         "cursor" : "BtreeCursor gender_1_age_1",
 4         "isMultiKey" : false,
 5         "n" : 2,
 6         "nscannedObjects" : 2,
 7         "nscanned" : 2,
 8         "nscannedObjectsAllPlans" : 2,
 9         "nscannedAllPlans" : 2,
10         "scanAndOrder" : false,
11         "indexOnly" : false,
12         "nYields" : 0,
13         "nChunkSkips" : 0,
14         "millis" : 0,
15         "indexBounds" : {
16                 "gender" : [
17                         [
18                                 "Female",
19                                 "Female"
20                         ]
21                 ],
22                 "age" : [
23                         [
24                                 23,
25                                 1.7976931348623157e+308
26                         ]
27                 ]
28         },
29         "server" : "××××:27017"
30 }
31 >

 

存在多条索引的状况下,MongoDB首选nscanned值最低的索引。

 

索引和排序

基于上面的例子,咱们加上对"name"的排序操做。这时,咱们能够看到"scanAndOrder"变成了"true"。

 1 > db.school.students.find({"age":{"$gte":23}, "gender":"Female"}).sort({"name":1}).explain()
 2 {
 3         "cursor" : "BtreeCursor gender_1_age_1",
 4         "isMultiKey" : false,
 5         "n" : 2,
 6         "nscannedObjects" : 2,
 7         "nscanned" : 2,
 8         "nscannedObjectsAllPlans" : 7,
 9         "nscannedAllPlans" : 9,
10         "scanAndOrder" : true,
11         "indexOnly" : false,
12         "nYields" : 0,
13         "nChunkSkips" : 0,
14         "millis" : 0,
15         "indexBounds" : {
16                 "gender" : [
17                         [
18                                 "Female",
19                                 "Female"
20                         ]
21                 ],
22                 "age" : [
23                         [
24                                 23,
25                                 1.7976931348623157e+308
26                         ]
27                 ]
28         },
29         "server" : "××××:27017"
30 }

 

在这个例子中,"nscanned"是最小的,因此这个方案是查询效率最高的。可是,咱们要注意一下"scanAndOrder",根据MongoDB文档的解释,查询结果的排序不能利用现有的索引,MongoDB会把find找到的结果放入内存从新排序。这样的话,若是数据量很大,会对性能产生很大的影响。

最好的办法是利用索引来进行排序。

在这种状况下,就要加入一个"name"的索引,同时在find操做时使用hint来指定索引方式,由于默认状况MongoDB会选择"nscanned"最小的方式。

 1 > db.school.students.ensureIndex({"gender":1,"name":1})
 2 > db.school.students.find({"age":{"$gte":23}, "gender":"Female"}).sort({"name":1}).hint("gender_1_name_1").explain()
 3 {
 4         "cursor" : "BtreeCursor gender_1_name_1",
 5         "isMultiKey" : false,
 6         "n" : 2,
 7         "nscannedObjects" : 5,
 8         "nscanned" : 5,
 9         "nscannedObjectsAllPlans" : 5,
10         "nscannedAllPlans" : 5,
11         "scanAndOrder" : false,
12         "indexOnly" : false,
13         "nYields" : 0,
14         "nChunkSkips" : 0,
15         "millis" : 0,
16         "indexBounds" : {
17                 "gender" : [
18                         [
19                                 "Female",
20                                 "Female"
21                         ]
22                 ],
23                 "name" : [
24                         [
25                                 {
26                                         "$minElement" : 1
27                                 },
28                                 {
29                                         "$maxElement" : 1
30                                 }
31                         ]
32                 ]
33         },
34         "server" : "xxxx:27017"
35 }
36 >

 

经过这种方式,就能够利用索引的排序来避免"scanAndOrder"为true的状况。可是再看看上面的方式,彷佛能够进一步优化,虽然不能减小"nscanned",可是能够减小"nscannedObjects"。

 1 > db.school.students.ensureIndex({"gender":1,"name":1,"age":1})
 2 > db.school.students.find({"age":{"$gte":23}, "gender":"Female"}).sort({"name":1}).hint("gender_1_name_1_age_1").explain()
 3 {
 4         "cursor" : "BtreeCursor gender_1_name_1_age_1",
 5         "isMultiKey" : false,
 6         "n" : 2,
 7         "nscannedObjects" : 2,
 8         "nscanned" : 5,
 9         "nscannedObjectsAllPlans" : 2,
10         "nscannedAllPlans" : 5,
11         "scanAndOrder" : false,
12         "indexOnly" : false,
13         "nYields" : 0,
14         "nChunkSkips" : 0,
15         "millis" : 0,
16         "indexBounds" : {
17                 "gender" : [
18                         [
19                                 "Female",
20                                 "Female"
21                         ]
22                 ],
23                 "name" : [
24                         [
25                                 {
26                                         "$minElement" : 1
27                                 },
28                                 {
29                                         "$maxElement" : 1
30                                 }
31                         ]
32                 ],
33                 "age" : [
34                         [
35                                 23,
36                                 1.7976931348623157e+308
37                         ]
38                 ]
39         },
40         "server" : "xxxx:27017"
41 }
42 >

 

 

总结

MongoDB中,索引还有不少东西,本文只是经过一些例子来介绍了索引的使用,以及组合索引的简单分析

Ps: 本文中全部例子中的命令均可以参考如下连接

http://files.cnblogs.com/wilber2013/index.js

相关文章
相关标签/搜索