MongoDB study notes (six)

MongoDB acquaintance in the index

Index of books like directories, allows us to quickly locate the desired content, relational database has an index, NoSQL of course there are, this article we take a brief introduction of the index in MongoDB.

Index creation

By default, the collection of  _id field is the index, we can see a collection indexed by getIndexes () method:

1
db.sang_collect.getIndexes()

The results are as follows:

1
2
3
4
5
6
7
8
9
10
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "sang.sang_collect"
}
]

We see that there is only one index, that is  _id.

Now my collection of 10,000 documents, I want to query the document x 1, my query is as follows:

1
db.sang_collect.find({x:1})

By default, this query will do a full table scan, we can spend explain article describes () to look at the query plan, as follows:

1
db.sang_collect.find({x:1}).explain("executionStats")

The results are as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
"queryPlanner" : {
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 15,
"totalKeysExamined" : 0,
"totalDocsExamined" : 10000,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"x" : {
"$eq" : 1.0
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 29,
"works" : 10002,
"advanced" : 1,
"needTime" : 10000,
"needYield" : 0,
"saveState" : 78,
"restoreState" : 78,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 10000
}
},
"serverInfo" : {
},
"ok" : 1.0
}

The results longer, I picked up the key part. We can see a full table scan query, scanning a total of 10,000 documents only found out the results I want. In fact I want to document the second row, but the system does not know this set a total number of x is 1 of the document, it will complete a full table scan, of course, very inefficient in this way, but if I add limit, as follows:

1
db.sang_collect.find({x:1}).limit(1)

Then look at the query plan scans found only two documents have the results, but if I have to find x 9999 for the record, that still have the full table scan again, this time, we will be able to index the field, indexing as follows:

1
db.sang_collect.ensureIndex({x:1})

1 for ascending, descending -1 means. When we index to field x, then x field according to a query, the speed is very fast, we look at the following query execution plan operations:

1
db.sang_collect.find({x:9999}).explain("executionStats")

这个查询计划过长我就不贴出来了,我们可以重点关注查询要耗费的时间大幅度下降。

此时调用 getIndexes() 方法可以看到我们刚刚创建的索引,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "sang.sang_collect"
},
{
"v" : 2,
"key" : {
"x" : 1.0
},
"name" : "x_1",
"ns" : "sang.sang_collect"
}
]

我们看到每个索引都有一个名字,默认的索引名字为 字段名_排序值,当然我们也可以在创建索引时自定义索引名字,如下:

1
db.sang_collect.ensureIndex({x:1},{name:"myfirstindex"})

此时创建好的索引如下:

1
2
3
4
5
6
7
8
{
"v" : 2,
"key" : {
"x" : 1.0
},
"name" : "myfirstindex",
"ns" : "sang.sang_collect"
}

当然索引在创建的过程中还有许多其他可选参数,如下:

1
db.sang_collect.ensureIndex({x:1},{name:"myfirstindex",dropDups:true,background:true,unique:true,sparse:true,v:1,weights:99999})

关于这里的参数,我说一下:

  1. name 表示索引的名称
  2. dropDups 表示创建唯一性索引时如果出现重复,则将重复的删除,只保留第一个
  3. background 是否在后台创建索引,在后台创建索引不影响数据库当前的操作,默认为 false
  4. unique 是否创建唯一索引,默认 false
  5. sparse 对文档中不存在的字段是否不起用索引,默认 false
  6. v 表示索引的版本号,默认为 2
  7. weights 表示索引的权重

此时创建好的索引如下:

1
2
3
4
5
6
7
8
9
10
11
12
{
"v" : 1,
"unique" : true,
"key" : {
"x" : 1.0
},
"name" : "myfirstindex",
"ns" : "sang.sang_collect",
"background" : true,
"sparse" : true,
"weights" : 99999.0
}

查看索引

上文我们介绍了 getIndexes() 可以用来查看索引,我们还可以通过 totalIndexSize() 来查看索引的大小,如下:

1
db.sang_collect.totalIndexSize()

删除索引

我们可以按名称删除索引,如下:

1
db.sang_collect.dropIndex("xIndex")

表示删除一个名为xIndex的索引,当然我们也可以删除所有索引,如下:

1
db.sang_collect.dropIndexes()

总结

索引是个好东西,可以有效的提高查询速度,但是索引会降低插入、更新和删除的速度,因为这些操作不仅要更新文档,还要更新索引,MongoDB 限制每个集合上最多有 64 个索引,我们在创建索引时要仔细斟酌索引的字段。

好了,MongoDB 中的索引入门我们就说到这里,小伙伴们有问题欢迎留言讨论。

参考资料:

  1. 《MongoDB权威指南第2版》

Guess you like

Origin www.cnblogs.com/eer123/p/11734081.html