Skip to content

Commit 0e367a6

Browse files
author
Kay Kim
committed
DOCS-11962,DOCS-12387,DOCS-11959,DOCS-11965: queryHash and planCacheKey
1 parent ea89f82 commit 0e367a6

18 files changed

+324
-56
lines changed

source/administration/analyzing-mongodb-performance.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ The following profiling levels are available:
136136

137137
.. note::
138138
.. include:: /includes/fact-log-slow-queries.rst
139+
140+
.. include:: /includes/extracts/4.2-changes-log-query-shapes-plan-cache-key.rst
139141

140142
.. _ftdc-stub:
141143

source/core/index-partial.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ You can specify a ``partialFilterExpression`` option for all MongoDB
4646
Behavior
4747
--------
4848

49+
.. _partial-index-query-coverage:
50+
4951
Query Coverage
5052
~~~~~~~~~~~~~~
5153

source/core/query-plans.txt

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,9 @@ The following diagram illustrates the query planner logic:
4242
See :ref:`query-plans-plan-cache-flushes` for additional scenarios that trigger
4343
changes to the plan cache.
4444

45-
You can use the :method:`db.collection.explain()` or the
46-
:method:`cursor.explain()` method to view statistics about the query
47-
plan for a given query. This information can help as you develop
48-
:doc:`indexing strategies </applications/indexes>`.
49-
50-
.. include:: /includes/fact-explain-collection-method.rst
51-
52-
.. versionchanged:: 2.6
53-
54-
:method:`~db.collection.explain()` operations no longer read from or write
55-
to the query planner cache.
45+
See :method:`db.collection.explain()` or the :method:`cursor.explain()`
46+
method for information about viewing query plan information for a given
47+
query.
5648

5749
.. _query-plans-query-revision:
5850

@@ -61,16 +53,85 @@ plan for a given query. This information can help as you develop
6153
Plan Cache Flushes
6254
------------------
6355

64-
Catalog operations like index or collection drops flush the plan cache.
56+
The query plan cache does not persist if a :binary:`~bin.mongod`
57+
restarts or shuts down. In addition, catalog operations like index or
58+
collection drops clear the plan cache.
6559

66-
The plan cache does not persist if a :binary:`~bin.mongod` restarts or shuts down.
60+
Users can also:
6761

68-
.. versionadded:: 2.6
62+
- Manually clear the entire plan cache using the
63+
:method:`PlanCache.clear()` method.
64+
65+
- Manually clear specific plan cache entries using the
66+
:method:`PlanCache.clearPlansByQuery()` method.
67+
68+
.. seealso:: :ref:`query-hash-plan-cache-key`
69+
70+
.. _query-hash-plan-cache-key:
71+
72+
``queryHash`` and ``planCacheKey``
73+
----------------------------------
74+
75+
.. _query-hash:
76+
77+
78+
``queryHash``
79+
~~~~~~~~~~~~~
80+
81+
.. include:: /includes/extracts/4.2-changes-query-shapes.rst
82+
83+
.. _plan-cache-key:
84+
85+
``planCacheKey``
86+
~~~~~~~~~~~~~~~~
87+
88+
.. include:: /includes/extracts/4.2-changes-plan-cache-key.rst
6989

70-
MongoDB provides :doc:`/reference/method/js-plan-cache` to view and modify
71-
the cached query plans. The :method:`PlanCache.clear()` method flushes the
72-
entire plan cache. Users can also clear particular plan cache entries using
73-
:method:`PlanCache.clearPlansByQuery()`.
90+
For example, consider a collection ``foo`` with the following indexes:
91+
92+
.. code-block:: javascript
93+
94+
db.foo.createIndex( { x: 1 } )
95+
db.foo.createIndex( { x: 1, y: 1 } )
96+
db.foo.createIndex( { x: 1, z: 1 }, { partialFilterExpression: { x: { $gt: 10 } } } )
97+
98+
The following queries on the collection have the same shape:
99+
100+
.. code-block:: javascript
101+
102+
db.foo.explain().find( { x: { $gt: 5 } } ) // Query Operation 1
103+
db.foo.explain().find( { x: { $gt: 20 } } ) // Query Operation 2
104+
105+
Given these queries, the index with the :ref:`partial filter expression
106+
<partial-index-query-coverage>` can support query operation 2 but *not*
107+
support query operation 1. Since the indexes available to support query operation 1
108+
differs from query operation 2, the two queries have different
109+
``planCacheKey``.
110+
111+
If one of the indexes were dropped, or if a new index ``{ x: 1, a: 1
112+
}`` were added, the ``planCacheKey`` for both query operations will
113+
change.
114+
115+
Availability
116+
~~~~~~~~~~~~
117+
118+
The ``queryHash`` and ``planCacheKey`` are available in:
119+
120+
- :doc:`explain() output </reference/explain-results>` fields:
121+
:data:`queryPlanner.queryHash <explain.queryPlanner.queryHash>` and
122+
:data:`queryPlanner.planCacheKey <explain.queryPlanner.planCacheKey>`
123+
124+
- :doc:`profiler log messages </tutorial/manage-the-database-profiler>`
125+
and :doc:`diagnostic log messages (i.e. mongod/mongos log
126+
messages)</reference/log-messages>` when logging slow queries.
127+
128+
- :pipeline:`$planCacheStatus` aggregation stage (*New in MongoDB 4.2*)
129+
130+
- :method:`PlanCache.listQueryShapes()`
131+
method/:dbcommand:`planCacheListQueryShapes` command
132+
133+
- :method:`PlanCache.getPlansByQuery()`
134+
method/:dbcommand:`planCacheListPlans` command
74135

75136
.. _index-filters:
76137

@@ -105,3 +166,7 @@ sparingly.
105166

106167
See :dbcommand:`planCacheListFilters`,
107168
:dbcommand:`planCacheClearFilters`, and :dbcommand:`planCacheSetFilter`.
169+
170+
.. seealso::
171+
172+
:doc:`/applications/indexes`

source/includes/extracts-4.2-changes.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,53 @@ content: |
105105
entries </tutorial/manage-the-database-profiler>` and
106106
:doc:`diagnostic log messages </reference/log-messages>` include
107107
:data:`~system.profile.storage` information.
108+
---
109+
ref: 4.2-changes-log-query-shapes-plan-cache-key
110+
content: |
111+
112+
Starting in MongoDB 4.2, the :doc:`profiler entries
113+
</tutorial/manage-the-database-profiler>` and the :ref:`diagnostic
114+
log messages (i.e. mongod/mongos log
115+
messages)<log-message-slow-ops>` for read/write operations include:
116+
117+
- ``queryHash`` to help identify slow queries with the same
118+
:term:`query shape`.
119+
120+
- ``planCacheKey`` to provide more insight into the :doc:`query plan
121+
cache </core/query-plans>` for slow queries.
122+
123+
---
124+
ref: 4.2-changes-query-shapes
125+
content: |
126+
127+
To help identify slow queries with the same :term:`query shape`,
128+
starting in MongoDB 4.2, each :term:`query shape` is associated with
129+
a :ref:`queryHash <4.2-query-hash>`. The ``queryHash`` is a
130+
hexadecimal string that represents a hash of the query shape and
131+
is dependent only on the query shape.
132+
133+
.. note::
134+
135+
As with any hash function, two different query shapes may result
136+
in the same hash value. However, the occurrence of hash
137+
collisions between different query shapes is unlikely.
138+
139+
---
140+
ref: 4.2-changes-plan-cache-key
141+
content: |
142+
143+
To provide more insight into the :doc:`query plan cache
144+
</core/query-plans>`, MongoDB 4.2 introduces the :ref:`planCacheKey
145+
<4.2-plan-cache-key>`. ``planCacheKey`` is a hexadecimal string that
146+
represents a query shape and the possible plans for that shape.
147+
148+
.. note::
149+
150+
Unlike the ``queryHash``, the ``planCacheKey`` is dependent on
151+
both the query shape and the currently available indexes for the
152+
shape. That is, if indexes that can support the query shape are
153+
added/dropped, the ``planCacheKey`` value changes whereas the
154+
``queryHash`` value would not change.
108155
109156
...
110157

source/reference/command/planCacheClear.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ If a collection ``orders`` has the following query shape:
6060
{
6161
"query" : { "qty" : { "$gt" : 10 } },
6262
"sort" : { "ord_date" : 1 },
63-
"projection" : { }
63+
"projection" : { },
64+
"queryHash" : "9AAD95BE" // Available starting in MongoDB 4.2
6465
}
6566

6667
The following operation clears the query plan cached for the shape:

source/reference/command/planCacheListPlans.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ Definition
1515

1616
.. dbcommand:: planCacheListPlans
1717

18-
.. versionadded:: 2.6
19-
2018
Displays the cached query plans for the specified :term:`query
2119
shape`.
2220

21+
.. include:: /includes/extracts/4.2-changes-query-shapes.rst
22+
2323
.. include:: /includes/fact-query-optimizer-cache-behavior.rst
2424

2525
The :binary:`~bin.mongo` shell provides the wrapper
@@ -65,7 +65,8 @@ If a collection ``orders`` has the following query shape:
6565
{
6666
"query" : { "qty" : { "$gt" : 10 } },
6767
"sort" : { "ord_date" : 1 },
68-
"projection" : { }
68+
"projection" : { },
69+
"queryHash" : "9AAD95BE" // Available starting in MongoDB 4.2
6970
}
7071

7172
The following operation displays the query plan cached for the shape:

source/reference/command/planCacheListQueryShapes.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ Definition
1515

1616
.. dbcommand:: planCacheListQueryShapes
1717

18-
.. versionadded:: 2.6
19-
2018
Displays the :term:`query shapes <query shape>` for which cached
2119
query plans exist for a collection.
2220

21+
.. include:: /includes/extracts/4.2-changes-query-shapes.rst
22+
2323
.. include:: /includes/fact-query-optimizer-cache-behavior.rst
2424

2525
The :binary:`~bin.mongo` shell provides the wrapper
@@ -74,7 +74,8 @@ query plans associated with the following shapes:
7474
{
7575
"query" : { "qty" : { "$gt" : 10 } },
7676
"sort" : { "ord_date" : 1 },
77-
"projection" : { }
77+
"projection" : { },
78+
"queryHash" : "9AAD95BE" // Available starting in MongoDB 4.2
7879
},
7980
{
8081
"query" : { "$or" : [ { "qty" : { "$gt" : 15 } }, { "status" : "A" } ] },
@@ -89,7 +90,8 @@ query plans associated with the following shapes:
8990
]
9091
},
9192
"sort" : { },
92-
"projection" : { }
93+
"projection" : { },
94+
"queryHash" : "0A087AD0" // Available starting in MongoDB 4.2
9395
}
9496
],
9597
"ok" : 1

source/reference/database-profiler.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,42 @@ operation.
441441
see :ref:`the FAQ on when operations yield
442442
<faq-concurrency-yielding>`.
443443

444+
.. data:: system.profile.queryHash
445+
446+
A hexadecimal string that represents a hash of the :term:`query
447+
shape` and is dependent only on the query shape. ``queryHash`` can
448+
help identify slow queries (including the query filter of write
449+
operations) with the same query shape.
450+
451+
.. note::
452+
453+
As with any hash function, two different query shapes may result
454+
in the same hash value. However, the occurrence of hash
455+
collisions between different query shapes is unlikely.
456+
457+
For more information on ``queryHash`` and ``planCacheKey``, see
458+
:ref:`query-hash-plan-cache-key`.
459+
460+
.. versionadded:: 4.2
461+
462+
.. data:: system.profile.planCacheKey
463+
464+
A hexadecimal string that represents a query shape and the possible
465+
plans for that shape. ``planCacheKey`` can provide insight into the
466+
plan cache.
467+
468+
Unlike the :data:`~system.profile.queryHash`, the
469+
:data:`~system.profile.planCacheKey` is dependent on both the query
470+
shape and the currently available indexes for the shape. That is, if
471+
indexes that can support the query shape are added/dropped, the
472+
``planCacheKey`` value changes whereas the ``queryHash`` value would
473+
not change.
474+
475+
For more information on ``queryHash`` and ``planCacheKey``, see
476+
:ref:`query-hash-plan-cache-key`.
477+
478+
.. versionadded:: 4.2
479+
444480
.. data:: system.profile.locks
445481

446482
.. versionadded:: 3.0.0

source/reference/explain-results.txt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ the ``explain`` operation.
9797
"parsedQuery" : {
9898
...
9999
},
100+
"queryHash" : <hexadecimal string>,
101+
"planCacheKey" : <hexadecimal string>,
100102
"winningPlan" : {
101103
"stage" : <STAGE1>,
102104
...
@@ -139,7 +141,9 @@ the ``explain`` operation.
139141
},
140142
"plannerVersion" : <int>,
141143
"namespace" : <string>,
142-
...
144+
"parsedQuery" : <document>,
145+
"queryHash" : <hexadecimal string>,
146+
"planCacheKey" : <hexadecimal string>,
143147
"winningPlan" : {
144148
"stage" : <STAGE2>,
145149
"inputStage" : {
@@ -173,6 +177,42 @@ the ``explain`` operation.
173177
A boolean that specifies whether MongoDB applied an :ref:`index
174178
filter <index-filters>` for the :term:`query shape`.
175179

180+
.. data:: explain.queryPlanner.queryHash
181+
182+
A hexadecimal string that represents the hash of the
183+
:term:`query shape` and is dependent only on the query shapes.
184+
``queryHash`` can help identify slow queries (including the
185+
query filter of write operations) with the same query shape.
186+
187+
.. note::
188+
189+
As with any hash function, two different query shapes may result
190+
in the same hash value. However, the occurrence of hash
191+
collisions between different query shapes is unlikely.
192+
193+
For more information on ``queryHash`` and ``planCacheKey``,
194+
see :ref:`query-hash-plan-cache-key`.
195+
196+
.. versionadded:: 4.2
197+
198+
.. data:: explain.queryPlanner.planCacheKey
199+
200+
A hexadecimal string that represents the mapping between a
201+
query shape and the query plan. ``planCacheKey`` can provide
202+
insight into the plan cache.
203+
204+
Unlike the :data:`~explain.queryPlanner.queryHash`, the
205+
:data:`~explain.queryPlanner.planCacheKey` is dependent on
206+
both the query shape and the currently available indexes for
207+
the shape. That is, if indexes that can support the query
208+
shape are added/dropped, the ``planCacheKey`` value changes
209+
whereas the ``queryHash`` value would not change.
210+
211+
For more information on ``queryHash`` and ``planCacheKey``,
212+
see :ref:`query-hash-plan-cache-key`.
213+
214+
.. versionadded:: 4.2
215+
176216
.. data:: explain.queryPlanner.winningPlan
177217

178218
A document that details the plan selected by the :doc:`query

source/reference/glossary.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,15 +725,16 @@ Glossary
725725
query plan. See :ref:`read-operations-query-optimization`.
726726

727727
query shape
728-
A combination of query predicate, sort, and projection
729-
specifications.
728+
A combination of query predicate, sort, and projection.
730729

731730
For the query predicate, only the structure of the predicate,
732731
including the field names, are significant; the values in the
733732
query predicate are insignificant. As such, a query predicate ``{
734733
type: 'food' }`` is equivalent to the query predicate ``{ type:
735734
'utensil' }`` for a query shape.
736735

736+
.. include:: /includes/extracts/4.2-changes-query-shapes.rst
737+
737738
read concern
738739
Specifies a level of isolation for read operations. For example,
739740
you can use read concern to only read data that has propagated to

0 commit comments

Comments
 (0)