Skip to content

Commit 57ba6e0

Browse files
authored
DOCSP-50588 Find Slow Queries with currentOp (#12445)
1 parent 1a4a46e commit 57ba6e0

File tree

6 files changed

+184
-7
lines changed

6 files changed

+184
-7
lines changed

content/manual/upcoming/source/administration/query.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Troubleshooting
5858

5959
- :ref:`manual-find-slow-queries-with-database-profiler`
6060

61+
- :ref:`manual-monitor-slow-queries`
62+
6163
- :ref:`manual-explain-slow-queries`
6264

6365
Solutions
@@ -122,8 +124,34 @@ into how you can improve query performance, such as:
122124

123125
- Whether the query uses an index
124126

127+
125128
For more information, see :ref:`profiler`.
126129

130+
.. note::
131+
132+
The database profiler is not available on :program:`mongos`.
133+
If you need to find slow queries on a sharded cluster, see
134+
:ref:`manual-qp-currentOp`.
135+
136+
.. _manual-qp-currentOp:
137+
138+
Monitor Current Operations
139+
~~~~~~~~~~~~~~~~~~~~~~~~~~
140+
141+
The :pipeline:`$currentOp` aggregation stage returns information
142+
on operations currently running on a MongoDB server or cluster.
143+
``$currentOp`` can help you identify performance issues, such as:
144+
145+
- How long the query takes to run
146+
147+
- Whether the query is waiting on a lock
148+
149+
- Whether the query uses an index
150+
151+
For more information, see :ref:`manual-monitor-slow-queries`.
152+
153+
.. _manual-qp-explain:
154+
127155
Explain
128156
~~~~~~~
129157

content/manual/upcoming/source/includes/fact-profiler-use.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
This task runs the :method:`~db.collection.explain` method on a
2-
sample query in an attempt to identify performance issues. In
3-
practice, it may be difficult to run ``explain()`` on every
4-
query your application runs.
5-
6-
To narrow the list of queries sent by your application to only
7-
those that are slow, you can use a profiler:
81

92
.. list-table::
103
:header-rows: 1

content/manual/upcoming/source/tutorial/evaluate-operation-performance.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,5 @@ queries, and can provide fine-grained performance insights:
174174

175175
Explain Results </reference/explain-results>
176176
Database Profiler </tutorial/manage-the-database-profiler>
177+
Monitor Slow Queries </tutorial/monitor-slow-queries>
177178
Block Slow Queries </tutorial/operation-rejection-filters>

content/manual/upcoming/source/tutorial/explain-slow-queries.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ queries that use the following methods:
3232
Profilers
3333
~~~~~~~~~
3434

35+
This task runs the :method:`~db.collection.explain` method on a
36+
sample query in an attempt to identify performance issues. In
37+
practice, it may be difficult to run ``explain()`` on every
38+
query your application runs.
39+
40+
To narrow the list of queries to analyze to only those that are
41+
slow, you can use a profiler:
42+
3543
.. include:: /includes/fact-profiler-use
3644

3745
Steps

content/manual/upcoming/source/tutorial/find-slow-queries-with-database-profiler.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ Steps
156156
- If ``keysExamined`` is high and ``docsExamined`` is low,
157157
it indicates effective index use.
158158

159+
.. step:: Explain the query.
160+
161+
If the database profiler identifies a query that requires
162+
further investigation, use the
163+
:method:`~db.collection.explain` method to analyze the
164+
query plan and execution statistics.
165+
166+
For details, see :ref:`manual-explain-slow-queries`.
167+
159168
.. step:: Disable the database profiler.
160169

161170
To ensure that the database profiler does not further
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
.. meta::
2+
:description: Manage the database profiler to collect detailed information on database commands, affecting performance and disk use when enabled.
3+
4+
.. _manual-monitor-slow-queries:
5+
6+
====================
7+
Monitor Slow Queries
8+
====================
9+
10+
.. contents:: On this page
11+
:local:
12+
:backlinks: none
13+
:depth: 1
14+
:class: singlecol
15+
16+
The :pipeline:`$currentOp` aggregation stage provides
17+
information on all operations currently running on MongoDB. If
18+
your application is experiencing performance issues, you can
19+
build an aggregation pipeline around this stage to monitor for
20+
slow queries and similar issues.
21+
22+
About This Task
23+
---------------
24+
25+
Database Profilers
26+
~~~~~~~~~~~~~~~~~~
27+
28+
This task uses :pipeline:`$currentOp` to identify slow queries
29+
currently running on your application. To find all slow queries
30+
within a specified period, consider using a profiler.
31+
32+
.. include:: /includes/fact-profiler-use
33+
34+
Explain Queries
35+
~~~~~~~~~~~~~~~
36+
37+
This task identifies queries with performance issues. If you
38+
already know which queries have performance issues, see
39+
:ref:`manual-explain-slow-queries` to troubleshoot them.
40+
41+
Steps
42+
-----
43+
44+
.. procedure::
45+
:style: normal
46+
47+
.. step:: Retrieve current operations.
48+
49+
Use the :pipeline:`$currentOp` aggregation stage to
50+
retrieve current operations from MongoDB:
51+
52+
.. io-code-block::
53+
54+
.. input::
55+
:language: javascript
56+
57+
db.getSiblingDB("admin").aggregate( [
58+
{ $currentOp: { allUsers: true } },
59+
{ $match: { secs_running: { $gt: 2 } } },
60+
{ $sort: { secs_running: 1 } }
61+
] )
62+
63+
.. output::
64+
:language: javascript
65+
66+
[
67+
{
68+
"opid": "12345",
69+
"secs_running": 5,
70+
"active": true,
71+
"ns": "sample_mflix.movies",
72+
"command": {
73+
"find": "movies",
74+
"filter": { "title": { "$regex": "The" } }
75+
},
76+
"planSummary": "COLLSCAN",
77+
"locks": { ... },
78+
"client": "203.0.113.25:43210"
79+
}
80+
]
81+
82+
This aggregation pipeline retrieves all current operations
83+
in the cluster. The :pipeline:`$match` aggregation stage
84+
then filters the operations to those that have been
85+
running for more than two seconds. This allows you to
86+
filter queries that run within a specified period. Adjust
87+
the value to match your application and database needs.
88+
89+
The :pipeline:`$sort` stage sorts the results in
90+
ascending operation time order.
91+
92+
.. step:: Check for activity.
93+
94+
View the :data:`currentOp.active` field. If ``currentOp.active`` is
95+
``true``, MongoDB indicates that the operation is currently
96+
running.
97+
98+
To stop a long running operation, use the
99+
:method:`db.killOp` method to stop the given
100+
:data:`~currentOp.opid`.
101+
102+
.. step:: Check for locks.
103+
104+
View the :data:`currentOp.waitingForLock` field. If ``currentOp.waitingForLock`` is
105+
``true``, another operation running on the server or cluster is blocking the query.
106+
107+
108+
To stop a blocked operation, use the :method:`db.killOp`
109+
method to stop the given :data:`~currentOp.opid`.
110+
111+
.. step:: Check the plan summary.
112+
113+
Check the value in the :data:`currentOp.planSummary`
114+
field.
115+
116+
``IXSCAN``
117+
Indicates the query performed an index scan.
118+
119+
``COLLSCAN``
120+
Indicates the query performed a full collection scan.
121+
To correct this, :ref:`manual-create-an-index`.
122+
123+
.. step:: Explain the query.
124+
125+
If the :pipeline:`$currentOp` aggregation stage returns a
126+
query that requires further investigation, use the
127+
:method:`~db.collection.explain` method to analyze the
128+
query plan and execution statistics.
129+
130+
For details, see :ref:`manual-explain-slow-queries`.
131+
132+
Learn More
133+
----------
134+
135+
- :ref:`query-performance`
136+
- :ref:`profiler`
137+
- :ref:`manual-find-slow-queries-with-database-profiler`
138+
- :ref:`manual-explain-slow-queries`

0 commit comments

Comments
 (0)