(function() {
|
"use strict";
|
|
const coll = db.sbe_replan;
|
coll.createIndex({selectiveKey: 1});
|
coll.createIndex({notSelectiveKey: 1});
|
|
for (let i = 0; i < 10; ++i) {
|
// Include one document which will not match the filter.
|
assert.commandWorked(coll.insert({notSelectiveKey: 55, selectiveKey: i}));
|
|
// If selectiveKey == 2 add one more document which doesn't match the filter.
|
// This will make the plan using the index on selectiveKey a *little* bit less
|
// 'productive', but not enough to warrant replanning. (< 1%).
|
if (i == 2) {
|
assert.commandWorked(coll.insert({notSelectiveKey: 55, selectiveKey: i}));
|
}
|
|
// Add a bunch of documents which do match the filter.
|
for (let j = 0; j < 110; ++j) {
|
assert.commandWorked(coll.insert({notSelectiveKey: 10, selectiveKey: i}));
|
}
|
}
|
|
// Summary:
|
// Possible values of selectiveKey: 0-9
|
// Possible values of notSelectiveKey: [10, 55]
|
|
// This should choose the plan using the index on 'selectiveKey'.
|
printjson(coll.find({notSelectiveKey: {$lt: 50}, selectiveKey: 3}).explain());
|
|
// Run some queries. The plan using the selectiveKey index should get cached.
|
print("Running initial query\n");
|
print(coll.find({notSelectiveKey: {$lt: 50}, selectiveKey: 3}).itcount());
|
print(coll.find({notSelectiveKey: {$lt: 50}, selectiveKey: 3}).itcount());
|
print(coll.find({notSelectiveKey: {$lt: 50}, selectiveKey: 3}).itcount());
|
|
// Sleep to avoid overlapping debug prints etc.
|
sleep(1000);
|
print("Running new query\n");
|
sleep(1000);
|
// Run a query with the special value of 'selectiveKey' which has one extra document that
|
// doesn't match the filter. The plan is indeed less 'efficient' but it's not
|
// doing anywhere close to 10x more reads compared to the original plan.
|
// This query gets replanned.
|
print(coll.find({notSelectiveKey: {$lt: 50}, selectiveKey: 2}).itcount());
|
})();
|