In MySQL, slow query logs are often used as the basis for database optimization. Does MongoDB provide similar functions? The answer is yes, that is, MongoDB database profiler. Therefore, MongoDB not only has more detailed information than MySQL's slow query log.
Enable profiling
There are two ways to control the profiling switch and level. The first is to directly set it in the startup parameters. Add-profile = to start MongoDB. You can also call the DB. setprofilinglevel (level) command on the client for real-time configuration. The profiler information is stored in system. profile. You can use the DB. getprofilinglevel () command to obtain the current profile level. perform the following operations:
DB. setprofilinglevel (2 );
The profile level can be 0, 1, or 2. Their meanings are as follows:
0-Disable
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:
DB. setprofilinglevel (1, 10 );
Query profiling records
Unlike the slow query log of MySQL, The MongoDB profile record exists directly in the system dB, and the record location is system. profile, so we only need to query the records of this collection to get our profile records. List profile records with execution time longer than a certain limit (5 ms:
DB. system. profile. Find ({millis :{$ GT: 5 }})
View the latest profile record:
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 :{}, orderby :{$ natural:-1.0 }}nreturned: 8 Bytes: 1203 "," millis ": 0}
Field description
TS: When to execute the command info: detailed information of this command reslen: returned result set size nscanned: number of records scanned for this query nreturned: actually returned result set for this query
Millis: the execution time of this command, in milliseconds
MongoDB shell also provides a simple command "show profile" to list the last five execution times that have exceeded
1 ms profile record.