Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of MongoDB Index Management

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shares with you the content of a sample analysis of MongoDB index management. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Index is the most effective means to improve the efficiency of query. An index is a special data structure, which stores part of the data in a form that is easy to traverse (for example, a specific field or a set of field values), and the index sorts the stored values according to certain rules. and the storage location of the index is in memory, where it is very fast to retrieve data from the index. Without an index, MongoDB must scan every document in the collection, which is very inefficient, especially when the amount of data is large.

1. Create / rebuild index

MongoDB creates a new index using the ensureIndex () method, and an existing index can be rebuilt using reIndex ().

1.1 create index ensureIndex ()

MongoDB uses the ensureIndex () method to create the index.

Grammatical structure

Db.COLLECTION_NAME.ensureIndex (keys [, options])

Keys, the list of parameters to index. For example: {KEY:1}, where key represents the field name, 1 indicates ascending sort, or you can use the number-1 descending order.

Options, an optional parameter, indicates the setting for indexing. Available values are as follows:

Background,Boolean, indexing in the background so that other database activity is not blocked when indexing. The default is false.

Unique,Boolean, create a unique index. The default is false.

Name,String, specifying the name of the index. If not specified, MongoDB generates an index field whose name is concatenated with the sort order.

DropDups,Boolean, when creating a unique index, only the first index is retained if the same index is deleted repeatedly.

Sparse,Boolean, which does not enable indexing for field data that does not exist in the document. The default value is false.

V _ index version, the version number of the index.

Weights,document, the index weight value, with a value between 1 and 99999, indicates the score weight of the index relative to other index fields.

For example, index the collection sites:

> db.sites.ensureIndex ({name: 1, domain:-1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1}

Note: createIndex () was used to create the index before version 1.8, but this method has been removed since version 1.8.

1.2 re-index reIndex ()

Db.COLLECTION_NAME.reIndex ()

For example, rebuild all indexes of the collection sites:

> db.sites.reIndex () {"nIndexesWas": 2, "nIndexes": 2, "indexes": [{"key": {"_ id": 1}, "name": "_ id_", "ns": "newDB.sites"}, {"key": {"name": 1, "domain":-1}, "name": "name_1_domain_-1" "ns": "newDB.sites"}], "ok": 1}

two。 View Index

MongoDB provides a way to view index information: the getIndexes () method can be used to view all indexes of the collection, totalIndexSize () to view the total size of the collection index, and db.system.indexes.find () to view all index information in the database.

2.1 View the index getIndexes () in the collection

Db.COLLECTION_NAME.getIndexes ()

For example, look at the index in the collection sites:

> db.sites.getIndexes () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "newDB.sites"}, {"v": 1, "key": {"name": 1, "domain":-1}, "name": "name_1_domain_-1" "ns": "newDB.sites"}]

2.2 View the index size in the collection totalIndexSize ()

Db.COLLECTION_NAME.totalIndexSize ()

For example, view the sites index size of the collection:

> db.sites.totalIndexSize () 16352

2.3 View all indexes in the database db.system.indexes.find ()

Db.system.indexes.find ()

For example, all indexes in the current database:

> db.system.indexes.find ()

3. Delete index

We can delete the index that is no longer needed. When you delete an index, you can delete one of the indexes in the collection and delete all the indexes.

3.1 Delete the specified index dropIndex ()

Db.COLLECTION_NAME.dropIndex ("INDEX-NAME")

For example, delete the index named "name_1_domain_-1" in the collection sites:

> db.sites.dropIndex ("name_1_domain_-1") {"nIndexesWas": 2, "ok": 1}

Delete all indexes dropIndexes ()

Db.COLLECTION_NAME.dropIndexes ()

For example, delete all indexes in the collection sites:

> db.sites.dropIndexes () {"nIndexesWas": 1, "msg": "non-_id indexes dropped for collection", "ok": 1} Thank you for reading! This is the end of this article on "sample Analysis of MongoDB Index Management". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report