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

What is the knowledge about indexing in mongodb

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

Share

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

This article mainly shows you "what is the knowledge about the index in mongodb", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn "what is the knowledge about index in mongodb".

When we do daily development, we can't avoid optimizing the performance of the program, and the operation of the program is nothing more than CURD, and usually we spend 50% of our time on R, because Read operation is very sensitive to users, and it will be spurned if it is not handled properly.

Algorithmically speaking, there are five classic lookups, specifically you can see my algorithm crash series, which includes what we call "index lookup" today. If you know more about mysql, I believe that index lookup can bring us what kind of performance improvement.

Let's first insert 10w data. The above picture says:

One: performance analysis function (explain)

Well, the data has been inserted successfully, and since we want to do analysis, we must have an analysis tool. Fortunately, mongodb provides us with a keyword called "explain", so how to use it?

Looking at the picture again, notice that there is no index in the name field here, so here I will query the name of a "name10000".

If you take a closer look at the red area, there are several key that we care about.

Cursor: here is "BasicCursor". What does it mean? that is to say, the search here uses "table scan", that is, sequential search, which is very sad.

Nscanned: this is 10w, which means that the database has browsed 10w documents. It's scary, isn't it? it's unbearable to play like this.

N: here is 1, which means that 1 document is finally returned.

Millis: this is our most. It takes a total of 114 milliseconds to care about things.

Second: index establishment (ensureIndex)

It takes 114 milliseconds to find a document in a collection as simple as 10w, which is a little bit unacceptable. Okay, so how do we optimize it? Mongodb brings us index lookups to see if we can make our queries soar.

Here we use ensureIndex to index on name. "1": indicates ascending order according to name, and "- 1" means descending order according to name.

Oh, my God, let's take a look at this sensitive information.

Cursor: what appears here is "BtreeCursor", which uses the B-tree structure to store the index. The index name is the following "name_1".

Nscanned: the database OK after browsing only one document.

N: directly locate and return.

Millis: look at this time. I can't believe it. Second kill.

Through this example, I believe you also have a sensory understanding of the index.

Three: unique index

Like sqlserver, you can build a unique index, and duplicate key values cannot be inserted naturally. In mongodb, you can use the following methods:

Db.person.ensureIndex ({"name": 1}, {"unique": true})

IV: combinatorial index

Sometimes our query is not single-conditional, but may be multi-conditional, such as looking for students born in '1989-3-2' whose name is' jack'', then we can set up a joint index of "name" and "birthday" to speed up the query.

If you see the figure above, you may know that different indexes are built between name and birthday, and different ascending and descending order will produce different indexes, so we can use getindexes to see which indexes are generated in the person collection.

At this point, we must be curious about which query the query optimizer will use as an operation, hehe, or look at the effect diagram:

After looking at the figure above, we have to trust the query optimizer, which gives us the best choice, because when we do a query, the query optimizer will use these indexes to create a query scheme. If one of the query schemes is executed first, the other query schemes will be dropped by close, and this scheme will be saved by mongodb. Of course, if you have to use your own specified query scheme, this is also possible. The hint method is provided to us in mongodb so that we can execute it violently.

Five: delete the index

Maybe with the change of business requirements, the previously established index may not be necessary, and some people may want to say that it is not necessary, but please remember that the index will degrade the performance of these three operations of CUD, because this thing requires real-time maintenance, so all problems should be taken into account. Here, empty the index you just built to demonstrate the use of dropIndexes.

The above is all the contents of the article "what is the knowledge about indexing in mongodb?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Database

Wechat

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

12
Report