简体   繁体   English

索引如何提高mongodb中的查询性能

[英]How Indexing improve query performance in mongodb

I need to know abt how indexing in mongo improve query performance. 我需要知道abt在mongo中如何提高查询性能。 And currently my db is not indexed. 并且当前我的数据库没有索引。 How can i index an existing DB.? 如何索引现有数据库? Also is i need to create a new field only for indexing.?. 我是否也需要创建一个仅用于索引的新字段?

Fundamentally, indexes in MongoDB are similar to indexes in other database systems. 从根本上讲,MongoDB中的索引类似于其他数据库系统中的索引。 MongoDB supports indexes on any field or sub-field contained in documents within a MongoDB collection. MongoDB支持MongoDB集合中文档中包含的任何字段或子字段的索引。

Indexes are covered in detail here and I highly recommend reading this documentation. 索引在这里详细介绍,我强烈建议您阅读本文档。

There are sections on indexing operations, strategies and creation options as well as a detailed explanations on the various indexes such as compound indexes (ie an index on multiple fields). 其中包括有关索引操作,策略和创建选项的部分,以及有关各种索引(如复合索引 (即,多个字段的索引))的详细说明。

One thing to note is that by default, creating an index is a blocking operation. 要注意的一件事是,默认情况下,创建索引是一项阻止操作。 Creating an index is as simple as: 创建索引很简单:

db.collection.ensureIndex( { zip: 1})

Something like this will be returned, indicating the index was correctly inserted: 将返回类似这样的内容,指示已正确插入索引:

Inserted 1 record(s) in 7ms

Building an index on a large collection of data, the operation can take a long time to complete. 基于大量数据建立索引,该操作可能需要很长时间才能完成。 To resolve this issue, the background option can allow you to continue to use your mongod instance during the index build. 要解决此问题,可以使用background选项在索引构建过程中继续使用mongod实例。

Limitations on indexing in MongoDB is covered here . 这里介绍了MongoDB中的索引限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM