diff options
Diffstat (limited to 'src/mongo/db/query/plan_ranker.h')
-rw-r--r-- | src/mongo/db/query/plan_ranker.h | 135 |
1 files changed, 67 insertions, 68 deletions
diff --git a/src/mongo/db/query/plan_ranker.h b/src/mongo/db/query/plan_ranker.h index ed41c0c1c5a..653fb332f12 100644 --- a/src/mongo/db/query/plan_ranker.h +++ b/src/mongo/db/query/plan_ranker.h @@ -39,86 +39,85 @@ namespace mongo { - struct CandidatePlan; - struct PlanRankingDecision; +struct CandidatePlan; +struct PlanRankingDecision; +/** + * Ranks 2 or more plans. + */ +class PlanRanker { +public: /** - * Ranks 2 or more plans. + * Returns index in 'candidates' of which plan is best. + * Populates 'why' with information relevant to how each plan fared in the ranking process. + * Caller owns pointers in 'why'. + * 'candidateOrder' holds indices into candidates ordered by score (winner in first element). */ - class PlanRanker { - public: - /** - * Returns index in 'candidates' of which plan is best. - * Populates 'why' with information relevant to how each plan fared in the ranking process. - * Caller owns pointers in 'why'. - * 'candidateOrder' holds indices into candidates ordered by score (winner in first element). - */ - static size_t pickBestPlan(const std::vector<CandidatePlan>& candidates, - PlanRankingDecision* why); - - /** - * Assign the stats tree a 'goodness' score. The higher the score, the better - * the plan. The exact value isn't meaningful except for imposing a ranking. - */ - static double scoreTree(const PlanStageStats* stats); - }; + static size_t pickBestPlan(const std::vector<CandidatePlan>& candidates, + PlanRankingDecision* why); /** - * A container holding one to-be-ranked plan and its associated/relevant data. - * Does not own any of its pointers. + * Assign the stats tree a 'goodness' score. The higher the score, the better + * the plan. The exact value isn't meaningful except for imposing a ranking. */ - struct CandidatePlan { - CandidatePlan(QuerySolution* s, PlanStage* r, WorkingSet* w) - : solution(s), root(r), ws(w), failed(false) { } + static double scoreTree(const PlanStageStats* stats); +}; + +/** + * A container holding one to-be-ranked plan and its associated/relevant data. + * Does not own any of its pointers. + */ +struct CandidatePlan { + CandidatePlan(QuerySolution* s, PlanStage* r, WorkingSet* w) + : solution(s), root(r), ws(w), failed(false) {} - QuerySolution* solution; - PlanStage* root; - WorkingSet* ws; + QuerySolution* solution; + PlanStage* root; + WorkingSet* ws; - // Any results produced during the plan's execution prior to ranking are retained here. - std::list<WorkingSetID> results; + // Any results produced during the plan's execution prior to ranking are retained here. + std::list<WorkingSetID> results; - bool failed; - }; + bool failed; +}; + +/** + * Information about why a plan was picked to be the best. Data here is placed into the cache + * and used to compare expected performance with actual. + */ +struct PlanRankingDecision { + PlanRankingDecision() {} /** - * Information about why a plan was picked to be the best. Data here is placed into the cache - * and used to compare expected performance with actual. + * Make a deep copy. */ - struct PlanRankingDecision { - - PlanRankingDecision() { } - - /** - * Make a deep copy. - */ - PlanRankingDecision* clone() const { - PlanRankingDecision* decision = new PlanRankingDecision(); - for (size_t i = 0; i < stats.size(); ++i) { - PlanStageStats* s = stats.vector()[i]; - invariant(s); - decision->stats.mutableVector().push_back(s->clone()); - } - decision->scores = scores; - decision->candidateOrder = candidateOrder; - return decision; + PlanRankingDecision* clone() const { + PlanRankingDecision* decision = new PlanRankingDecision(); + for (size_t i = 0; i < stats.size(); ++i) { + PlanStageStats* s = stats.vector()[i]; + invariant(s); + decision->stats.mutableVector().push_back(s->clone()); } - - // Stats of all plans sorted in descending order by score. - // Owned by us. - OwnedPointerVector<PlanStageStats> stats; - - // The "goodness" score corresponding to 'stats'. - // Sorted in descending order. - std::vector<double> scores; - - // Ordering of original plans in descending of score. - // Filled in by PlanRanker::pickBestPlan(candidates, ...) - // so that candidates[candidateOrder[0]] refers to the best plan - // with corresponding cores[0] and stats[0]. Runner-up would be - // candidates[candidateOrder[1]] followed by - // candidates[candidateOrder[2]], ... - std::vector<size_t> candidateOrder; - }; + decision->scores = scores; + decision->candidateOrder = candidateOrder; + return decision; + } + + // Stats of all plans sorted in descending order by score. + // Owned by us. + OwnedPointerVector<PlanStageStats> stats; + + // The "goodness" score corresponding to 'stats'. + // Sorted in descending order. + std::vector<double> scores; + + // Ordering of original plans in descending of score. + // Filled in by PlanRanker::pickBestPlan(candidates, ...) + // so that candidates[candidateOrder[0]] refers to the best plan + // with corresponding cores[0] and stats[0]. Runner-up would be + // candidates[candidateOrder[1]] followed by + // candidates[candidateOrder[2]], ... + std::vector<size_t> candidateOrder; +}; } // namespace mongo |