In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about how to create an index in MongoDB. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Syntax for MongoDB to create an index
1. Add an index to the normal field and name the index
Db. Collection name .createIndex ({"field name": 1}, {"name": 'idx_ field name'})
Description: (1) Index naming convention: idx_. If the field name is too long, you can use the field abbreviation.
(2) the 1 after the field value represents ascending order; if-1 represents descending order.
two。 Add indexes to embedded fields
Db. Collection name .createIndex ({"field name. embedded field name": 1}, {"name": 'idx_ field name _ embedded field name'})
3. Create an index through the background
Db. Collection name .createIndex ({"field name": 1}, {"name": 'idx_ field name', background:true})
4: composite index
Db. Collection name .createIndex ({"field name 1":-1, "field name 2": 1}, {"name": 'idx_ field name 1 _ field name 2)
5. Set the TTL index
Db. Collection name .createIndex ({"field name": 1}, {"name": 'idx_ field name', expireAfterSeconds: defined time, background:true})
Description: expireAfterSeconds is the expiration time (in seconds)
MongoDB index creation performance increases by 1000 times
Above we introduced the common index creation syntax of MongoDB. Some students also want to see how powerful MongoDB is, so add an example here to feel the performance of the index.
By creating an index on a field, the performance is improved by more than 1000 times from 15.15S before optimization to 0.013S after optimization.
This is a real case of actual production, we have a collection QQStatements, its data volume is 2604W, as shown in the following figure.
The system needs to query the recent changes in this table, that is, the amount of new data and modified data needs to be crawled.
The query statement is as follows:
Db.QQStatements.find ({$or: [{Rec_CreateTime: {$gt: ISODate ("2019-01-07 16")}}, {Rec_ModifyTime: {$gt: ISODate ("2019-01-07 16")}}]})
However, this query statement is not ideal, sometimes it takes 25s, and it takes about 15s after multiple execution with cache, as shown in the following figure:
Looking at this table, it is found that the Rec_CreateTime field is indexed and the statements that meet the Rec_CreateTime conditions are executed separately, which is soon completed within 0.1s.
While the Rec_ModifyTime field does not have an index, it is slow to execute statements that meet the Rec_ModifyTime conditions alone, which requires about 15s.
At this point, you can tell that the problem is the missing index, and the developer confirms that this scenario is often used, and this field needs to be indexed.
Execute the command to add the index:
Db.QQStatements.createIndex ({"Rec_ModifyTime": 1}, {"name": 'idx_Rec_ModifyTime',background:true})
After the index is added to the Rec_ModifyTime field, the execution of the whole statement is reduced to 0.013S (20Smuri-> 0.02S).
The above is the editor for you to share how to create an index in MongoDB, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.