-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathdistinct_plan_cache_md.js
105 lines (91 loc) · 3.15 KB
/
distinct_plan_cache_md.js
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
/**
* Tests DISTINCT_SCANs generated from multiplanning correctly utilize the plan cache.
*
* @tags: [
* featureFlagShardFilteringDistinctScan,
* requires_fcv_82
* ]
*/
import {section} from "jstests/libs/pretty_md.js";
import {
runDistinctAndOutputPlanCacheStats,
validateAggPlanCacheUse,
validateDistinctPlanCacheUse,
} from "jstests/libs/query/golden_test_utils.js";
const coll = db[jsTestName()];
{
section("Distinct command utilizes plan cache");
coll.drop();
coll.createIndex({x: 1, y: 1});
coll.createIndex({y: 1, x: 1});
coll.createIndex({x: 1, z: 1, y: 1});
coll.insertMany([
{x: 3, y: 5, z: 7},
{x: 5, y: 6, z: 5},
{x: 5, y: 5, z: 4},
{x: 6, y: 5, z: 3},
{x: 6, y: 5, z: 7},
{x: 7, y: 5, z: 8},
{x: 7, y: 5, z: 9},
{x: 8, y: 7, z: 3},
{x: 8, y: 8, z: 3},
{x: 8, y: 5, z: 5},
{x: 9, y: 5, z: 3},
{x: 9, y: 5, z: 4},
]);
validateDistinctPlanCacheUse(coll, "x", {x: {$gt: 3}, y: 5});
}
{
section("distinct() uses same plan cache entry with different predicate");
coll.drop();
for (let i = 0; i < 100; ++i) {
coll.insert({x: i % 2, y: i + 100, z: i + 200});
}
coll.createIndex({x: 1});
coll.createIndex({x: 1, y: 1});
coll.createIndex({y: 1, z: 1});
coll.createIndex({x: 1, y: 1, z: 1});
runDistinctAndOutputPlanCacheStats(coll, "x", {x: {$gt: 12}, y: {$lt: 200}});
runDistinctAndOutputPlanCacheStats(coll, "x", {x: {$gt: 12}, y: {$lt: 250}});
}
{
section("Prefer cached IXSCAN over DISTINCT_SCAN for no duplicate values in the collection");
coll.drop();
for (let i = 0; i < 100; ++i)
coll.insert({x: i, y: i + 100, z: i + 200});
coll.createIndex({x: 1});
coll.createIndex({x: 1, y: 1});
coll.createIndex({y: 1, z: 1});
validateDistinctPlanCacheUse(coll, "x", {x: {$gt: -1}, y: {$lt: 105}});
}
{
section("Aggregation DISTINCT_SCAN utilizes plan cache");
coll.drop();
coll.createIndex({a: 1});
coll.createIndex({b: 1});
coll.createIndex({a: 1, b: 1});
coll.createIndex({a: -1, b: 1});
coll.createIndex({a: 1, b: -1});
coll.createIndex({a: 1, b: 1, c: 1});
coll.createIndex({a: 1, b: 1, d: 1});
coll.createIndex({b: 1, a: 1});
coll.createIndex({b: 1, c: 1});
coll.createIndex({d: 1, c: -1});
coll.insertMany([
{_id: 1, a: 4, b: 2, c: 3, d: 4},
{_id: 2, a: 4, b: 3, c: 6, d: 5},
{_id: 3, a: 5, b: 4, c: 7, d: 5},
{a: 4, b: 2, c: 3},
{a: 4, b: 3, c: 6},
{a: 5, b: 4, c: 7, d: [1, 2, 3]}
]);
validateAggPlanCacheUse(coll,
[{$sort: {a: 1, b: 1}}, {$group: {_id: "$a", accum: {$first: "$b"}}}]);
validateAggPlanCacheUse(
coll, [{$group: {_id: "$a", accum: {$bottom: {sortBy: {a: -1, b: -1}, output: "$c"}}}}]);
section("DISTINCT_SCAN with embedded FETCH utilizes plan cache");
validateAggPlanCacheUse(
coll, [{$group: {_id: "$a", accum: {$top: {sortBy: {a: 1, b: 1}, output: "$c"}}}}]);
validateAggPlanCacheUse(
coll, [{$group: {_id: "$a", accum: {$bottom: {sortBy: {a: -1, b: -1}, output: "$c"}}}}]);
}