MongoDB: mongodb index operations

Source: Internet
Author: User

For databases, it is nothing more than adding, deleting, modifying, and querying. Generally, in project applications, READ operations account for more than 50% of the operations, and customers are usually sensitive to this, it is not easy to handle in terms of efficiency, and it is often to be put aside by future generations! So now we need indexes to play a role. Next, let's take a look at the sensory differences brought by indexes. Then, let's talk about the mongodb index operations in detail!

1. First, we insert 0.1 million data records into the database:

for(var i=0;i<100000;i++){var index=parseInt(i*Math.random());db.person.insert({"name":"jessonlv"+i,"age":i});}

 

Then we can find the data of the first 10 thousand data entries, that is, the data name is jessonlv10000. Note that we have not created an index for person before, and mongodb has provided us with the explain keyword so that we can perform analysis operations.

db.person.find({"name":"jessonlv"+10000}).explain()

Cursor: "BasicCursor" indicates that table scanning is used for table search, that is, sequential search.

Nscanned: This indicates that the query operation browses a total of 0.1 million data (documents), that is, the total number of documents in the table. A little bit.

Millis: This is our most important time. It took 66 milliseconds in total. Join! We can do better.

2. Create an index:

Create an index db. person. ensureIndex ({"name": 1}) db. person. find ({"name": "jessonlv" + 10000}). explain ()

 

After the index is created, we can compare the parameter values before the index is created. We searched for a document in total, and the time used was far from 0 (in fact, it cannot be zero, it's just that the speed is too fast. So far, we should feel this ice-fire has been around for two days ......

Iii. Unique Index

Mongodb operates like this:

db.person.ensureIndex({"name":1},{"unique":true})
Duplicate keys cannot be inserted.

Iv. Composite Index

Composite Index db. person. insert ({"name": "je", "age": 26, "birthday": "1986-5-4"}) db. person. insert ({"name": "jes", "age": 26, "birthday": "1986-4-4"}) db. person. insert ({"name": "jess", "age": 26, "birthday": "1986-3-4"}) db. person. insert ({"name": "jesso", "age": 26, "birthday": "1986-2-4"}) db. person. insert ({"name": "jessonlv", "age": 26, "birthday": "1986-1-4"}) index creation: db. person. ensureIndex ({"name": 1, "birthday": 1}) db. person. ensureIndex ({"birthday": 1, "name": 1}) db. person. getIndexs ()

Here, when we use the query, the optimizer will use the Optimal Index to query

db.person.find({"birthday":"1986-5-4","name":"jes"}).explain()

The query optimizer uses the indexes we have created to create a query scheme. If one of the indexes is executed first, the other query schemes are closed, this scheme will be saved by mongodb. Of course, if you have to use your own query scheme, this is also possible. In mongodb, we provide the hint Method for us to execute it violently.

5. delete an index

Our business needs are constantly changing, so the index must change according to the actual situation. The index needs to be maintained and the original index needs to be deleted and then created.

db.person.dropIndexes("1")

So... over, I will summarize the master-slave replication of mongodb in the next period.

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.