@@ -16,6 +16,7 @@ The database profiler collects detailed information about
16
16
:ref:`database-commands` executed against a running :binary:`~bin.mongod`
17
17
instance. This includes CRUD operations as well as configuration and
18
18
administration commands.
19
+
19
20
The profiler writes all the data it collects to a
20
21
:data:`system.profile <<database>.system.profile>` collection, a
21
22
:doc:`capped collection </core/capped-collections>` in each profiled
@@ -30,8 +31,8 @@ levels <database-profiling-level>`.
30
31
When enabled, profiling has an effect on database performance and
31
32
disk use. See :ref:`Database Profiler Overhead<database-profiling-overhead>` for more information.
32
33
33
- This document outlines a number of key administration options for the
34
- database profiler. For additional related information, see:
34
+ This page shows important administration options for the
35
+ database profiler. For additional information, see:
35
36
36
37
- :doc:`/reference/database-profiler`
37
38
- :doc:`Profile Command </reference/command/profile>`
@@ -56,31 +57,32 @@ Enable and Configure Database Profiling
56
57
57
58
You can enable database profiling for :binary:`~bin.mongod` instances.
58
59
59
- This section uses :binary:`~bin.mongosh` helper
60
- :method:`db.setProfilingLevel()` helper to enable profiling. For
61
- instructions using the driver, see your :driver:`driver
62
- documentation </>`.
60
+ This section shows how you use the :binary:`~bin.mongosh` helper method
61
+ :method:`db.setProfilingLevel()` to enable profiling. To use a driver
62
+ method instead, see the :driver:`driver documentation`.
63
63
64
- When you enable profiling for a :binary:`~bin.mongod` instance, you set
64
+ To enable profiling for a :binary:`~bin.mongod` instance, set
65
65
the :ref:`profiling level <database-profiling-levels>` to a value
66
- greater than 0 . The profiler records data in the :data:`system.profile
66
+ greater than ``0`` . The profiler records data in the :data:`system.profile
67
67
<<database>.system.profile>` collection. MongoDB creates the
68
68
:data:`system.profile <<database>.system.profile>` collection in a
69
69
database after you enable profiling for that database.
70
70
71
71
To enable profiling and set the profiling level, pass the profiling
72
72
level to the :method:`db.setProfilingLevel()` helper. For example, to
73
- enable profiling for all database operations, consider the following
74
- operation in :binary:`~bin.mongosh`:
73
+ enable profiling for all database operations for the currently connected
74
+ database, run this operation in :binary:`~bin.mongosh`:
75
75
76
76
.. code-block:: javascript
77
77
78
78
db.setProfilingLevel(2)
79
79
80
- The shell returns a document showing the *previous* level of profiling.
81
- The ``"ok" : 1`` key-value pair indicates the operation succeeded:
80
+ The shell returns the *previous* profiling level in the ``was`` field
81
+ and sets the new level. In the following output, the ``"ok" :
82
+ 1`` key-value pair indicates the operation succeeded:
82
83
83
84
.. code-block:: javascript
85
+ :copyable: false
84
86
85
87
{ "was" : 0, "slowms" : 100, "sampleRate" : 1.0, "ok" : 1 }
86
88
@@ -97,7 +99,7 @@ The :ref:`slowms <set-profiling-level-options-slowms>` and
97
99
settings are *global*. When set, these settings affect all databases in
98
100
your process.
99
101
100
- When set via the :dbcommand:`profile` command or
102
+ When set through the :dbcommand:`profile` command or
101
103
:method:`db.setProfilingLevel()` shell helper method, :ref:`profiling
102
104
level <database-profiling-level>` and :ref:`filter
103
105
<set-profiling-level-options-filter>` settings are set at the *database*
@@ -111,7 +113,7 @@ Specify the Threshold for Slow Operations
111
113
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112
114
113
115
By default, the slow operation threshold is 100 milliseconds. To change
114
- the slow operation threshold, specify the desired threshold value in
116
+ the slow operation threshold, specify the required threshold value in
115
117
one of the following ways:
116
118
117
119
- Set the value of ``slowms`` using the :dbcommand:`profile` command or
@@ -122,37 +124,38 @@ one of the following ways:
122
124
- Set the value of :setting:`~operationProfiling.slowOpThresholdMs` in a
123
125
:ref:`configuration file <configuration-options>`.
124
126
125
- For example, the following code sets the profiling level for the
126
- current :binary:`~bin.mongod` instance to ``1`` and sets the slow
127
- operation threshold for the :binary:`~bin.mongod` instance to 20
127
+ The following example sets the profiling level for the
128
+ currently connected database to ``1`` and sets the slow
129
+ operation threshold for the :binary:`~bin.mongod` instance to ``20``
128
130
milliseconds:
129
131
130
132
.. code-block:: javascript
131
133
132
- db.setProfilingLevel(1, { slowms: 20 })
134
+ db.setProfilingLevel( 1, { slowms: 20 } )
133
135
134
- Profiling level of ``1`` will profile operations slower than the
135
- threshold.
136
+ A profiling level of ``1`` causes the profiler to record operations
137
+ slower than the ``slowms`` threshold.
136
138
137
139
.. important::
140
+
138
141
The slow operation threshold applies to all databases in a
139
142
:binary:`~bin.mongod` instance. It is used by both the database profiler
140
143
and the diagnostic log and should be set to the highest useful value to
141
144
avoid performance degradation.
142
145
143
- Starting in MongoDB 4.0, you can use :method:`db.setProfilingLevel()`
146
+ You can use :method:`db.setProfilingLevel()`
144
147
to configure ``slowms`` and ``sampleRate`` for :binary:`~bin.mongos`.
145
148
For the :binary:`~bin.mongos`, the ``slowms`` and ``sampleRate``
146
149
configuration settings only affect the diagnostic log and not the
147
150
profiler since profiling is not available on :binary:`~bin.mongos`.
148
151
[#mongos-systemlog]_
149
152
150
- For example, the following sets a :binary:`~bin.mongos` instance's slow
151
- operation threshold for logging slow operations:
153
+ The following example sets a :binary:`~bin.mongos` instance's slow
154
+ operation threshold for logging slow operations to ``20`` :
152
155
153
156
.. code-block:: javascript
154
157
155
- db.setProfilingLevel(0, { slowms: 20 })
158
+ db.setProfilingLevel( 0, { slowms: 20 } )
156
159
157
160
.. include:: /includes/extracts/4.2-changes-log-query-shapes-plan-cache-key.rst
158
161
@@ -161,7 +164,7 @@ operation threshold for logging slow operations:
161
164
Profile a Random Sample of Slow Operations
162
165
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163
166
164
- To profile only a randomly sampled subset of all *slow* operations ,
167
+ To profile only a randomly sampled subset of all *slow* operations,
165
168
specify the desired sample rate in one of the following ways:
166
169
[#slow-oplogs]_
167
170
@@ -176,33 +179,33 @@ specify the desired sample rate in one of the following ways:
176
179
:ref:`configuration file <configuration-options>`.
177
180
178
181
By default, ``sampleRate`` is set to ``1.0``, meaning all *slow*
179
- operations are profiled. When ``sampleRate`` is set between 0 and 1 ,
180
- databases with profiling level ``1`` will only profile a randomly sampled
181
- percentage of *slow* operations according to ``sampleRate``.
182
+ operations are profiled. When ``sampleRate`` is set between ``0`` and ``1`` ,
183
+ databases with a profiling level ``1`` only profile a randomly sampled
184
+ percentage of *slow* operations based on ``sampleRate``.
182
185
183
- For example, the following method sets the profiling level for the
184
- :binary:`~bin.mongod` to ``1`` and sets the profiler to sample 42% of
185
- all *slow* operations:
186
+ The following example sets the profiling level for the currently
187
+ connected database to ``1`` and sets the profiler to sample 42% of all
188
+ *slow* operations:
186
189
187
190
.. code-block:: javascript
188
191
189
- db.setProfilingLevel(1, { sampleRate: 0.42 })
192
+ db.setProfilingLevel( 1, { sampleRate: 0.42 } )
190
193
191
194
The modified sample rate value also applies to the system log.
192
195
193
- Starting in MongoDB 4.0, you can use :method:`db.setProfilingLevel()`
196
+ You can use :method:`db.setProfilingLevel()`
194
197
to configure ``slowms`` and ``sampleRate`` for :binary:`~bin.mongos`.
195
198
For the :binary:`~bin.mongos`, the ``slowms`` and
196
199
``sampleRate`` configuration settings only affect the diagnostic log
197
- and not the profiler since profiling is not available on
200
+ and not the profiler because profiling isn't available on
198
201
:binary:`~bin.mongos`. [#mongos-systemlog]_
199
202
200
203
For example, the following sets a :binary:`~bin.mongos` instance's
201
204
sampling rate for logging slow operations:
202
205
203
206
.. code-block:: javascript
204
207
205
- db.setProfilingLevel(0, { sampleRate: 0.42 })
208
+ db.setProfilingLevel( 0, { sampleRate: 0.42 } )
206
209
207
210
.. important::
208
211
@@ -221,7 +224,7 @@ You can set a filter to control which operations are profiled and
221
224
logged. You can set the profiling filter in one of the following ways:
222
225
223
226
- Set the value of ``filter`` using the :dbcommand:`profile` command
224
- or :method:`db.setProfilingLevel()` shell helper method.
227
+ or the :method:`db.setProfilingLevel()` shell helper method.
225
228
226
229
- Set the value of :setting:`~operationProfiling.filter` in a
227
230
:ref:`configuration file <configuration-options>`.
@@ -240,8 +243,8 @@ available on :binary:`~bin.mongos`.
240
243
<set-profiling-level-options-sampleRate>` options do not affect the
241
244
diagnostic log or the profiler.
242
245
243
- For example, the following :method:`db.setProfilingLevel()` method sets
244
- for a :binary:`~bin.mongod` instance :
246
+ The following :method:`db.setProfilingLevel()` example sets
247
+ the profile level for the currently connected database :
245
248
246
249
- the :ref:`profiling level <set-profiling-level-level>` to ``2``,
247
250
@@ -259,8 +262,8 @@ for a :binary:`~bin.mongod` instance:
259
262
Check Profiling Level
260
263
~~~~~~~~~~~~~~~~~~~~~
261
264
262
- To view the :ref:`profiling level <database-profiling-levels>`, issue
263
- the following from :binary:`~bin.mongosh`:
265
+ To view the :ref:`profiling level <database-profiling-levels>`, run
266
+ the following example in :binary:`~bin.mongosh`:
264
267
265
268
.. code-block:: javascript
266
269
@@ -283,17 +286,23 @@ that should be profiled.
283
286
Disable Profiling
284
287
~~~~~~~~~~~~~~~~~
285
288
286
- To disable profiling, use the following helper in
289
+ To disable profiling, run the following example in
287
290
:binary:`~bin.mongosh`:
288
291
289
292
.. code-block:: javascript
290
293
291
294
db.setProfilingLevel(0)
292
295
296
+ .. note::
297
+
298
+ Disabling profiling can improve database performance and lower disk
299
+ use. For more information, see :ref:`Database Profiler
300
+ Overhead<database-profiling-overhead>` .
301
+
293
302
Enable Profiling for an Entire ``mongod`` Instance
294
303
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
295
304
296
- For development purposes in testing environments, you can enable
305
+ For development and test environments, you can enable
297
306
database profiling for an entire :binary:`~bin.mongod` instance. The
298
307
profiling level applies to all databases provided by the
299
308
:binary:`~bin.mongod` instance.
@@ -313,10 +322,10 @@ This sets the profiling level to ``1``, defines slow operations as those
313
322
that last longer than ``15`` milliseconds, and specifies that only 50%
314
323
of *slow* operations should be profiled. [#slow-oplogs]_
315
324
316
- The ``slowms`` and ``slowOpSampleRate`` also affect which operations
317
- are recorded to the diagnostic log when :parameter:`logLevel` is
318
- set to ``0``. The ``slowms`` and ``slowOpSampleRate`` are also
319
- available to configure diagnostic logging for :binary:`~bin.mongos`. [#slow-oplogs]_
325
+ The ``slowms`` and ``slowOpSampleRate`` also affect the operations that
326
+ are recorded in the diagnostic log when :parameter:`logLevel` is set to
327
+ ``0``. The ``slowms`` and ``slowOpSampleRate`` are also available to
328
+ configure diagnostic logging for :binary:`~bin.mongos`. [#slow-oplogs]_
320
329
321
330
.. seealso::
322
331
@@ -365,8 +374,8 @@ operation, including reads, on the :data:`system.profile
365
374
Example Profiler Data Queries
366
375
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
367
376
368
- This section displays example queries to the :data:`system.profile <<database>.system.profile>`
369
- collection. For an explanation of the query output, see
377
+ This section shows example queries on the :data:`system.profile
378
+ <<database>.system.profile>` collection. For query output details , see
370
379
:doc:`/reference/database-profiler`.
371
380
372
381
To return the most recent 10 log entries in the :data:`system.profile <<database>.system.profile>`
@@ -391,45 +400,44 @@ the following. This example returns operations in the ``mydb`` database's
391
400
392
401
db.system.profile.find( { ns : 'mydb.test' } ).pretty()
393
402
394
- To return operations slower than ``5`` milliseconds, run a query
395
- similar to the following :
403
+ To return operations that take longer than 5 milliseconds to complete,
404
+ run :
396
405
397
406
.. code-block:: javascript
398
407
399
408
db.system.profile.find( { millis : { $gt : 5 } } ).pretty()
400
409
401
- To return information from a certain time range, run a query similar to
402
- the following:
410
+ To return operations for a specific time range, run:
403
411
404
412
.. code-block:: javascript
405
413
406
- db.system.profile.find({
407
- ts : {
408
- $gt: new ISODate("2012-12-09T03:00:00Z"),
409
- $lt: new ISODate("2012-12-09T03:40:00Z")
410
- }
411
- }).pretty()
414
+ db.system.profile.find( {
415
+ ts : {
416
+ $gt: new ISODate("2012-12-09T03:00:00Z"),
417
+ $lt: new ISODate("2012-12-09T03:40:00Z")
418
+ }
419
+ } ).pretty()
412
420
413
421
The following example looks at the time range, suppresses the ``user``
414
422
field from the output to make it easier to read, and sorts the results
415
423
by how long each operation took to run:
416
424
417
425
.. code-block:: javascript
418
426
419
- db.system.profile.find({
420
- ts : {
421
- $gt: new ISODate("2011-07-12T03:00:00Z"),
422
- $lt: new ISODate("2011-07-12T03:40:00Z")
423
- }
424
- }, { user: 0 }).sort( { millis: -1 } )
427
+ db.system.profile.find( {
428
+ ts : {
429
+ $gt: new ISODate("2011-07-12T03:00:00Z"),
430
+ $lt: new ISODate("2011-07-12T03:40:00Z")
431
+ }
432
+ }, { user: 0 } ).sort( { millis: -1 } )
425
433
426
434
Show the Five Most Recent Events
427
435
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
428
436
429
437
On a database that has profiling enabled, the ``show profile`` helper
430
438
in :binary:`~bin.mongosh` displays the 5 most recent operations
431
- that took at least 1 millisecond to execute. Issue ``show profile``
432
- from :binary:`~bin.mongosh`, as follows :
439
+ that took at least 1 millisecond to execute. Run ``show profile``
440
+ from :binary:`~bin.mongosh`:
433
441
434
442
.. code-block:: javascript
435
443
@@ -444,11 +452,16 @@ When enabled, profiling has an effect on database performance,
444
452
especially when configured with a
445
453
:ref:`profiling level<database-profiling-level>` of 2, or when using a
446
454
low :ref:`threshold<database-profiling-specify-slowms-threshold>` value
447
- with a profiling level of 1. Profiling also consumes disk space, as it
448
- logs to both the :data:`system.profile <<database>.system.profile>`
449
- collection and also the MongoDB :option:`logfile <mongod --logpath>`.
450
- Carefully consider any performance and security implications before
451
- configuring and enabling the profiler on a production deployment.
455
+ with a profiling level of 1.
456
+
457
+ Profiling also uses disk space, because profiling
458
+ writes logs to the :data:`system.profile <<database>.system.profile>`
459
+ collection and the MongoDB :option:`logfile <mongod --logpath>`.
460
+
461
+ .. warning::
462
+
463
+ Consider performance and storage implications before
464
+ you enable the profiler in a production deployment.
452
465
453
466
The ``system.profile`` Collection
454
467
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments