-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathPlanCache.list.txt
558 lines (460 loc) · 17.8 KB
/
PlanCache.list.txt
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
================
PlanCache.list()
================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: PlanCache.list(<pipeline>)
Returns an array of :ref:`plan cache <query-plans-query-optimization>` entries
for a collection.
The method is only available from the :method:`plan cache object
<db.collection.getPlanCache()>` of a specific collection; i.e.
.. code-block:: javascript
db.collection.getPlanCache().list(<pipeline>)
.. list-table::
:header-rows: 1
:widths: 20 20 60
* - Parameter
- Type
- Description
* - :ref:`pipeline <plancache-list-pipeline>`
- Array
- .. _plancache-list-pipeline:
Optional. :ref:`Aggregation pipeline <aggregation-pipeline>`
to filter/process the :term:`plan cache query shapes <plan cache query shape>`.
You can run this method from a :binary:`~bin.mongod` or
:binary:`~bin.mongos` instance. When run on a sharded cluster, the
method returns plan cache entry information from a single member in
each shard replica set. This member is identified with the
:ref:`shard <plancachestats-shard>` and :ref:`host
<plancachestats-host>` fields. See also
:ref:`plancache-list-read-pref`.
The method :method:`PlanCache.list()` wraps the
:pipeline:`$planCacheStats` aggregation pipeline. That is,
.. code-block:: javascript
db.collection.getPlanCache().list([<stage1>, <stage2>, ...] )
is equivalent to
.. code-block:: javascript
db.collection.aggregate([ <$planCacheStats stage>, <stage1>, <stage2>, ... ]).toArray();
For details on the output, see :ref:`$planCacheStats output
<planCacheStats-output>`.
Not all queries automatically place a query plan in the cache.
:method:`PlanCache.list()` returns an empty array if there are
currently no plan cache query shapes with cached query plans.
.. seealso::
- :pipeline:`$planCacheStats`
- :method:`db.collection.aggregate()`
- :method:`~cursor.toArray()`
Compatibility
-------------
This method is available in deployments hosted in the following environments:
.. include:: /includes/fact-environments-atlas-only.rst
.. include:: /includes/fact-environments-onprem-only.rst
Considerations
--------------
Query Settings
~~~~~~~~~~~~~~
.. include:: /includes/persistent-query-settings-avoid-index-filters-intro.rst
Restrictions
~~~~~~~~~~~~
:method:`PlanCache.list()` is not allowed in :doc:`transactions
</core/transactions>`.
Query Hash and Plan Cache Query Shapes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/4.2-changes-query-shapes.rst
.. include:: /includes/fact-query-optimizer-cache-behavior.rst
Each entry in the plan cache is associated with a ``planCacheShapeHash``.
.. warning::
.. include:: /includes/plan-cache-rename.rst
.. _plancache-list-read-pref:
Read Preference
~~~~~~~~~~~~~~~
:method:`PlanCache.list()` observes the :doc:`read preference
</core/read-preference>` in selecting the host(s) from which to return
the plan cache information.
Applications may target different members of a replica set. As such,
each replica set member might receive different read commands and have
plan cache information that differs from other members. Nevertheless,
running :method:`PlanCache.list()` on a replica set or a sharded
cluster obeys the normal read preference rules. That is, on a replica
set, the operation gathers plan cache information from just one member
of replica set, and on a sharded cluster, the operation gathers plan
cache information from just one member of each shard replica set.
Required Access
---------------
On systems running with :setting:`~security.authorization`, the user
must have the :authaction:`planCacheRead` privilege for the collection.
Examples
--------
.. note::
- Not all queries automatically place a query plan in the cache.
:method:`PlanCache.list()` returns an empty array if
there are currently no query shapes with cached query plans.
- For details on the output, see :ref:`$planCacheStats output
<planCacheStats-output>`.
The examples in this section use the following ``orders`` collection:
.. code-block:: javascript
db.orders.insertMany( [
{ "_id" : 1, "item" : "abc", "price" : NumberDecimal("12"), "quantity" : 2, "type": "apparel" },
{ "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : 1, "type": "electronics" },
{ "_id" : 3, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : 5, "type": "apparel" },
{ "_id" : 4, "item" : "abc", "price" : NumberDecimal("8"), "quantity" : 10, "type": "apparel" },
{ "_id" : 5, "item" : "jkl", "price" : NumberDecimal("15"), "quantity" : 15, "type": "electronics" }
] )
Create the following indexes on the collection:
.. code-block:: javascript
db.orders.createIndex( { item: 1 } );
db.orders.createIndex( { item: 1, quantity: 1 } );
db.orders.createIndex( { item: 1, price: 1 }, { partialFilterExpression: { price: { $gte: NumberDecimal("10")} } } );
db.orders.createIndex( { quantity: 1 } );
db.orders.createIndex( { quantity: 1, type: 1 } );
.. note::
Index ``{ item: 1, price: 1 }`` is a :doc:`partial index
</core/index-partial>` and only indexes documents with ``price``
field greater than or equal to ``NumberDecimal("10")``.
Run some queries against the collection:
.. code-block:: javascript
db.orders.find( { item: "abc", price: { $gte: NumberDecimal("10") } } )
db.orders.find( { item: "abc", price: { $gte: NumberDecimal("5") } } )
db.orders.find( { quantity: { $gte: 20 } } )
db.orders.find( { quantity: { $gte: 5 }, type: "apparel" } )
Return Information for All Entries in the Query Cache
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following returns the :term:`plan cache query shapes <plan cache query shape>` that have
cached plans for the ``orders`` collection:
.. code-block:: javascript
db.orders.getPlanCache().list()
The method returns an array of the plan cache query shapes currently in the cache.
In the example, the ``orders`` collection had cached query plans
associated with the following shapes:
.. code-block:: javascript
:copyable: false
[
{
"createdFromQuery" : {
"query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" },
"sort" : { },
"projection" : { }
},
"planCacheShapeHash" : "4D151C4C",
"planCacheKey" : "DD67E353",
"isActive" : false,
"works" : NumberLong(4),
"cachedPlan" : {
"stage" : "FETCH",
"filter" : { "type" : { "$eq" : "apparel" } },
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : { "quantity" : 1 },
"indexName" : "quantity_1",
"isMultiKey" : false,
"multiKeyPaths" : { "quantity" : [ ] },
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : { "quantity" : [ "[5.0, inf.0]" ] }
}
},
"timeOfCreation" : ISODate("2020-02-06T15:57:18.219Z"),
"creationExecStats" : [
{
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 3,
"totalDocsExamined" : 3,
"executionStages" : {
"stage" : "FETCH",
"filter" : { "type" : { "$eq" : "apparel" } },
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 4,
...
}
},
{
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 3,
"totalDocsExamined" : 2,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 4,
...
}
}
],
"candidatePlanScores" : [
1.5002,
1.5002
],
"indexFilterSet" : false,
"estimatedSizeBytes" : NumberLong(3160), // Available starting in MongoDB 5.0
"host" : "mongodb1.example.net:27018",
"shard" : "shardA" // Available if run on sharded cluster
},
{
"createdFromQuery" : {
"query" : { "quantity" : { "$gte" : 20 } },
"sort" : { },
"projection" : { }
},
"planCacheShapeHash" : "23B19B75",
"planCacheKey" : "6F23F858",
"isActive" : false,
"works" : NumberLong(1),
...
},
{
"createdFromQuery" : {
"query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("5") } },
"sort" : { },
"projection" : { }
},
"planCacheShapeHash" : "117A6B10",
"planCacheKey" : "A1824628",
"isActive" : false,
"works" : NumberLong(4),
...
},
{
"createdFromQuery" : {
"query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("10") } },
"sort" : { },
"projection" : { }
},
"planCacheShapeHash" : "117A6B10",
"planCacheKey" : "2E6E536B",
"isActive" : false,
"works" : NumberLong(3),
...
}
]
.. warning::
.. include:: /includes/plan-cache-rename.rst
For details on the output, see :ref:`$planCacheStats output
<planCacheStats-output>`.
.. planCache-list-query-shapes:
List Plan Cache Query Shapes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To obtain a list of all of the query shapes for which there is a cached plan,
you can use the :method:`PlanCache.list()`. For example, the following operation
passes in a pipeline with a :pipeline:`$project` stage to only output the
:ref:`createdFromQuery <plancachestats-createdFromQuery>` field and the
:ref:`planCacheShapeHash <plancachestats-planCacheShapeHash>` field.
.. code-block:: javascript
db.orders.getPlanCache().list( [ { $project: {createdFromQuery: 1, planCacheShapeHash: 1 } } ] )
.. warning::
.. include:: /includes/plan-cache-rename.rst
The operation returns the following plan cache query shapes:
.. code-block:: javascript
:copyable: false
[
{ "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "4D151C4C" },
{ "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 20 } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "23B19B75" },
{ "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("5") } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "117A6B10" },
{ "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("10") } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "117A6B10" }
]
For details on the output, see :ref:`$planCacheStats output
<planCacheStats-output>`.
.. _planCache-list-cache-entry-for-query-shape:
Find Cache Entry Details for a Plan Cache Query Shape
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To return plan cache information for a particular query shape, pass in
a pipeline with a :pipeline:`$match` on the :ref:`planCacheKey
<plancachestats-planCacheKey>` field.
.. code-block:: javascript
db.orders.getPlanCache().list([ { $match: { planCacheKey: "DD67E353"} } ] )
The operation returns the following:
.. code-block:: javascript
:copyable: false
[
{
"createdFromQuery" : {
"query" : {
"quantity" : {
"$gte" : 5
},
"type" : "apparel"
},
"sort" : {
},
"projection" : {
}
},
"planCacheShapeHash" : "4D151C4C",
"planCacheKey" : "DD67E353",
"isActive" : false,
"works" : NumberLong(4),
"cachedPlan" : {
"stage" : "FETCH",
"filter" : {
"type" : {
"$eq" : "apparel"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"quantity" : 1
},
"indexName" : "quantity_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"quantity" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"quantity" : [
"[5.0, inf.0]"
]
}
}
},
"timeOfCreation" : ISODate("2020-02-11T17:14:33.873Z"),
"creationExecStats" : [
{
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 3,
"totalDocsExamined" : 3,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"type" : {
"$eq" : "apparel"
}
},
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 4,
"advanced" : 2,
"needTime" : 1,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"docsExamined" : 3,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 3,
"executionTimeMillisEstimate" : 0,
"works" : 4,
"advanced" : 3,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"keyPattern" : {
"quantity" : 1
},
"indexName" : "quantity_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"quantity" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"quantity" : [
"[5.0, inf.0]"
]
},
"keysExamined" : 3,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
},
{
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 3,
"totalDocsExamined" : 2,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 4,
"advanced" : 2,
"needTime" : 1,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"docsExamined" : 2,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 4,
"advanced" : 2,
"needTime" : 1,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"keyPattern" : {
"quantity" : 1,
"type" : 1
},
"indexName" : "quantity_1_type_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"quantity" : [ ],
"type" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"quantity" : [
"[5.0, inf.0]"
],
"type" : [
"[\"apparel\", \"apparel\"]"
]
},
"keysExamined" : 3,
"seeks" : 2,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
}
],
"candidatePlanScores" : [
1.5002,
1.5002
],
"indexFilterSet" : false,
"estimatedSizeBytes" : NumberLong(3160), // Available starting in MongoDB 5.0
"host" : "mongodb1.example.net:27018",
"shard" : "shardA" // Available if run on sharded cluster
}
]
.. warning::
.. include:: /includes/plan-cache-rename.rst
For details on the output, see :ref:`$planCacheStats output
<planCacheStats-output>`.