MongoDB Learning Summary-4 (Index operations)

Source: Internet
Author: User

These days the project revision, time is tight, blog also did not follow, also hope everyone forgive.

Well, today we share the basic operation of the index in MongoDB, we do not have to develop the daily development of the program to optimize performance, and the operation of the program is nothing more than curd, usually we

And will spend 50% of the time on the R, because the read operation is very sensitive to the user, processing is not good will be spurned, hehe.

There are 5 classic lookups from the algorithm, specifically in my algorithm crash series, which includes what we call "Index lookup" today, if you know more about SQL Server

, we believe that index search can bring us a performance improvement.

We first insert 10w data, speak:

One: Performance analysis function (explain)

Well, the data has been inserted successfully, since we have to do analysis, we must have a tool for analysis, fortunately, MongoDB provides us with a key word called "explain", then how to use it?

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

Look closely at the red area, there are several key we care about.

Cursor: Here is the "basiccursor", what is the meaning of the search here is the "table scan", that is, the order of search, very sad urge ah.

Nscanned: Here is 10w, that is to say the database browsed 10w documents, very scary bar, so that people can't stand ah.

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

Millis: This is our most most .... Care for something that takes 114 milliseconds altogether.

II: Indexing (Ensureindex)

Finding a document in this simple collection of 10w 114 milliseconds is a little bit unacceptable, okay, so how do we optimize it? In MongoDB give

We brought the index to find out and see if we could get our query soar .....

Here we use the Ensureindex to build an index on name. "1": Indicates ascending by name, "1": Indicates descending by name.

Come and look at these sensitive messages, my God.

Cursor: Here is the "btreecursor", so the ox X,MONGODB the structure of the B-tree to hold the index, the index is named after the "Name_1".

nscanned: I wipe, the database only browsing a document is OK.

N: Direct position return.

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

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

Three: Unique index

As with SQL Server, you can create a unique index, and duplicate key values cannot be inserted naturally, and the use in MongoDB is as follows:

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

Four: Combined index

Sometimes our query is not single-condition, may be multi-conditional, such as looking for the "1989-3-2 's" named ' Jack ' classmate, then we can establish "name" and "Birthday"

Federated indexes to speed up the query.

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

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

At this point we must be very curious, in the end the query optimizer will use which query as the operation, hehe, or look at:

After reading we want to believe the query optimizer, it gives us the choice is often optimal, because when we do queries, the query optimizer will use the indexes we built to create the query scheme,

If one executes the other, the other query scheme is close, and the scheme is saved by MongoDB, of course, if you want to use your own specified query scheme, this is

Yes, in MongoDB we are provided with a hint method that allows us to execute violently.

Five: Delete index

As business needs change, the original index may not be necessary, and some people may want to say that there is no need to do so, but keep in mind that the index will reduce the cud three

The performance of the operation, because this thing needs real-time maintenance, so what the problem should be considered, here to clear the index just created to demonstrate: the use of dropindexes.

MongoDB Learning Summary-4 (Index operations)

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.