Mongo database performance optimization

Source: Internet
Author: User
Tags mongo shell

SQL Server has tools for database optimization. Mongo database profiler not only has but also has more powerful functions.

MongoDB comes with a profiler that allows you to easily record all operations that take a long time to facilitate optimization. There are two ways to control the profiling switch and level. The first is to directly set it in the startup parameters.

When MongoDB is started, add-profile =LevelYou can.

You can also call dB. setprofilinglevel (Level) Command to configure in real time. You can use the DB. getprofilinglevel () command to obtain the current profile level.

1

2

3

> DB. setprofilinglevel (2 );

{"Was": 0, "OK": 1}

> DB. getprofilinglevel ()

ItalicsLevelWe can take values 0, 1, and 2. Their meanings are as follows:

0-Disable. Disable performance analysis. Enable the test environment and disable the generation environment, which has a great impact on the performance.

1-record slow commands (default:> 100 ms)

2-record all commands

Profile records slow commands at level 1. What is the definition of this slow command? As mentioned above, the default value is 100 ms. Of course, the default value is ms. The setting method and level are the same. One is to add the-slowms startup parameter configuration. The second parameter is added when dB. setprofilinglevel is called:

1

2

DB. setprofilinglevel (Level, slowms)

DB. setprofilinglevel (1, 10 );

The profiler information is stored in system. Profile (capped collection. You can also use this tool to set and view data: A Powerful MongoDB database management tool

Mongo shell also provides a simple command "show profile" to list the last five profile records that have been executed for more than 1 ms.

View the analysis data of all sets in the current database

DB. system. profile. Find ()
View the analysis data of a set

DB. system. profile. Find ({info:/user.info /})
View the execution operations that take more than 100 milliseconds, sort them in reverse order, and take the first 5 rows

DB. system. profile. Find ({millis: {$ GT: 100}). Sort ({$ natural:-1}). Limit (5 );

Profile details:

Ts-when to execute the command.

Millis time-the execution time of this command, in milliseconds.

Info-detailed information about this command.

Query-indicates that this is a query operation.

Ntoreturn-the number of records returned by the query client. For example, when the findone () command is executed, ntoreturn is 1. When the limit (n) condition exists, ntoreturn is N.

Query-specific query conditions (for example, x> 3 ).

Nscanned-number of records scanned for this query.

Reslen-size of the returned result set.

Nreturned-the actual returned result set for this query.

Update-indicates that this is an update operation.

Fastmod-indicates a fast modify operation. See updates. These operations are normally quite fast.

Fastmodinsert-indicates a fast modify operation that runs med an upsert.

Upsert-indicates that the upsert parameter of update is true. If the update record does not exist, insert a record using the update condition.

Moved-indicates whether the data on the hard disk is moved in this update. If the new record is shorter than the original record, the current record is usually not moved. If the new record is longer than the original record, therefore, records may be moved to other locations, which may lead to updates of related indexes. more disk operations and index updates will lead to slow operations.

Insert-this is an insert operation.
Getmore-this is a getmore operation. getmore usually returns partial results for the first query when the result set is large. The subsequent results are obtained through getmore.

2. Optimization

MongoDBQuery Optimization

If nscanned (number of scanned records) is much larger than nreturned (number of records returned), we should consider adding indexes to optimize record location.

If reslen is too large, it indicates that the returned result set is too large. In this case, check whether the second parameter of the find function only contains the attribute name you need. (Similar to MySQL, do not always select *)

It is recommended that you do not add an index if you do not have to read the index. Because the more indexes, the slower the write operation. If the read volume is large, it is more cost-effective to create an index.

MongoDBUpdate Optimization

If the query write volume or update volume is too large, it is advantageous to add more indexes. And ~~~~ (N words are omitted, which is similar to RDBMS)

Use fast modify operations when possible (and usually with these, an index). See updates.

ProfilerEfficiency

The profiling function will definitely affect efficiency, but it is not very serious because it uses system. profile, and system. profile is a capped collection, which has some restrictions and features in operation, but is more efficient.

Optimization suggestions:

    • If nscanned is much larger than nreturned, you need to use an index.
    • If reslen returns a very large byte, consider obtaining only the required fields.
    • when performing the update operation, check nscanned and use the index to reduce the number of document scans.
    • Use dB. eval () to perform certain statistical operations on the server.
    • reduce the number of returned documents and use the Skip & limit tab.

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.