In MySQL, the slow query log is often used as the basis for us to optimize the database, is there a similar function in MongoDB? The answer is yes, that's the MongoDB database Profiler. So MongoDB not only has, but also has some more detailed information than the MySQL slow Query log.
turn on the Profiling function
There are two ways to control the Profiling switch and level, the first is directly in the startup parameters directly set. When you start MongoDB, add the –profile= level. You can also invoke the Db.setprofilinglevel (level) command on the client to configure it in real time, and the Profiler information is stored in system.profile. We can get the current profile level through the Db.getprofilinglevel () command, like this:
Db.setprofilinglevel (2);
The above profile level can take 0,1,2 three values, they say the meaning is as follows:
0– not open
--Record slow command (default = >100ms)
--Record all orders
The profile record will record the slow command at Level 1 o'clock, so what is this slow definition? Above we say that the default is the 100ms, of course there are settings, there are two ways to set the method and level, one is to add the –slowms boot parameter configuration. The second is to add the second parameter when calling Db.setprofilinglevel:
Db.setprofilinglevel (1, 10);
Query Profiling Records
Unlike MySQL's slow query log, MongoDB profile records are directly present in the system DB, recording location system.profile, so we can only query this Collection record to get our profile record. List the profile records that have an execution time longer than a certain limit (5ms):
Db.system.profile.find ({millis: {$gt: 5}})
View the latest profile records:
Db.system.profile.find (). Sort ({$natural: -1}). Limit (1)
{"TS": Isodate ("2012-05-20t16:50:36.321z"), "info": "Query Test.system.profile reslen:1219
Nscanned:8 \nquery: {query: {}, by: {$natural: -1.0}} nreturned:8 bytes:1203 "," Millis ": 0}
Field description
TS: When the command executes info: The details of this command reslen: Returns the size of the result set nscanned: The number of records scanned by this query nreturned: The result set actually returned by this query Millis: This command executes time-consuming, in milliseconds
MongoDB Shell also provides a relatively concise command of show profile, which lists the last 5 execution times over
Profile record for 1ms.
Mongodb profile (slow query log)