"MongoDB" MongoDB Optimizer Profiler

Source: Internet
Author: User
Tags mongodb profiler mysql slow query log

In the MySQL database, the slow query log is often used as the basis for database optimization, and MongoDB still has similar functions. MongoDB's own profiler allows easy recording of all time-consuming operations for ease of tuning;

First, start the profiler function

There are two ways to turn on the Profier function:

The first is to set it directly in the startup parameters, and add the-profile= level to the winter MongoDB.

The second is to execute the "Db.setprofilinglevel (level)" command on the client.

The profiler information is stored in the System.profile table and can be obtained by executing the "db.getprofilinglevel ()" command to obtain the current profiler level:




As you can see, level has a total of three parameters, 0 is off, 1 is slow query, 2 is all. A setting of 2 means that all statements are recorded in log. The default time for slow queries is 100ms, which can also be set by parameter slowsms.

Second, query the profiler record

The MySQL slow query log is stored on disk, while the MongoDB Profiler record directly exists in the system's db. Recorded in the System.profile. Profile is the set of capped collection previously spoken. So as long as you query this collection record can get the profiler record log, you can use the Db.system.profile.find () command directly find the execution time is greater than a certain limit (such as 5ms) of the profiler log;


Turn on set to 100ms

<pre name= "code" class= "HTML" >> db.students.find ({' Comment.aihao ': ' reading '}). Limit (1) {"_id": ObjectId (" 5485c80bdf41c6670aa8d51c ")," name ":" Albert "," Age ":" comment ": {" Aihao ": [" basket "," reading "]," relatives ": ["Parent", "Brother"]} }> Db.system.profile.find (). Sort ({$natural: -1}). Limit (1) {"OP": "Query", "ns": "Test.system.indexes", "query": {"E Xpireafterseconds ": {" $exists ": true}," Ntoreturn ": 0," Ntoskip ": 0," nscanned ": 7," nscannedobjects ": 7," keyUp Dates ": 0," Numyield ": 0," Lockstats ": {" Timelockedmicros ": {" R ": Numberlong (159)," W ": Numberlong (0)}," Timeacqu Iringmicros ": {" R ": Numberlong (4)," W ": Numberlong (6)}}," nreturned ": 0," responselength ":," Millis ": 0," exec  Stats ": {" type ":" Collscan "," Works ": 9," yields ": 0," Unyields ": 0," invalidates ": 0," advanced ": 0," Needtime ": 8, "Needfetch": 0, "IsEOF": 1, "docstested": 7, "Children": []}, "ts": Isodate ("2014-12-08t15:54:47.377z"), "Clien T ":"0.0.0.0 "," allUsers ": [{" User ":" __system "," db ":" Local "}]," User ":" [email protected] "} 

By executing the above statement, you can see that the detailed query information is recorded in the System.profile. Main Field Description:

When the 1:ts command executes

Details of the 2:info command

3:reslen: Returns the size of the result set

4:nscanned: Number of records scanned for this query

5:nreturned: The result set actually returned by this query

6:mills: The execution time of this command (in milliseconds)


You can use commands to query for values for a specific condition: For example: Query execution time greater than 4MS query statement:





"MongoDB" MongoDB Optimizer Profiler

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.