mongoDB index introduction

索引为mongoDB的查询提供了有效的解决方案,若是没有索引,mongodb必须的扫描文档集中全部记录来match查询条件的记录。然而这些扫描是没有必要,并且每一次操做mongod进程会处理大量的数据。mongodb

索引是一种存储文档集中一部分数据集的特殊的数据结构,以便更容易的去遍历。索引存储了一个经过value排序具体字段或者字段集。数据库

mongodb的索引和其余数据库系统的索引基本上相同。mongodb的索引在文档集collection层面上,而且支持文档集的任何字段或复合字段上面建索引。数组

一个合适的索引会大大减小mongoDB检索文档集的次数。在某些状况下,mongoDB能够用索引去判断哪些记录复合查询条件。数据结构

下图阐释了一个利用索引的查询:less

Diagram of a query selecting documents using an index. MongoDB narrows the query by scanning the range of documents with values of ``score`` less than ``30``.

diagram of a query selecting documents using an index. MongoDB narrows the query by scanning the range of documents with values of score less than 30.ide

Tip性能

Create indexes to support common and user-facing queries. Having these indexes will ensure that MongoDB only scans the smallest possible number of documents.测试

索引也能够优化性能在如下具体状况:优化

排序结果集spa

mongodb能够直接返回用索引排序文档,不须要额外的排序阶段。

Diagram of a query that uses an index to select and return sorted results. The index stores ``score`` values in ascending order. MongoDB can traverse the index in either ascending or descending order to return sorted results.

 

Covered Results

当查询条件规则和查询的映仅仅包括索引字段,mongoDB将直接从索引数据里返回结果集,不须要扫描任何文档,不须要将文档放入内存。这些覆盖的查询将会很是高效。并且,这些索引也能够covers聚合管道的操做。

Diagram of a query that uses only the index to match the query criteria and return the results. MongoDB does not need to inspect data outside of the index to fulfill the query.

索引类型

mongoDB提供了一系列的索引类型来支持具体的数据和查询。

Default _id

默认状况,mongoDB全部的collections都有一个索引在_id字段,若是应用没有具体为_id字段制定一个具体的driver,mongod将会建立一个值为ObjectID的_id字段。

_id的索引是惟一索引,不容许客户端插入_id字段值相同的两条记录。

Single Field (单一字段)

为了补充mongoDB自定义的_id索引,mongoDB支持用户在文档的单个字段上面自定义索引。下图是单一索引的说明:

Diagram of an index on the ``score`` field (ascending).

Diagram of an index on the score field (ascending).

Compound Index(复合索引)

MongoDB也支持用户在符合的字段上面自定义索引,这些复合索引和单一字段索引相似;然而,当查询条件依赖于额外的字段,复合索引中字段的顺序会有很大的影响。举个例子来讲,若是一个复合索包括{ userid: 1, score: -1 },索引首先经过userid排序,而后经过score的value排序,下图是复合索引的说明:

 

Diagram of a compound index on the ``userid`` field (ascending) and the ``score`` field (descending). The index sorts first by the ``userid`` field and then by the ``score`` field.

Diagram of a compound index on the userid field (ascending) and the scorefield (descending). The index sorts first by the userid field and then by thescore field.

Multikey Index(多键索引)

mongoDB用多键索引为存储数组内容的字段作索引,若是你的索引字段包含一个数组,MongoDB建立单独的索引为数组里面的每个元素。这些多键索引容许查询去选择包含数组中某个元素或者全部元素。MongoDB自动的决定是否建立多键索引当索引字段中包含数组,你不须要明确的去制定多建类型。下图为多键索引的说明:

 

Diagram of a multikey index on the ``addr.zip`` field. The ``addr`` field contains an array of address documents. The address documents contain the ``zip`` field.

Diagram of a multikey index on the addr.zip field. The addr field contains an array of address documents. The address documents contain the zip field.

Geospatial Index(地理空间索引)

为了有效支持地理空间坐标数据的查询,mongoDB提供了两个特殊的索引:

2d indexes 返回结果用平面几何 ,2sphere indexes 返回结果用球面几何

See 2d Index Internals for a high level introduction to geospatial indexes.

Text Indexes(文本索引)

MongoDB为文档中字符串内容提供一个测试版的文本类型的索引。这些文本索引不存储特定语言的停顿词 (e.g. “the”, “a”, “or”) 。

See Text Indexes for more information on text indexes and search.

Hashed Indexes(哈希索引)

为了支持基于哈希的分片,mongoDB提供了哈希索引类型。哈希索引经过字段的哈希值来检索,这些索引有更随机的分布,可是不适用于对于基于范围的查询

Index Properties(索引的属性)

Unique Indexes

惟一索引不容许索引重复索引字段值。若是要在一个已经有重复的字段上面见索引,see Drop Duplicates for index creation options.除了惟一约束,惟一索引和MongoDB的其余索引能够互换。

Sparse Indexes

索引稀疏的属性确保索引仅仅包含文档集中有索引字段的的实体,索引忽略文档中没有索引字段的记录。你能够联合稀疏索引和惟一索引来约束文档中重复的值忽略文档中没有索引key的记录。

相关文章
相关标签/搜索