In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you what the index in MongoDB is like, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
An index is like a book catalog, which allows us to quickly locate the content we need. There are indexes in relational databases and certainly in NoSQL. In this article, we will briefly introduce the indexes in MongoDB.
Index creation
By default, the _ id field in the collection is the index, and we can view the index in a collection through the getIndexes () method:
Db.sang_collect.getIndexes ()
The results are as follows:
[{"v": 2, "key": {"_ id": 1}, "name": "_ id_", "ns": "sang.sang_collect"}]
We see that there is only one index here, which is _ id.
Now that I have 10000 documents in my collection, I want to query the document with x = 1. My query operation is as follows:
Db.sang_collect.find ({XRO 1})
This kind of query does a full table scan by default, and we can use explain () introduced in the previous article to look at the query plan, as follows:
Db.sang_collect.find ({XRV 1}) .explain ("executionStats")
The results are as follows:
{"queryPlanner": {}, "executionStats": {"executionSuccess": true, "nReturned": 1, "executionTimeMillis": 15, "totalKeysExamined": 0, "totalDocsExamined": 10000, "executionStages": {"stage": "COLLSCAN" "filter": {"x": {"$eq": 1.0}}, "nReturned": 1, "executionTimeMillisEstimate": 29, "works": 10002, "advanced": 1, "needTime": 10000 "needYield": 0, "saveState": 78, "restoreState": 78, "isEOF": 1, "invalidates": 0, "direction": "forward", "docsExamined": 10000}, "serverInfo": {}, "ok": 10000}
The result is relatively long, I extracted the key part. We can see that the query method is a full table scan, a total of 10000 documents were scanned to find out the results I wanted. In fact, the document I want is the second, but the system does not know how many x-1 documents are in this collection, so it scans the whole table, which is of course inefficient, but if I add limit, it is as follows:
Db.sang_collect.find ({XRV 1}) .limit (1)
At this point, if we look at the query plan, we will find that only two documents have been scanned, but if I want to query the record with x = 9999, I still have to scan the whole table. At this point, we can index the field as follows:
Db.sang_collect.ensureIndex ({XRO 1})
1 indicates ascending order, and-1 indicates descending order. When we index the x field, and then query according to the x field, the speed is very fast. Let's take a look at the execution plan of the following query operation:
Db.sang_collect.find ({xpur9999}) .explain ("executionStats")
If this query plan is too long, I won't post it, and we can focus on a significant reduction in the time spent on the query.
At this point, call the getIndexes () method to see the index we just created, as follows:
[{"v": 2, "key": {"_ id": 1}, "name": "_ id_", "ns": "sang.sang_collect"}, {"v": 2, "key": {"x": 1.0} "name": "Xero1", "ns": "sang.sang_collect"}]
We see that each index has a name, and the default index name is the field name _ sort value. Of course, we can also customize the index name when creating the index, as follows:
Db.sang_collect.ensureIndex ({x myfirstindex 1}, {name: "myfirstindex"})
The index created here is as follows:
{"v": 2, "key": {"x": 1.0}, "name": "myfirstindex", "ns": "sang.sang_collect"}
Of course, there are many other optional parameters during the creation of the index, as follows:
Db.sang_collect.ensureIndex ({myfirstindex 1}, {name: "myfirstindex", dropDups:true,background:true,unique:true,sparse:true,v:1,weights:99999})
With regard to the parameters here, let me say:
1.name represents the name of the index
2.dropDups means that if there is a duplicate when creating a unique index, the duplicate will be deleted, leaving only the first one.
Whether or not 3.background creates an index in the background does not affect the current operation of the database. The default is false.
Whether 4.unique creates a unique index. Default is false.
Whether 5.sparse cannot index fields that do not exist in the document. Default is false.
6.v indicates the version number of the index. The default is 2.
7.weights represents the weight of the index
The index created here is as follows:
{"v": 1, "unique": true, "key": {"x": 1.0}, "name": "myfirstindex", "ns": "sang.sang_collect", "background": true, "sparse": true, "weights": 99999.0} View index
We introduced earlier that getIndexes () can be used to view the index, and we can also use totalIndexSize () to see the size of the index, as follows:
Db.sang_collect.totalIndexSize () deletes the index
We can delete the index by name, as follows:
Db.sang_collect.dropIndex ("xIndex")
It means to delete an index named xIndex. Of course, we can also delete all indexes as follows:
The db.sang_collect.dropIndexes () index is a good thing, which can effectively improve the query speed, but the index will slow down the insertion, update, and delete speed, because these operations not only update the document, but also update the index. MongoDB limits a maximum of 64 indexes per collection, so we should carefully consider the fields of the index when creating the index.
The above is what the index in MongoDB is like, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.