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
|
// Cannot implicitly shard accessed collections because the explain output from a mongod when run
// against a sharded collection is wrapped in a "shards" object with keys for each shard.
// @tags: [assumes_unsharded_collection]
// Tests for the .explain() shell helper, which provides syntactic sugar for the explain command.
var t = db.jstests_explain_helpers;
t.drop();
// Include helpers for analyzing explain output.
load("jstests/libs/analyze_plan.js");
var explain;
var stage;
t.ensureIndex({a: 1});
for (var i = 0; i < 10; i++) {
t.insert({_id: i, a: i, b: 1});
}
//
// Basic .find()
//
// No verbosity specified means that we should use "queryPlanner" verbosity.
explain = t.explain().find().finish();
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert(!("executionStats" in explain));
// .explain() can also come after .find().
explain = t.find().explain();
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert(!("executionStats" in explain));
// .explain(true) means get execution stats for all candidate plans.
explain = t.explain(true).find().finish();
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert("executionStats" in explain);
assert("allPlansExecution" in explain.executionStats);
// .explain(true) after .find().
explain = t.find().explain(true);
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert("executionStats" in explain);
assert("allPlansExecution" in explain.executionStats);
//
// Test verbosity specifiers.
//
// "queryPlanner"
explain = t.explain("queryPlanner").find().finish();
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert(!("executionStats" in explain));
explain = t.find().explain("queryPlanner");
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert(!("executionStats" in explain));
// "executionStats"
explain = t.explain("executionStats").find().finish();
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert("executionStats" in explain);
assert(!("allPlansExecution" in explain.executionStats));
explain = t.find().explain("executionStats");
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert("executionStats" in explain);
assert(!("allPlansExecution" in explain.executionStats));
// "allPlansExecution"
explain = t.explain("allPlansExecution").find().finish();
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert("executionStats" in explain);
assert("allPlansExecution" in explain.executionStats);
explain = t.find().explain("allPlansExecution");
assert.commandWorked(explain);
assert("queryPlanner" in explain);
assert("executionStats" in explain);
assert("allPlansExecution" in explain.executionStats);
//
// Tests for DBExplainQuery helpers.
//
// .limit()
explain = t.explain().find().limit(3).finish();
assert.commandWorked(explain);
explain = t.find().limit(3).explain();
assert.commandWorked(explain);
// .batchSize()
explain = t.explain().find().batchSize(3).finish();
assert.commandWorked(explain);
explain = t.find().batchSize(3).explain();
assert.commandWorked(explain);
// .addOption()
explain = t.explain().find().addOption(DBQuery.Option.noTimeout).finish();
assert.commandWorked(explain);
explain = t.find().batchSize(DBQuery.Option.noTimeout).explain();
assert.commandWorked(explain);
// .skip()
explain = t.explain().find().skip(3).finish();
assert.commandWorked(explain);
explain = t.find().skip(3).explain();
assert.commandWorked(explain);
// .sort()
explain = t.explain().find().sort({b: -1}).finish();
assert.commandWorked(explain);
assert(planHasStage(explain.queryPlanner.winningPlan, "SORT"));
explain = t.find().sort({b: -1}).explain();
assert.commandWorked(explain);
assert(planHasStage(explain.queryPlanner.winningPlan, "SORT"));
// .hint()
explain = t.explain().find().hint({a: 1}).finish();
assert.commandWorked(explain);
assert(isIxscan(explain.queryPlanner.winningPlan));
explain = t.explain().find().hint("a_1").finish();
assert.commandWorked(explain);
assert(isIxscan(explain.queryPlanner.winningPlan));
explain = t.find().hint({a: 1}).explain();
assert.commandWorked(explain);
assert(isIxscan(explain.queryPlanner.winningPlan));
explain = t.find().hint("a_1").explain();
assert.commandWorked(explain);
assert(isIxscan(explain.queryPlanner.winningPlan));
// .min()
explain = t.explain().find().min({a: 1}).finish();
assert.commandWorked(explain);
assert(isIxscan(explain.queryPlanner.winningPlan));
explain = t.find().min({a: 1}).explain();
assert.commandWorked(explain);
assert(isIxscan(explain.queryPlanner.winningPlan));
// .max()
explain = t.explain().find().max({a: 1}).finish();
assert.commandWorked(explain);
assert(isIxscan(explain.queryPlanner.winningPlan));
explain = t.find().max({a: 1}).explain();
assert.commandWorked(explain);
assert(isIxscan(explain.queryPlanner.winningPlan));
// .showDiskLoc()
explain = t.explain().find().showDiskLoc().finish();
assert.commandWorked(explain);
explain = t.find().showDiskLoc().explain();
assert.commandWorked(explain);
// .maxTimeMS()
explain = t.explain().find().maxTimeMS(200).finish();
assert.commandWorked(explain);
explain = t.find().maxTimeMS(200).explain();
assert.commandWorked(explain);
// .readPref()
explain = t.explain().find().readPref("secondary").finish();
assert.commandWorked(explain);
explain = t.find().readPref("secondary").explain();
assert.commandWorked(explain);
// .comment()
explain = t.explain().find().comment("test .comment").finish();
assert.commandWorked(explain);
explain = t.find().comment("test .comment").explain();
assert.commandWorked(explain);
// .snapshot()
explain = t.explain().find().snapshot().finish();
assert.commandWorked(explain);
explain = t.find().snapshot().explain();
assert.commandWorked(explain);
// .next()
explain = t.explain().find().next();
assert.commandWorked(explain);
assert("queryPlanner" in explain);
// .hasNext()
var explainQuery = t.explain().find();
assert(explainQuery.hasNext());
assert.commandWorked(explainQuery.next());
assert(!explainQuery.hasNext());
// .forEach()
var results = [];
t.explain().find().forEach(function(res) {
results.push(res);
});
assert.eq(1, results.length);
assert.commandWorked(results[0]);
//
// .aggregate()
//
explain = t.explain().aggregate([{$match: {a: 3}}]);
assert.commandWorked(explain);
assert.eq(1, explain.stages.length);
assert("queryPlanner" in explain.stages[0].$cursor);
// Legacy varargs format.
explain = t.explain().aggregate({$match: {a: 3}});
assert.commandWorked(explain);
assert.eq(1, explain.stages.length);
assert("queryPlanner" in explain.stages[0].$cursor);
explain = t.explain().aggregate({$match: {a: 3}}, {$project: {a: 1}});
assert.commandWorked(explain);
assert.eq(2, explain.stages.length);
assert("queryPlanner" in explain.stages[0].$cursor);
// Options already provided.
explain = t.explain().aggregate([{$match: {a: 3}}], {allowDiskUse: true});
assert.commandWorked(explain);
assert.eq(1, explain.stages.length);
assert("queryPlanner" in explain.stages[0].$cursor);
//
// .count()
//
// Basic count.
explain = t.explain().count();
assert.commandWorked(explain);
assert(planHasStage(explain.queryPlanner.winningPlan, "COUNT"));
// Tests for applySkipLimit argument to .count. When we don't apply the skip, we
// count one result. When we do apply the skip we count zero.
explain = t.explain("executionStats").find({a: 3}).skip(1).count(false);
stage = explain.executionStats.executionStages;
if ("SINGLE_SHARD" === stage.stage) {
stage = stage.shards[0].executionStages;
}
assert.eq(1, stage.nCounted);
explain = t.explain("executionStats").find({a: 3}).skip(1).count(true);
stage = explain.executionStats.executionStages;
if ("SINGLE_SHARD" === stage.stage) {
stage = stage.shards[0].executionStages;
}
assert.eq(0, stage.nCounted);
// Count with hint.
explain = t.explain().find({a: 3}).hint({a: 1}).count();
assert.commandWorked(explain);
assert(planHasStage(explain.queryPlanner.winningPlan, "COUNT"));
assert(planHasStage(explain.queryPlanner.winningPlan, "COUNT_SCAN"));
// Explainable count with hint.
assert.commandWorked(t.ensureIndex({c: 1}, {sparse: true}));
explain = t.explain().count({c: {$exists: false}}, {hint: "c_1"});
assert.commandWorked(explain);
assert(planHasStage(explain.queryPlanner.winningPlan, "IXSCAN"));
assert.eq(getPlanStage(explain.queryPlanner.winningPlan, "IXSCAN").indexName, "c_1");
assert.commandWorked(t.dropIndex({c: 1}));
//
// .group()
//
explain = t.explain().group({key: "a", initial: {}, reduce: function() {}});
assert.commandWorked(explain);
//
// .distinct()
//
explain = t.explain().distinct('_id');
assert.commandWorked(explain);
assert(planHasStage(explain.queryPlanner.winningPlan, "PROJECTION"));
assert(planHasStage(explain.queryPlanner.winningPlan, "DISTINCT_SCAN"));
explain = t.explain().distinct('a');
assert.commandWorked(explain);
assert(planHasStage(explain.queryPlanner.winningPlan, "PROJECTION"));
assert(planHasStage(explain.queryPlanner.winningPlan, "DISTINCT_SCAN"));
explain = t.explain().distinct('b');
assert.commandWorked(explain);
assert(planHasStage(explain.queryPlanner.winningPlan, "COLLSCAN"));
//
// .remove()
//
// Check that there is one matching document.
assert.eq(1, t.find({a: 3}).itcount());
// Explain a single-document delete.
explain = t.explain("executionStats").remove({a: 3}, true);
assert.commandWorked(explain);
assert.eq(1, explain.executionStats.totalDocsExamined);
// Document should not have been deleted.
assert.eq(1, t.find({a: 3}).itcount());
// Explain a single-document delete with the new syntax.
explain = t.explain("executionStats").remove({a: 3}, {justOne: true});
assert.commandWorked(explain);
assert.eq(1, explain.executionStats.totalDocsExamined);
// Document should not have been deleted.
assert.eq(1, t.find({a: 3}).itcount());
// Explain a multi-document delete.
explain = t.explain("executionStats").remove({a: {$lte: 2}});
assert.commandWorked(explain);
assert.eq(3, explain.executionStats.totalDocsExamined);
// All 10 docs in the collection should still be present.
assert.eq(10, t.count());
//
// .update()
//
// Basic update.
explain = t.explain("executionStats").update({a: 3}, {$set: {b: 3}});
assert.commandWorked(explain);
assert.eq(1, explain.executionStats.totalDocsExamined);
// Document should not have been updated.
assert.eq(1, t.findOne({a: 3})["b"]);
// Update with upsert flag set that should do an insert.
explain = t.explain("executionStats").update({a: 15}, {$set: {b: 3}}, true);
assert.commandWorked(explain);
stage = explain.executionStats.executionStages;
if ("SHARD_WRITE" === stage.stage) {
stage = stage.shards[0].executionStages;
}
assert.eq(stage.stage, "UPDATE");
assert(stage.wouldInsert);
// Make sure that the insert didn't actually happen.
assert.eq(10, t.count());
// Use the {upsert: true} syntax.
explain = t.explain("executionStats").update({a: 15}, {$set: {b: 3}}, {upsert: true});
assert.commandWorked(explain);
var stage = explain.executionStats.executionStages;
if ("SHARD_WRITE" === stage.stage) {
stage = stage.shards[0].executionStages;
}
assert.eq(stage.stage, "UPDATE");
assert(stage.wouldInsert);
assert.eq(0, stage.nMatched);
// Make sure that the insert didn't actually happen.
assert.eq(10, t.count());
// Update with multi-update flag set.
explain = t.explain("executionStats").update({a: {$lte: 2}}, {$set: {b: 3}}, false, true);
assert.commandWorked(explain);
var stage = explain.executionStats.executionStages;
if ("SHARD_WRITE" === stage.stage) {
stage = stage.shards[0].executionStages;
}
assert.eq(stage.stage, "UPDATE");
assert(!stage.wouldInsert);
assert.eq(3, stage.nMatched);
assert.eq(3, stage.nWouldModify);
// Use the {multi: true} syntax.
explain = t.explain("executionStats").update({a: {$lte: 2}}, {$set: {b: 3}}, {multi: true});
assert.commandWorked(explain);
var stage = explain.executionStats.executionStages;
if ("SHARD_WRITE" === stage.stage) {
stage = stage.shards[0].executionStages;
}
assert.eq(stage.stage, "UPDATE");
assert(!stage.wouldInsert);
assert.eq(3, stage.nMatched);
assert.eq(3, stage.nWouldModify);
//
// .findAndModify()
//
// Basic findAndModify with update.
explain = t.explain("executionStats").findAndModify({query: {a: 3}, update: {$set: {b: 3}}});
assert.commandWorked(explain);
assert.eq(1, explain.executionStats.totalDocsExamined);
// Document should not have been updated.
assert.eq(1, t.findOne({a: 3})["b"]);
// Basic findAndModify with delete.
explain = t.explain("executionStats").findAndModify({query: {a: 3}, remove: true});
assert.commandWorked(explain);
assert.eq(1, explain.executionStats.totalDocsExamined);
// Delete shouldn't have happened.
assert.eq(10, t.count());
// findAndModify with upsert flag set that should do an insert.
explain = t.explain("executionStats")
.findAndModify({query: {a: 15}, update: {$set: {b: 3}}, upsert: true});
assert.commandWorked(explain);
stage = explain.executionStats.executionStages;
if ("SINGLE_SHARD" === stage.stage) {
stage = stage.shards[0].executionStages;
}
assert.eq(stage.stage, "UPDATE");
assert(stage.wouldInsert);
// Make sure that the insert didn't actually happen.
assert.eq(10, t.count());
//
// Error cases.
//
// Invalid verbosity string.
assert.throws(function() {
t.explain("foobar").find().finish();
});
assert.throws(function() {
t.find().explain("foobar");
});
// Can't explain an update without a query.
assert.throws(function() {
t.explain().update();
});
// Can't explain an update without mods.
assert.throws(function() {
t.explain().update({a: 3});
});
// Can't add fourth arg when using document-style specification of update options.
assert.throws(function() {
t.explain().update({a: 3}, {$set: {b: 4}}, {multi: true}, true);
});
// Missing "initial" for explaining a group.
assert.throws(function() {
t.explain().group({key: "a", reduce: function() {}});
});
// Can't specify both remove and update in a findAndModify
assert.throws(function() {
t.explain().findAndModify({remove: true, update: {$set: {b: 3}}});
});
|