MongoDB Performance Optimization

Source: Internet
Author: User

Mongdb is used in the project. When the data volume is small, it is the same as that of relational databases. Mongdb becomes very slow when data operations change frequently, such as continuous query operations on Data Tables of the hundreds of thousands.

Why?

This is the mongdb operation in spring. Spring's mongotemplate only encapsulates the source method. If there are such problems in spring, the same problem occurs when you directly use the Source Method for Data operations.

The following is my optimization process:

1. indexing has a huge impact on the speed of mongdb queries. There are many ways to create mongdb indexes on the Internet. Here is a practical example:

The name and age attributes of a data table are used as query conditions. The name and age attributes are frequently used. Age and name are used as query conditions at the same time in a specific place, age is rarely used as a query condition.

The indexing process is as follows:

DB. Student. ensureindex ({"name": 1, "Age":-1 },{ "name": "name_age_index "});

If you use the kernel vue client software, you can directly add the index input: {"name": 1, "Age":-1.

Do not drop the table after creating an index.

2. OptimizationCodeImproper use of the method will significantly affect mongdb performance. Here I will list my usage experience:

A. simply update the data using template. updatefirst () or template. updatemulti () instead of template. findandmodify ();

B. Simply add a data operation and use 'template. insert () 'instead of 'template. Save ();

C. query one piece of data and use intent template. findone () instead of intent template. Find () to traverse the data;

D. pay attention to the data volume when querying data. the result set of find () is too large. For example, if it is 0.1 million 1 million, the data will be accumulated in the memory. This is very dangerous. Try to use the limit method to limit the number of results.

3. Be careful when choosing whether to use mongdb. Some problems are found in our project. If you choose to use MongoDB, you need to consider the server configuration, because MongoDB is infinitely greedy for memory. If you only have 2 GB of memory, you need to run other services, if you deploy MongoDB to process small data volumes, it is not suitable to process large volumes of data. In my opinion, using nosql means that you can spend the minimum amount of money to process more data, and the data security is not very important, such as allowing data loss or being unable to process transactions. If you have high requirements on data security, consider it with caution.

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.