代码改变世界

MongoDB Database Profiler

  abce  阅读(593)  评论(0编辑  收藏  举报

数据库profiler细粒度收集mongodb的写操作、游标、数据库命令等。可以在数据库级别或者实例级别开启profiling。

profiler将收集到的数据写入system.profile集合中。

 

profiling的级别:

0, 关闭profile,但是mongod会将超过slowOpThresholdMs的值操作写入日志

1, 只抓取slow操作,缺省是超过100毫秒的操作

2, 抓取所有数据库操作

 

 

1.查看是否开启profile功能:

1
2
3
> db.getProfilingLevel()
0
>

 

2.关闭profile功能:

1
> db.setProfilingLevel(0)

 

3.设置profile级别:

1
2
3
> db.setProfilingLevel(1,50)
{ "was" : 0, "slowms" : 100, "ok" : 1 }
>

setProfilingLevel的第一个参数是设置当前数据的profiling级别,第二个参数是设置整个mongod实例的慢操作阈值。

 

4.设置整个mongod的profiling级别

1
2
#启动时,指定参数profile的值
./mongod --profile=1 --slowms=15

 

示例:

查看profiler数据
数据库的profiler日志信息位于system.profile集合中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
> db.setProfilingLevel(1,50)
{ "was" : 1, "slowms" : 50, "ok" : 1 }
> db.profilingTest.insert({i:1})
WriteResult({ "nInserted" : 1 })
> db.profilingTest.find()
{ "_id" : ObjectId("56e3f62673c59411c4b1234c"), "i" : 1 }
> db.profilingTest.find({$where:'sleep(70)'})
> db.system.profile.find().pretty()
{
        "op" : "insert",
        "ns" : "test.currentOpTest",
        "query" : {
                "insert" : "currentOpTest",
                "documents" : [
                        {
                                "_id" : ObjectId("56e3ebb713b6892d02827dde"),
                                "i" : 6411413
                        }
                ],
                "ordered" : true
        },
        "ninserted" : 1,
        "keyUpdates" : 0,
        "writeConflicts" : 0,
        "numYield" : 0,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(1),
                                "w" : NumberLong(1)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                }
        },
        "responseLength" : 25,
        "protocol" : "op_command",
        "millis" : 50,
        "execStats" : {
 
        },
        "ts" : ISODate("2016-03-12T10:13:11.073Z"),
        "client" : "127.0.0.1",
        "allUsers" : [ ],
        "user" : ""
}
{
        "op" : "insert",
        "ns" : "test.currentOpTest",
        "query" : {
                "insert" : "currentOpTest",
                "documents" : [
                        {
                                "_id" : ObjectId("56e3ebb713b6892d02827de6"),
                                "i" : 6411421
                        }
                ],
                "ordered" : true
        },
        "ninserted" : 1,
        "keyUpdates" : 0,
        "writeConflicts" : 0,
        "numYield" : 0,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(1),
                                "w" : NumberLong(1)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                }
        },
        "responseLength" : 25,
        "protocol" : "op_command",
        "millis" : 166,
        "execStats" : {
 
        },
        "ts" : ISODate("2016-03-12T10:13:11.252Z"),
        "client" : "127.0.0.1",
        "allUsers" : [ ],
        "user" : ""
}
{
        "op" : "insert",
        "ns" : "test.currentOpTest",
        "query" : {
                "insert" : "currentOpTest",
                "documents" : [
                        {
                                "_id" : ObjectId("56e3ed7e13b6892d028f10a4"),
                                "i" : 7235419
                        }
                ],
                "ordered" : true
        },
        "ninserted" : 1,
        "keyUpdates" : 0,
        "writeConflicts" : 0,
        "numYield" : 0,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(1),
                                "w" : NumberLong(1)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                }
        },
        "responseLength" : 25,
        "protocol" : "op_command",
        "millis" : 58,
        "execStats" : {
 
        },
        "ts" : ISODate("2016-03-12T10:20:46.223Z"),
        "client" : "127.0.0.1",
        "allUsers" : [ ],
        "user" : ""
}
{
        "op" : "insert",
        "ns" : "test.currentOpTest",
        "query" : {
                "insert" : "currentOpTest",
                "documents" : [
                        {
                                "_id" : ObjectId("56e3eea513b6892d02975bee"),
                                "i" : 7778981
                        }
                ],
                "ordered" : true
        },
        "ninserted" : 1,
        "keyUpdates" : 0,
        "writeConflicts" : 0,
        "numYield" : 0,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(1),
                                "w" : NumberLong(1)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                }
        },
        "responseLength" : 25,
        "protocol" : "op_command",
        "millis" : 149,
        "execStats" : {
 
        },
        "ts" : ISODate("2016-03-12T10:25:41.989Z"),
        "client" : "127.0.0.1",
        "allUsers" : [ ],
        "user" : ""
}
{
        "op" : "insert",
        "ns" : "test.currentOpTest",
        "query" : {
                "insert" : "currentOpTest",
                "documents" : [
                        {
                                "_id" : ObjectId("56e3f2bf13b6892d02b4401b"),
                                "i" : 9672402
                        }
                ],
                "ordered" : true
        },
        "ninserted" : 1,
        "keyUpdates" : 0,
        "writeConflicts" : 0,
        "numYield" : 0,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(1),
                                "w" : NumberLong(1)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "w" : NumberLong(1)
                        }
                }
        },
        "responseLength" : 25,
        "protocol" : "op_command",
        "millis" : 143,
        "execStats" : {
 
        },
        "ts" : ISODate("2016-03-12T10:43:11.201Z"),
        "client" : "127.0.0.1",
        "allUsers" : [ ],
        "user" : ""
}
{
        "op" : "insert",
        "ns" : "test.profilingTest",
        "query" : {
                "insert" : "profilingTest",
                "documents" : [
                        {
                                "_id" : ObjectId("56e3f62673c59411c4b1234c"),
                                "i" : 1
                        }
                ],
                "ordered" : true
        },
        "ninserted" : 1,
        "keyUpdates" : 0,
        "writeConflicts" : 0,
        "numYield" : 0,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(2),
                                "w" : NumberLong(2)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "w" : NumberLong(1),
                                "W" : NumberLong(1)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "w" : NumberLong(1),
                                "W" : NumberLong(1)
                        }
                }
        },
        "responseLength" : 25,
        "protocol" : "op_command",
        "millis" : 88,
        "execStats" : {
 
        },
        "ts" : ISODate("2016-03-12T10:57:42.769Z"),
        "client" : "127.0.0.1",
        "allUsers" : [ ],
        "user" : ""
}
{
        "op" : "query",
        "ns" : "test.profilingTest",
        "query" : {
                "find" : "profilingTest",
                "filter" : {
 
                }
        },
        "keysExamined" : 0,
        "docsExamined" : 1,
        "cursorExhausted" : true,
        "keyUpdates" : 0,
        "writeConflicts" : 0,
        "numYield" : 0,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(2)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "r" : NumberLong(1)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "r" : NumberLong(1)
                        }
                }
        },
        "nreturned" : 1,
        "responseLength" : 145,
        "protocol" : "op_command",
        "millis" : 94,
        "execStats" : {
                "stage" : "COLLSCAN",
                "filter" : {
                        "$and" : [ ]
                },
                "nReturned" : 1,
                "executionTimeMillisEstimate" : 0,
                "works" : 3,
                "advanced" : 1,
                "needTime" : 1,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "direction" : "forward",
                "docsExamined" : 1
        },
        "ts" : ISODate("2016-03-12T10:57:55.996Z"),
        "client" : "127.0.0.1",
        "allUsers" : [ ],
        "user" : ""
}
{
        "op" : "query",
        "ns" : "test.profilingTest",
        "query" : {
                "find" : "profilingTest",
                "filter" : {
                        "$where" : "sleep(70)"
                }
        },
        "keysExamined" : 0,
        "docsExamined" : 1,
        "cursorExhausted" : true,
        "keyUpdates" : 0,
        "writeConflicts" : 0,
        "numYield" : 1,
        "locks" : {
                "Global" : {
                        "acquireCount" : {
                                "r" : NumberLong(8)
                        }
                },
                "Database" : {
                        "acquireCount" : {
                                "r" : NumberLong(4)
                        }
                },
                "Collection" : {
                        "acquireCount" : {
                                "r" : NumberLong(4)
                        }
                }
        },
        "nreturned" : 0,
        "responseLength" : 109,
        "protocol" : "op_command",
        "millis" : 1030,
        "execStats" : {
                "stage" : "COLLSCAN",
                "filter" : {
                        "$where" : undefined
                },
                "nReturned" : 0,
                "executionTimeMillisEstimate" : 70,
                "works" : 3,
                "advanced" : 0,
                "needTime" : 2,
                "needYield" : 0,
                "saveState" : 1,
                "restoreState" : 1,
                "isEOF" : 1,
                "invalidates" : 0,
                "direction" : "forward",
                "docsExamined" : 1
        },
        "ts" : ISODate("2016-03-12T10:58:07.439Z"),
        "client" : "127.0.0.1",
        "allUsers" : [ ],
        "user" : ""
}
>

 

5.修改system.profile集合的大小

1
2
3
4
5
6
7
8
#1.关闭profiling
> db.setProfilingLevel(0)
#2.删除system.profile集合
> db.system.profile.drop()
#3.创建新得system.profile
> db.createCollection( "system.profile", { capped: true, size:4000000 } )
#4.重新开启profiling功能
> db.setProfilingLevel(1)

 

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示