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
[HTML]View Plaincopy
- <pre name="code" class="html">> Db.students.find ({' Comment.aihao ': ' Reading '}). Limit (1)
- {"_id": ObjectId ("5485c80bdf41c6670aa8d51c"), "name": "Albert", "Age": "comment": {"Aihao": ["basket", "Readin G "]," relatives ": [" Parent "," Brother "]}}
- > Db.system.profile.find (). Sort ({$natural: -1}). Limit (1)
- { "OP" : "Query", "ns" : "test.system.indexes", "Query" : { "Expireafterseconds" : { "$exists" : true } }, "Ntoreturn" &NBSP;: 0, "Ntoskip" : 0, "nscanned" : 7, "Nscannedobjects" : 7, "Keyupdates" : 0, "Numyield" : 0, "Lockstats" : { " Timelockedmicros " : { " R " : numberlong (159), " W " : numberlong (0) }, "Timeacquiringmicros" : { "R" : numberlong (4), "W" : Numberlong (6) } }, "nreturned" : 0, "ResponseLength" : 20, " Millis " : 0, " execstats " : { " type " : " Collscan ", " Works "&NBSP;: 9, "yields" : 0, "Unyields" : 0, "invalidates" : 0, " Advanced " : 0, " Needtime "&Nbsp;: 8, "Needfetch" : 0, "IsEOF" : 1, "docstested" : 7, "Children" : [ ] }, "ts" : isodate ("2014-12-08t15:54:47.377z"), "Client" : "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)
"MongoDB" MongoDB Optimizer Profiler