|
| 1 | +.. meta:: |
| 2 | + :description: Manage the database profiler to collect detailed information on database commands, affecting performance and disk use when enabled. |
| 3 | + |
| 4 | +.. contents:: On this page |
| 5 | + :local: |
| 6 | + :backlinks: none |
| 7 | + :depth: 1 |
| 8 | + :class: singlecol |
| 9 | + |
| 10 | +.. _manual-find-slow-queries-with-database-profiler: |
| 11 | + |
| 12 | +================= |
| 13 | +Find Slow Queries |
| 14 | +================= |
| 15 | + |
| 16 | +MongoDB includes the :ref:`database-profiler`, which can |
| 17 | +identify slow queries and help you determine how to improve |
| 18 | +query performance. |
| 19 | + |
| 20 | +About This Task |
| 21 | +--------------- |
| 22 | + |
| 23 | +Performance, Storage, and Security |
| 24 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 25 | + |
| 26 | +This task uses the database profiler to identify slow queries on |
| 27 | +a running :program:`mongod` instance. When enabled, the database |
| 28 | +profiler can affect performance and disk usage and expose |
| 29 | +unencrypted query data. |
| 30 | + |
| 31 | +.. warning:: |
| 32 | + |
| 33 | + Consider the performance, storage, and security implications |
| 34 | + before using the database profiler on a production |
| 35 | + deployment. |
| 36 | + |
| 37 | +Atlas Query Profiler |
| 38 | +~~~~~~~~~~~~~~~~~~~~ |
| 39 | + |
| 40 | +Atlas users can take advantage of the Atlas Query Profiler to |
| 41 | +identify slow queries with the convenience of visualization |
| 42 | +through a scatterplot chart. |
| 43 | + |
| 44 | +For more information, see :ref:`query-profiler`. |
| 45 | + |
| 46 | +Slow Queries |
| 47 | +~~~~~~~~~~~~ |
| 48 | + |
| 49 | +A slow query is one that takes longer than a specified amount of |
| 50 | +time to run. For this task, the slow query threshold is set to |
| 51 | +100 milliseconds. |
| 52 | + |
| 53 | +In some use cases, you may require queries run faster. In |
| 54 | +others, you may need to raise the threshold to focus only on |
| 55 | +those queries that are the slowest. |
| 56 | + |
| 57 | +Choose a slow query threshold that reflects your specific |
| 58 | +application and database needs. |
| 59 | + |
| 60 | +Context |
| 61 | +~~~~~~~ |
| 62 | + |
| 63 | +When enabled, the database profiler monitors queries at a |
| 64 | +database-level only. If you need the profiler to monitor slow |
| 65 | +queries on multiple databases, run the |
| 66 | +:method:`db.setProfilingLevel` method on each database. |
| 67 | + |
| 68 | +Sharded Clusters |
| 69 | +~~~~~~~~~~~~~~~~ |
| 70 | + |
| 71 | +The database profiler is not available through |
| 72 | +:program:`mongos`. |
| 73 | + |
| 74 | +Steps |
| 75 | +----- |
| 76 | + |
| 77 | +.. procedure:: |
| 78 | + :style: normal |
| 79 | + |
| 80 | + .. step:: Enable the database profiler. |
| 81 | + |
| 82 | + To enable the database profiler to monitor slow queries, |
| 83 | + use the :method:`db.setProfilingLevel` method: |
| 84 | + |
| 85 | + .. io-code-block:: |
| 86 | + |
| 87 | + .. input:: |
| 88 | + :language: js |
| 89 | + |
| 90 | + db.setProfilingLevel(1, 100) |
| 91 | + |
| 92 | + .. output:: |
| 93 | + :language: js |
| 94 | + |
| 95 | + { was: 0, slowms: 1, sampleRate: 1, ok: 1} |
| 96 | + |
| 97 | + This sets the profiling level to ``1``, which monitors for |
| 98 | + slow queries, and defines a query as slow if it takes |
| 99 | + longer than 100 milliseconds to run. |
| 100 | + |
| 101 | + .. step:: Check for slow queries. |
| 102 | + |
| 103 | + To list any slow queries found by the database profiler, |
| 104 | + query the :data:`system.profile <<database>.system.profile>` |
| 105 | + collection for relevant data: |
| 106 | + |
| 107 | + .. io-code-block:: |
| 108 | + |
| 109 | + .. input:: |
| 110 | + :language: js |
| 111 | + |
| 112 | + db.system.profile.find( { }, |
| 113 | + { |
| 114 | + command: 1, |
| 115 | + millis: 1, |
| 116 | + docsExamined: 1, |
| 117 | + keysExamined: 1, |
| 118 | + nreturned: 1 |
| 119 | + } |
| 120 | + ).sort( { ts: -1 } ) |
| 121 | + |
| 122 | + .. output:: |
| 123 | + :language: js |
| 124 | + |
| 125 | + [ |
| 126 | + { |
| 127 | + command: { |
| 128 | + find: 'people', |
| 129 | + filter: { age: { '$gt': 35 } }, |
| 130 | + lsid: { id: UUID('ae3e9932-0a78-47ab-b741-01dd3bfb3563') }, |
| 131 | + '$db': 'contacts' |
| 132 | + }, |
| 133 | + keysExamined: 0, |
| 134 | + docsExamined: 100000, |
| 135 | + nreturned: 40, |
| 136 | + millis: 143 |
| 137 | + } |
| 138 | + ] |
| 139 | + |
| 140 | + The command provides a list of slow queries observed by |
| 141 | + the database profiler. |
| 142 | + |
| 143 | + The projection filters the return documents to include |
| 144 | + information that you may find useful in determining what |
| 145 | + caused the query to run slow. |
| 146 | + |
| 147 | + - If ``keysExamined`` is ``0``, it indicates that an index |
| 148 | + was not used by the query. To solve this, :ref:`create |
| 149 | + an index <manual-create-an-index>` on the collection. |
| 150 | + |
| 151 | + - If an index was used and ``docsExamined`` is much larger |
| 152 | + than ``nreturned``, it indicates an ineffectual index. |
| 153 | + You may need to update the index or create a new one on |
| 154 | + a field or fields used by the query filter. |
| 155 | + |
| 156 | + - If ``keysExamined`` is high and ``docsExamined`` is low, |
| 157 | + it indicates effective index use. |
| 158 | + |
| 159 | + .. step:: Disable the database profiler. |
| 160 | + |
| 161 | + To ensure that the database profiler does not further |
| 162 | + disrupt performance, disable it when it's no longer |
| 163 | + needed: |
| 164 | + |
| 165 | + .. io-code-block:: |
| 166 | + |
| 167 | + .. input:: |
| 168 | + :language: javascript |
| 169 | + |
| 170 | + db.setProfilingLevel(0) |
| 171 | + |
| 172 | + .. output:: |
| 173 | + :language: javascript |
| 174 | + |
| 175 | + { was: 1, slowms: 1, sampleRate: 1, ok: 1} |
| 176 | + |
| 177 | +Examples |
| 178 | +-------- |
| 179 | + |
| 180 | +Ignore Indexes |
| 181 | +~~~~~~~~~~~~~~ |
| 182 | + |
| 183 | +To evaluate performance on a collection with an index, you can |
| 184 | +set the query to ignore indexes using the :method:`hint( { |
| 185 | +$natural: 1 } ) <cursor.hint>` method. |
| 186 | + |
| 187 | +.. code-block:: javascript |
| 188 | + |
| 189 | + db.listingsAndReviews.find( { |
| 190 | + $or: [ |
| 191 | + { "address.market": "Berlin" }, |
| 192 | + { "review_scores.review_scores_cleanliness": { $lt: 5 } } |
| 193 | + ], |
| 194 | + $where: function () { |
| 195 | + return this.amenities && this.amenities.length > 15; |
| 196 | + } |
| 197 | + } ).sort( { description: 1 } ).hint( { $natural: 1 } ); |
| 198 | + |
| 199 | +You may find this useful in cases where you want to compare how |
| 200 | +queries perform with a collection scan to that of an index scan. |
| 201 | + |
| 202 | +Next Steps |
| 203 | +---------- |
| 204 | + |
| 205 | +- :ref:`manual-create-an-index` |
| 206 | + |
| 207 | +Learn More |
| 208 | +---------- |
| 209 | + |
| 210 | +- :ref:`query-performance` |
| 211 | + |
| 212 | +- :ref:`Atlas Query Profiler <query-profiler>` |
| 213 | + |
| 214 | +- :ref:`database-profiler` |
| 215 | + |
| 216 | +- :ref:`indexes` |
| 217 | + |
0 commit comments