MongoDB Learning Note III (MONGODB index operation and performance test)

Source: Internet
Author: User
Tags mongodb client

Chapter III index operations and performance testing The importance of indexes under big data is not much to say.The following test used a MongoDB client tool Robomongo, you can choose to download on the Internet. Inserting test data

Insert 1 million test data first

 for (var i=1;i<1000000; i++) {  var person={name: "Jack" +i,age:i,address:["Henan", "Wuhan"],course:[{name: "Shuxue", Score:i  },{Name: "Wuli", Score:i}]}db. DemoTest.Person.insert (person)}

Performance analysis functions (explain)

MongoDB gives us a keyword called "explain" for performance analysis.

Perform the query analysis as follows:

Db. DemoTest.Person.find ({Name: "jack5784124"}). Explain ()


从我们可以看出在没有建立索引的情况下,查询的时间大约是是615毫秒。


Create an index
Db. TestCollection.Person.ensureIndex ({name:1})

-----1 indicates the index in ascending alphabetical order

Inquire

As we can see from the graph, the query time becomes about 2 milliseconds and the performance is increased by hundreds of times times.

 
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. TestCollection.Person.ensureIndex ({name:1},{"unique": True} )

Combined index

Sometimes our query is not single-condition, may be multi-conditional, then we can set up a composite index to speed up the query.

Db. TestCollection.Person.ensureIndex ({name:1,age:1})

Delete Index

Db. TestCollection.Person.dropIndex ("index name")

 

MongoDB Learning Note III (MONGODB index operation and performance test)

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.