summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/plan_summary_stats_visitor.h
blob: 3476c49d147530966e44df863ed17e9a4d8c344b (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
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
/**
 *    Copyright (C) 2021-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.
 */

#pragma once

#include "mongo/db/exec/plan_stats_visitor.h"
#include "mongo/db/exec/sbe/stages/plan_stats.h"
#include "mongo/db/query/plan_summary_stats.h"

namespace mongo {
/**
 * Visitor for accumulating execution stats.
 */
class PlanSummaryStatsVisitor : public PlanStatsVisitorBase<true> {
public:
    // To avoid overloaded-virtual warnings.
    using PlanStatsConstVisitor::visit;

    explicit PlanSummaryStatsVisitor(PlanSummaryStats& summary) : _summary(summary) {}

    void visit(tree_walker::MaybeConstPtr<true, sbe::ScanStats> stats) override final {
        _summary.totalDocsExamined += stats->numReads;
    }
    void visit(tree_walker::MaybeConstPtr<true, sbe::ColumnScanStats> stats) override final {
        _summary.totalDocsExamined += stats->numRowStoreFetches + stats->numRowStoreScans;
        for (auto const& stat : stats->cursorStats)
            _summary.totalKeysExamined += stat.numNexts + stat.numSeeks;
        for (auto const& stat : stats->parentCursorStats)
            _summary.totalKeysExamined += stat.numNexts + stat.numSeeks;
    }
    void visit(tree_walker::MaybeConstPtr<true, sbe::IndexScanStats> stats) override final {
        _summary.totalKeysExamined += stats->keysExamined;
    }
    void visit(tree_walker::MaybeConstPtr<true, sbe::HashAggStats> stats) override final {
        _summary.usedDisk |= stats->spilledRecords > 0;
    }
    void visit(tree_walker::MaybeConstPtr<true, SortStats> stats) override final {
        _summary.hasSortStage = true;
        _summary.usedDisk = _summary.usedDisk || stats->spills > 0;
        _summary.sortSpills += stats->spills;
        _summary.sortTotalDataSizeBytes += stats->totalDataSizeBytes;
        _summary.keysSorted += stats->keysSorted;
    }
    void visit(tree_walker::MaybeConstPtr<true, GroupStats> stats) override final {
        _summary.usedDisk = _summary.usedDisk || stats->spills > 0;
    }
    void visit(tree_walker::MaybeConstPtr<true, DocumentSourceCursorStats> stats) override final {
        accumulate(stats->planSummaryStats);
    }
    void visit(tree_walker::MaybeConstPtr<true, DocumentSourceLookupStats> stats) override final {
        accumulate(stats->planSummaryStats);
    }
    void visit(tree_walker::MaybeConstPtr<true, UnionWithStats> stats) override final {
        accumulate(stats->planSummaryStats);
    }
    void visit(tree_walker::MaybeConstPtr<true, DocumentSourceFacetStats> stats) override final {
        accumulate(stats->planSummaryStats);
    }

private:
    /**
     * Helper method to accumulate the plan summary stats from the input source.
     */
    void accumulate(const PlanSummaryStats& statsIn) {
        // Attributes replanReason and fromMultiPlanner have been intentionally skipped as they
        // always describe the left-hand side (or "local") collection.
        // Consider $lookup case. $lookup runtime plan selection may happen against the foreign
        // collection an arbitrary number of times. A single value of 'replanReason' and
        // 'fromMultiPlanner' can't really report correctly on the behavior of arbitrarily many
        // occurrences of runtime planning for a single query.

        _summary.nReturned += statsIn.nReturned;
        _summary.totalKeysExamined += statsIn.totalKeysExamined;
        _summary.totalDocsExamined += statsIn.totalDocsExamined;
        _summary.collectionScans += statsIn.collectionScans;
        _summary.collectionScansNonTailable += statsIn.collectionScansNonTailable;
        _summary.hasSortStage |= statsIn.hasSortStage;
        _summary.usedDisk |= statsIn.usedDisk;
        _summary.sortSpills += statsIn.sortSpills;
        _summary.sortTotalDataSizeBytes += statsIn.sortTotalDataSizeBytes;
        _summary.keysSorted += statsIn.keysSorted;
        _summary.planFailed |= statsIn.planFailed;
        _summary.indexesUsed.insert(statsIn.indexesUsed.begin(), statsIn.indexesUsed.end());
    }

    PlanSummaryStats& _summary;
};
}  // namespace mongo