简体   繁体   中英

How Indexing improve query performance in mongodb

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

Limitations on indexing in MongoDB is covered here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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