summaryrefslogtreecommitdiff
path: root/jstests/explainb.js
blob: bc15811e3195094bfe27bbd5eb3abd21161301dd (plain)
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
// nscanned and nscannedObjects report results for the winning plan; nscannedAllPlans and
// nscannedObjectsAllPlans report results for all plans.  SERVER-6268

t = db.jstests_explainb;
t.drop();

t.ensureIndex( { a:1, b:1 } );
t.ensureIndex( { b:1, a:1 } );

t.save( { a:0, b:1 } );
t.save( { a:1, b:0 } );

explain = t.find( { a:{ $gte:0 }, b:{ $gte:0 } } ).explain( true );

// The a:1,b:1 plan finishes scanning first.
assert.eq( 'BtreeCursor a_1_b_1', explain.cursor );
assert.eq( 2, explain.n );
// nscanned and nscannedObjects are reported for the a:1,b:1 plan.
assert.eq( 2, explain.nscanned );
assert.eq( 2, explain.nscannedObjects );
// nscannedAllPlans reports the combined total of all plans.
assert.eq( 6, explain.nscannedAllPlans );
// nscannedObjectsAllPlans reports the total for the set of interleaved plans.
assert.eq( 4, explain.nscannedObjectsAllPlans );

// A limit of 2.
explain = t.find( { a:{ $gte:0 }, b:{ $gte:0 } } ).limit( -2 ).explain( true );

assert.eq( 'BtreeCursor a_1_b_1', explain.cursor );
assert.eq( 2, explain.n );
assert.eq( 1, explain.nscanned );
assert.eq( 1, explain.nscannedObjects );
// The first result was identified for each plan.
assert.eq( 3, explain.nscannedAllPlans );
// One result was retrieved from each of the two indexed plans.
assert.eq( 2, explain.nscannedObjectsAllPlans );

// A $or query.
explain = t.find( { $or:[ { a:{ $gte:0 }, b:{ $gte:1 } },
                          { a:{ $gte:1 }, b:{ $gte:0 } } ] } ).explain( true );
assert.eq( 1, explain.clauses[ 0 ].n );
assert.eq( 1, explain.clauses[ 0 ].nscannedObjects );
assert.eq( 2, explain.clauses[ 0 ].nscanned );
assert.eq( 2, explain.clauses[ 0 ].nscannedObjectsAllPlans );
assert.eq( 4, explain.clauses[ 0 ].nscannedAllPlans );
assert.eq( 1, explain.clauses[ 1 ].n );
assert.eq( 1, explain.clauses[ 1 ].nscannedObjects );
assert.eq( 1, explain.clauses[ 1 ].nscanned );
assert.eq( 2, explain.clauses[ 1 ].nscannedObjectsAllPlans );
assert.eq( 3, explain.clauses[ 1 ].nscannedAllPlans );
assert.eq( 2, explain.n );

// These are computed by summing the values for each clause.
assert.eq( 2, explain.n );
assert.eq( 2, explain.nscannedObjects );
assert.eq( 3, explain.nscanned );
assert.eq( 4, explain.nscannedObjectsAllPlans );
assert.eq( 7, explain.nscannedAllPlans );

// A non $or case where nscanned != nscannedObjects.
t.remove();

t.save( { a:'0', b:'1' } );
t.save( { a:'1', b:'0' } );

explain = t.find( { a:/0/, b:/1/ } ).explain( true );

assert.eq( 'BtreeCursor a_1_b_1 multi', explain.cursor );
assert.eq( 1, explain.n );
assert.eq( 2, explain.nscanned );
assert.eq( 1, explain.nscannedObjects );
// Two results were scanned for each plan.
assert.eq( 6, explain.nscannedAllPlans );
// One result was generated by { a:1, b:1 }.  One result was matched by { b:1, a:1 } but it was a
// dup.  Two results were loaded for matching by the unindexed plan.
assert.eq( 3, explain.nscannedObjectsAllPlans );