索引如何提高mongodb中的查询性能 [英] How Indexing improve query performance in mongodb

查看:230
本文介绍了索引如何提高mongodb中的查询性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道mongo中的索引如何提高查询性能.并且当前我的数据库没有索引.如何索引现有数据库?我是否还需要创建一个仅用于索引的新字段?

解决方案

从根本上说,MongoDB中的索引类似于其他数据库系统中的索引. MongoDB支持MongoDB集合中文档中包含的任何字段或子字段上的索引.

此处中详细介绍了索引,我强烈建议您阅读本文档. /p>

其中有关于索引操作,策略和创建选项的部分,以及有关各种索引的详细说明,例如化合物索引(即,多个字段上的索引).

要注意的一件事是,默认情况下,创建索引是一项阻止操作. 创建索引很简单:

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

将返回类似这样的内容,表明已正确插入索引:

Inserted 1 record(s) in 7ms

在大量数据上建立索引,该操作可能需要很长时间才能完成.要解决此问题,可以使用background选项在索引构建期间继续使用mongod实例.

此处.

>

I need to know abt how indexing in mongo improve query performance. 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 supports indexes on any field or sub-field contained in documents within a MongoDB collection.

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 (i.e. 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.

Limitations on indexing in MongoDB is covered here.

这篇关于索引如何提高mongodb中的查询性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆