Apsaradb for MongoDB-index operation on the fourth day

Source: Internet
Author: User

I hope you will not be able to keep up with your blog after the project is revised over the past few days.

Well, today we will share with you the basic indexing operations in mongodb. We will not be able to optimize the performance of our programs during our daily development. The operations of programs are nothing more than CURD.

It will spend 50% of the time on R, because the Read operation is very sensitive to users. If the processing is not good, it will be put aside.

There are five typical search algorithms. For more information, see my quick algorithm series. This includes what we call index search today. If you know more about sqlserver

I believe that index search can improve our performance.

First, we need to insert 10 million data:

 

I. Performance analysis functions (explain)

Now that the data has been inserted successfully, we must have an analysis tool. Fortunately, mongodb provides us with the keyword "explain". How can we use it?

As shown in the figure, note that no index is created for the name field. Here, I will query the name of "name10000.

Take a closer look at the red area. There are several keys that we care about.

Cursor: "BasicCursor" appears here. 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 10 million. That is to say, the database has browsed 10 million documents. It is terrible. It can't be used to it.

N: Here is 1, that is, 1 Document is returned.

Millis: This is what we care about most. It takes 114 milliseconds in total.

 

Ii. ensureIndex)

It takes 114 milliseconds to search for a document in a simple set of 10 million records, which is unacceptable. Well, how can we optimize it? In mongodb

We brought in index search to see if we can make our queries fly in the sky .....

Here we use ensureIndex to create an index on name ." 1 ": Indicates ascending order by name,"-1 ": indicates descending order by name.

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

Cursor: here we see "BtreeCursor". In this case, mongodb uses the B-tree structure to store indexes. The index name is "name_1 ".

Nscanned: I wiped it. It is OK if the database only browses one document.

N: locate the returned result directly.

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

 

Through this example, I believe you have a sensory understanding of indexing.

 

Iii. Unique Index

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

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

 

Iv. Composite Index

Sometimes our query is not a single condition, but may be a multi-condition. For example, if we look for a student born in '2017-3-2 'named 'jack, then we can create "name" and "Birthday"

To accelerate the query.

As you can see, you may also know that the name and birthday are different and the indexes created are different. Different indexes are generated in different ascending and descending order,

Then we can use getindexes to check which indexes are generated in the person set.

 

At this point, we must be very curious about which query will be used by the query optimizer as an operation:

After reading the query optimizer, we need to believe that the query optimizer is always the best choice, because when we make a query, the query optimizer will use the indexes we have created to create a query scheme,

If a query is completed first, other query schemes will be closed. This scheme will be saved by mongodb. Of course, if you have to use your own query scheme, this is also

Yes. The hint method is provided in mongodb to allow brute force execution.

 

5. delete an index

As the business needs change, the original index may not be necessary. Some people may want to say that it is unnecessary if it is not necessary, but remember that the index will reduce CUD.

Operation Performance, because this requires real-time maintenance, so we need to consider all the issues. Here we will clear the index We just created to demonstrate the use of dropIndexes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.