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

Index of 4.mongodb

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

What is an index?

Indexes can usually greatly improve the efficiency of queries. If there is no index, MongoDB must scan each file in the collection and select those records that meet the query criteria when reading data.

The query efficiency of this kind of scanning full collection is very low, especially when dealing with a large amount of data, the query can take dozens of seconds or even minutes, which is very fatal to the performance of the website.

An index is a special data structure, which is stored in a data set that is easy to traverse and read. An index is a structure that sorts the values of one or more columns in a database table.

Second, the operation method of the index.

1. Create a normal index.

MongoDB uses the createIndex () method to create an index the basic syntax format of the createIndex () method is as follows:

Db.collection.createIndex (keys, options)

In the # syntax, the Key value is the index field you want to create, 1 is the specified index in ascending order, and if you want to create the index in descending order, specify-1.

Db.col.createIndex ({"title": 1})

In the # syntax, the Key value is the index field you want to create, 1 is the specified index in ascending order, and if you want to create the index in descending order, specify-1.

The # createIndex method has the following parameters.

CreateIndex () receives optional parameters. The list of optional parameters is as follows:

The ParameterTypeDescriptionbackgroundBoolean indexing process blocks other database operations, and background can specify that the index is created in the background, that is, adding the "background" optional parameter. The default value for background is false. Whether the index established by uniqueBoolean is unique. Specifies that a unique index is created for true. The default value is the name of the false.namestring index. If not specified, MongoDB generates an index name by concatenating the field name and sort order of the index. The dropDupsBoolean3.0+ version is obsolete. Whether to delete duplicate records when creating a unique index, and specify that true create a unique index. The default value is false.sparseBoolean, which does not enable indexing for field data that does not exist in the document; this parameter requires special attention. If set to true, documents that do not contain corresponding fields will not be queried in the index field. The default value is false.expireAfterSecondsinteger, which specifies a value in seconds, completes the TTL setting, and sets the lifetime of the collection. The version number of the vindex version index. The default index version depends on the version that mongod runs when the index is created. The weightsdocument index weight value, with a value between 1 and 99999, indicates the score weight of the index relative to other index fields. Default_languagestring for text indexing, this parameter determines the list of rules for deactivated words and stemming and lexical organs. The default is English language_overridestring for text index, this parameter specifies the field name included in the document, the language overrides the default language, and the default value is language.2. Create a unique index.

Db.col.createIndex ({"title": 1}, {unique:true})

3. Create a federated index.

When there are many operations of federated query, it is recommended to use federated index.

Example:

Db.user.createIndex ({'height':1} {' birthday':1})

# create a joint index of height and birthday.

# compliant index is not valid for single-condition query! The composite index is valid only for federated queries!

4. Check to see if the index is valid.

When you perform a query operation and want to know whether the query operation has lost the index or a full table query, you can use the explain () method to look at it.

Db.tmp.find ({name: "aaa"}) .explain ()

# We only need to pay attention to the stage field returned by the result. When the stage field is "COLLSCAN", it means that the query result is scanned all over the table and does not hit the index.

When the stage field is ixscan, it represents an index scan.

5. Full-text index.

Attention! Only one full-text index can be created in a collection! But a full-text index can have multiple columns!

Create a full-text index:

Db.tmp.createIndex ({"testtxt":''text "})

When the full-text index is created, according to the previous normal query method, it will not go through the full-text index. if the search needs to go through the full-text index, you need to have a special syntax.

Db.tmp.find ({testtxt: "aaa"}) # General search

Db.tmp.find ({$text: {$search: "aaa"}}) # is searched through a full-text index.

# Fuzzy queries can be made with ordinary indexes, but not full-text indexes!

Perform or / or queries through full-text indexing:

Db.tmp.find ($text: {$search: "ayumi kodakumi"}) # finds fields in the full-text index that contain ayumi or kodakumi.

# ordinary indexing is sorted by acronym, while full-text indexing is based on word segmentation.

# if you need to do full-text indexing in Chinese, mongodb is not recommended, because the Chinese word segmentation of mongo is not good, and when the amount of data is large, the query is very slow!

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