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
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kQuery
#include "mongo/platform/basic.h"
#include <algorithm>
#include <cmath>
#include <utility>
#include <vector>
#include "mongo/db/query/plan_ranker.h"
#include "mongo/db/exec/plan_stage.h"
#include "mongo/db/exec/working_set.h"
#include "mongo/db/query/explain.h"
#include "mongo/db/query/query_knobs_gen.h"
#include "mongo/db/query/query_solution.h"
#include "mongo/db/server_options.h"
#include "mongo/util/log.h"
namespace {
/**
* Comparator for (scores, candidateIndex) in pickBestPlan().
*/
bool scoreComparator(const std::pair<double, size_t>& lhs, const std::pair<double, size_t>& rhs) {
// Just compare score in lhs.first and rhs.first;
// Ignore candidate array index in lhs.second and rhs.second.
return lhs.first > rhs.first;
}
} // namespace
namespace mongo {
using std::endl;
using std::vector;
// static
StatusWith<std::unique_ptr<PlanRankingDecision>> PlanRanker::pickBestPlan(
const vector<CandidatePlan>& candidates) {
invariant(!candidates.empty());
// A plan that hits EOF is automatically scored above
// its peers. If multiple plans hit EOF during the same
// set of round-robin calls to work(), then all such plans
// receive the bonus.
double eofBonus = 1.0;
// Each plan will have a stat tree.
std::vector<std::unique_ptr<PlanStageStats>> statTrees;
// Get stat trees from each plan.
// Copy stats trees instead of transferring ownership
// because multi plan runner will need its own stats
// trees for explain.
for (size_t i = 0; i < candidates.size(); ++i) {
statTrees.push_back(candidates[i].root->getStats());
}
// Holds (score, candidateInndex).
// Used to derive scores and candidate ordering.
vector<std::pair<double, size_t>> scoresAndCandidateindices;
vector<size_t> failed;
// Compute score for each tree. Record the best.
for (size_t i = 0; i < statTrees.size(); ++i) {
if (!candidates[i].failed) {
LOG(5) << "Scoring plan " << i << ":" << endl
<< redact(candidates[i].solution->toString()) << "Stats:\n"
<< redact(Explain::statsToBSON(*statTrees[i]).jsonString(Strict, true));
LOG(2) << "Scoring query plan: " << Explain::getPlanSummary(candidates[i].root)
<< " planHitEOF=" << statTrees[i]->common.isEOF;
double score = scoreTree(statTrees[i].get());
LOG(5) << "score = " << score;
if (statTrees[i]->common.isEOF) {
LOG(5) << "Adding +" << eofBonus << " EOF bonus to score.";
score += 1;
}
scoresAndCandidateindices.push_back(std::make_pair(score, i));
} else {
failed.push_back(i);
LOG(2) << "Not scording plan: " << Explain::getPlanSummary(candidates[i].root)
<< " because the plan failed.";
}
}
// If there isn't a viable plan we should error.
if (scoresAndCandidateindices.size() == 0U) {
return {ErrorCodes::Error(31157),
"No viable plan was found because all candidate plans failed."};
}
// Sort (scores, candidateIndex). Get best child and populate candidate ordering.
std::stable_sort(
scoresAndCandidateindices.begin(), scoresAndCandidateindices.end(), scoreComparator);
auto why = std::make_unique<PlanRankingDecision>();
// Determine whether plans tied for the win.
if (scoresAndCandidateindices.size() > 1U) {
double bestScore = scoresAndCandidateindices[0].first;
double runnerUpScore = scoresAndCandidateindices[1].first;
const double epsilon = 1e-10;
why->tieForBest = std::abs(bestScore - runnerUpScore) < epsilon;
}
// Update results in 'why'
// Stats and scores in 'why' are sorted in descending order by score.
why->stats.clear();
why->scores.clear();
why->candidateOrder.clear();
why->failedCandidates = std::move(failed);
for (size_t i = 0; i < scoresAndCandidateindices.size(); ++i) {
double score = scoresAndCandidateindices[i].first;
size_t candidateIndex = scoresAndCandidateindices[i].second;
// We shouldn't cache the scores with the EOF bonus included,
// as this is just a tie-breaking measure for plan selection.
// Plans not run through the multi plan runner will not receive
// the bonus.
//
// An example of a bad thing that could happen if we stored scores
// with the EOF bonus included:
//
// Let's say Plan A hits EOF, is the highest ranking plan, and gets
// cached as such. On subsequent runs it will not receive the bonus.
// Eventually the plan cache feedback mechanism will evict the cache
// entry---the scores will appear to have fallen due to the missing
// EOF bonus.
//
// This begs the question, why don't we include the EOF bonus in
// scoring of cached plans as well? The problem here is that the cached
// plan runner always runs plans to completion before scoring. Queries
// that don't get the bonus in the multi plan runner might get the bonus
// after being run from the plan cache.
if (statTrees[candidateIndex]->common.isEOF) {
score -= eofBonus;
}
why->stats.push_back(std::move(statTrees[candidateIndex]));
why->scores.push_back(score);
why->candidateOrder.push_back(candidateIndex);
}
for (auto& i : why->failedCandidates) {
why->stats.push_back(std::move(statTrees[i]));
}
return StatusWith<std::unique_ptr<PlanRankingDecision>>(std::move(why));
}
// TODO: Move this out. This is a signal for ranking but will become its own complicated
// stats-collecting beast.
double computeSelectivity(const PlanStageStats* stats) {
if (STAGE_IXSCAN == stats->stageType) {
IndexScanStats* iss = static_cast<IndexScanStats*>(stats->specific.get());
return iss->keyPattern.nFields();
} else {
double sum = 0;
for (size_t i = 0; i < stats->children.size(); ++i) {
sum += computeSelectivity(stats->children[i].get());
}
return sum;
}
}
bool hasStage(const StageType type, const PlanStageStats* stats) {
if (type == stats->stageType) {
return true;
}
for (size_t i = 0; i < stats->children.size(); ++i) {
if (hasStage(type, stats->children[i].get())) {
return true;
}
}
return false;
}
// static
double PlanRanker::scoreTree(const PlanStageStats* stats) {
// We start all scores at 1. Our "no plan selected" score is 0 and we want all plans to
// be greater than that.
double baseScore = 1;
// How many "units of work" did the plan perform. Each call to work(...)
// counts as one unit.
size_t workUnits = stats->common.works;
invariant(workUnits != 0);
// How much did a plan produce?
// Range: [0, 1]
double productivity =
static_cast<double>(stats->common.advanced) / static_cast<double>(workUnits);
// Just enough to break a tie. Must be small enough to ensure that a more productive
// plan doesn't lose to a less productive plan due to tie breaking.
const double epsilon = std::min(1.0 / static_cast<double>(10 * workUnits), 1e-4);
// We prefer covered projections.
//
// We only do this when we have a projection stage because we have so many jstests that
// check bounds even when a collscan plan is just as good as the ixscan'd plan :(
double noFetchBonus = epsilon;
if ((hasStage(STAGE_PROJECTION_DEFAULT, stats) || hasStage(STAGE_PROJECTION_COVERED, stats) ||
hasStage(STAGE_PROJECTION_SIMPLE, stats)) &&
hasStage(STAGE_FETCH, stats)) {
noFetchBonus = 0;
}
// In the case of ties, prefer solutions without a blocking sort
// to solutions with a blocking sort.
double noSortBonus = epsilon;
if (hasStage(STAGE_SORT, stats)) {
noSortBonus = 0;
}
// In the case of ties, prefer single index solutions to ixisect. Index
// intersection solutions are often slower than single-index solutions
// because they require examining a superset of index keys that would be
// examined by a single index scan.
//
// On the other hand, index intersection solutions examine the same
// number or fewer of documents. In the case that index intersection
// allows us to examine fewer documents, the penalty given to ixisect
// can be made up via the no fetch bonus.
double noIxisectBonus = epsilon;
if (hasStage(STAGE_AND_HASH, stats) || hasStage(STAGE_AND_SORTED, stats)) {
noIxisectBonus = 0;
}
double tieBreakers = noFetchBonus + noSortBonus + noIxisectBonus;
double score = baseScore + productivity + tieBreakers;
StringBuilder sb;
sb << "score(" << str::convertDoubleToString(score) << ") = baseScore("
<< str::convertDoubleToString(baseScore) << ")"
<< " + productivity((" << stats->common.advanced << " advanced)/(" << stats->common.works
<< " works) = " << str::convertDoubleToString(productivity) << ")"
<< " + tieBreakers(" << str::convertDoubleToString(noFetchBonus) << " noFetchBonus + "
<< str::convertDoubleToString(noSortBonus) << " noSortBonus + "
<< str::convertDoubleToString(noIxisectBonus)
<< " noIxisectBonus = " << str::convertDoubleToString(tieBreakers) << ")";
LOG(2) << sb.str();
if (internalQueryForceIntersectionPlans.load()) {
if (hasStage(STAGE_AND_HASH, stats) || hasStage(STAGE_AND_SORTED, stats)) {
// The boost should be >2.001 to make absolutely sure the ixisect plan will win due
// to the combination of 1) productivity, 2) eof bonus, and 3) no ixisect bonus.
score += 3;
LOG(5) << "Score boosted to " << score << " due to intersection forcing.";
}
}
return score;
}
} // namespace mongo
|