An analysis of indexing operation of MongoDB introductory tutorials _mongodb

Source: Internet
Author: User
Tags mongodb

These days the project revision, the time is comparatively tight, the blog also did not keep abreast, also hoped everybody forgive me.

Well, today sharing the basic operation of the index in the MongoDB, we do not avoid the day-to-day development of the program to optimize performance, and the operation of the program is nothing but curd, usually we

It will take 50% of the time on the r above, because the read operation is very sensitive to the user, processing is not good will be spurned, hehe.

There are 5 kinds of classic search algorithms, the specific can see my algorithm crash series, which includes what we call Today "Index Lookup", if you know more about SQL Server

, I believe that index lookup can bring us some kind of performance improvement.

We first insert 10w data, on the illustrated words:

One: Performance analysis function (explain)

Well, the data has been inserted successfully, since we have to do analysis, we must have the tools of analysis, fortunately MongoDB to provide us with a keyword called "explain", then how to use it?

Or look at the picture, note that the name field here does not establish any index, here I query a "name10000" name.

Take a closer look at the red area, there are several key we care about.

Cursor: Here is the "basiccursor", what meaning, that is to find the use of "table scan", that is, the order to find, very sad reminders ah.

Nscanned: This is 10w, that is to say, the database browsed 10w documents, it is very scary, so it is unbearable to play.

N: Here is 1, which is the final return of 1 documents.

Millis: This is our most .... It takes 114 milliseconds to take care of something.

II: Indexing (Ensureindex)

To find a document in a simple set of 10w is 114 milliseconds a little bit is not acceptable, OK, so how do we optimize it? In MongoDB

We brought the index lookup to see if we could get our query soaring ....

Here we use the Ensureindex to set up an index on name. "1": Means ascending by name, "-1": Means descending by name.

My God, take a look at these sensitive information again.

cursor: Here is the "btreecursor", so the cow X,MONGODB the structure of the B-tree to store the index, the index is called "Name_1" behind.

nscanned: i wipe, the database only browse a document on the OK.

N: direct positioning return.

Millis: Look at this time really can't believe, seconds to kill.

In this example, I believe that we have a sense of the index of the understanding of it.

Three: Unique index

As with SQL Server, a unique index can be established, and duplicate key values are naturally not inserted, and in MongoDB the following methods are used:

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

Four: Combined index

Sometimes our query is not a single condition, may be a lot of conditions, such as looking for the ' 1989-3-2 ' named ' Jack ', then we can create a "name" and "Birthday" of the joint Index to speed up the query.

See above, you may also know that name is different from birthday, and that the index is different, and the order of ascending and descending will produce different indexes.

Then we can use getindexes to see exactly which indexes are generated in the next person collection.

At this time we must be very curious, in the end query optimizer will use which query as an operation, hehe, or look at the effect of the picture:

After reading the above figure we have to believe that the query optimizer, it gives us the choice is often the best, because when we do the query, the query optimizer will use the indexes we established to create the query scheme,

If one of the first finishes, the other query is close, and the scheme is saved by MongoDB, if you have to use your own specific query scheme, which is

Can, in the MongoDB gives us the hint method to let us be able to execute violently.

Five: Delete index

Perhaps as business requirements change, the original index may not be necessary to exist, some people may want to say that there is no need to chant, but remember, the index will reduce cud these three

The performance of the operation, because this thing needs to be maintained in real time, so everything should be considered in a comprehensive way, here is the index just created to clear out 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.