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

编程入门 行业动态 更新时间:2024-10-08 18:34:16
本文介绍了索引如何提高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中的查询性能

本文发布于:2023-10-19 17:33:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   性能   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!