-- ===========================
--MongoDB Slow query log
-- ===========================
reference:http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/
Profiling levels:
1 Resume
0-the Profiler is off, does does not collect any data.
1-collects profiling data for slow operations only. By default slow operations are those slower than.
You can modify the ' threshold for ' slow ' operations with the SLOWMS option or the runtime command. The Specify the Threshold for slow Operations section for more information.
2-collects profiling data for the all database operations.
2 able
2.1
Db.setprofilinglevel (1,500);
Db.setprofilinglevel (2,500);
Primary> Db.setprofilinglevel (1,500);
{"was": 0, "slowms": +, "OK": 1}
Primary>
Primary>
Primary>
Primary> Db.setprofilinglevel (1,500);
{"was": 1, "slowms": +, "OK": 1}
Primary>
Primary>
Primary>
Question: Why does it take set2 time to take effect.
0 means close, 1 represents record all operations slowlog,2, and here we set to 500, that is 500ms
2.2 Check the status
Primary> Db.getprofilingstatus ();
{"was": 2, "SLOWMS": 500}
Primary> Db.getprofilinglevel ();
Primary>
2.3 The last Log info
Db.system.profile.find (). Sort ({$natural:-1})
Primary> Db.system.profile.find (). Sort ({$natural:-1})
{"TS": Isodate ("2013-05-14t08:13:37.098z"), "OP": "Insert", "ns": "Test.tickets", "Millis": 0, "client": "127.0.0.1" , "User": ""}
{"TS": Isodate ("2013-05-14t08:13:37.098z"), "OP": "Insert", "ns": "Test.tickets", "Millis": 0, "client": "127.0.0.1" , "User": ""}
{"TS": Isodate ("2013-05-14t08:13:37.098z"), "OP": "Insert", "ns": "Test.tickets", "Millis": 0, "client": "127.0.0.1" , "User": ""}
Primary>
TS: Time stamp
OP: Action type
NS: Collection of objects performing operations
Millis: The time taken by the operation, milliseconds
Client: Clients performing the operation
User: MongoDB connected users performing an action
2.4 Can be set at the beginning of the MongoDB slow effective
Add directly to the start command: Mongod--profile=1--slowms=15
or write in the configuration file with the-F forced Load configuration file start MongoDB
Profile=1
slowms=1000
[] In the replicaset, it is necessary to put each instance into effect. Each one needs to be in effect once.
3 The Log
Db.system.profile.find (). Limit. Sort ({ts:-1}). Pretty ();
Db.system.profile.find (). Limit. Sort ({ts:1}). Pretty ();
4 Disable Profiling
To disable profiling, use the following helper in the MONGO Shell:set the default value 0;
Db.setprofilinglevel (0)
5 Adjust table size to 4MB when profile table is over hour
Db.setprofilinglevel (0)--profile failure
Db.system.profile.drop ()--delete
Db.createcollection ("System.profile", {capped:true, size:4000000})--rebuilding
Db.setprofilinglevel (1)--profile effective
6 Slow MONGO command to view out
6.1 shows the latest 5 operations record
Show profile;
6.2 Display Result analysis, query for slow command greater than 5 milliseconds.
Db.system.profile.find (). Sort ({millis:-1}). Limit (10);
Db.system.profile.find ({millis: {$gt: 5}}). Pretty ();
{
"TS": Isodate ("2013-01-16t18:26:18.041z"),
"OP": "Query"--execution type
"NS": "Ssodatabase.exchangetickets",--executing collection name
"Query": {
"XID": "x15e1481688254bc9a94701b3aa9e7abc627971358360754783"
},--what is executed
"Ntoreturn": 1,
"nscanned": 382793,
"Nreturned": 1,
"ResponseLength": 605,
"Millis": 5841,--time of execution
"Client": "10.100.10.161",--Clients executed
' User ': ' '--MONGO user executed
}
Observe the "Query" field in the result. No direct Db.test.insert ({xxxxxxx ...}) This shows that you need to use the query field to splice values.
7 to return a slow query record at a specific time
7.1 Ordinary Time short query
Db.system.profile.find (
{
TS: {
$GT: New Isodate ("2013-05-09t03:00:00z"),
$LT: New Isodate ("2013-05-17t03:40:00z")
}
}
). Pretty ();
run the command, follows:
Primary> Db.system.profile.find (
... {
... ts: {
$GT: New Isodate ("2013-05-09t03:00:00z"),
$LT: New Isodate ("2013-05-17t03:40:00z")
... }
... }
... ). Pretty ();
{
"TS": Isodate ("2013-05-14t08:36:58.691z"),
"OP": "Query",
"NS": "Ssodatabase.digitalriverorderdetails",
"Query": {
"Invoiceid": "15539232823"
},
"Ntoreturn": 1,
"Nscanned": 1,
"Nreturned": 1,
"ResponseLength": 1213,
"Millis": 663,
"Client": "10.100.10.162",
"User": "admin"
}
{
"TS": Isodate ("2013-05-14t09:17:58.911z"),
"OP": "Insert",
"NS": "Ssodatabase.tickets",
"Millis": 527,
"Client": "10.100.10.154",
"User": "admin"
}
{
"TS": Isodate ("2013-05-14t09:20:58.648z"),
"OP": "Insert",
"NS": "Ssodatabase.tickets",
"Millis": 529,
"Client": "10.100.10.153",
"User": "admin"
}
......
7.2 With execution time in reverse order, and only output user information
Db.system.profile.find (
{
TS: {
$GT: New Isodate ("2013-05-09t03:00:00z"),
$LT: New Isodate ("2013-05-17t09:40:00z")
}
},
{user:1}--Output only user information
). Sort ({millis:-1})--Reverse order
Primary> Db.system.profile.find (
... {
... ts: {
$GT: New Isodate ("2013-05-09t03:00:00z"),
$LT: New Isodate ("2013-05-17t09:40:00z")
... }
... },
... {user:1}
... ). Sort ({millis:-1})
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
{' user ': ' admin '}
7.3 with execution time in reverse order, and only output user information
Db.system.profile.find (
{
TS: {
$GT: New Isodate ("2013-05-09t03:00:00z"),
$LT: New Isodate ("2013-05-17t09:40:00z")
}
}
). Sort ({millis:-1})--Reverse order