Skip to content

Commit

Permalink
Merge #138246
Browse files Browse the repository at this point in the history
138246: opt/exec: reduce allocations for counting join algorithms and types r=mgartner a=mgartner

Arrays embedded in `execbuilder.Builder` are now used instead of maps to
keep track of the count of join algorithms and types used in a query
plan. This reduces allocations and overhead of using a hash map.

We now only track up to 255 of each algorithm and type. This seems like
a suitable limitation because it should be extremely rare for a query to
have so many joins and these metrics don't need to be completely
accurate - they are meant to provide a rough picture of how common
each join algorithm and type is in practice.

Epic: None

Release note: None


Co-authored-by: Marcus Gartner <[email protected]>
  • Loading branch information
craig[bot] and mgartner committed Jan 4, 2025
2 parents 35903af + 9a903a3 commit 848f485
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
11 changes: 5 additions & 6 deletions pkg/sql/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/execstats"
Expand Down Expand Up @@ -215,12 +214,12 @@ type instrumentationHelper struct {
nanosSinceStatsForecasted time.Duration

// joinTypeCounts records the number of times each type of logical join was
// used in the query.
joinTypeCounts map[descpb.JoinType]int
// used in the query, up to 255.
joinTypeCounts [execbuilder.NumRecordedJoinTypes]uint8

// joinAlgorithmCounts records the number of times each type of join algorithm
// was used in the query.
joinAlgorithmCounts map[exec.JoinAlgorithm]int
// joinAlgorithmCounts records the number of times each type of join
// algorithm was used in the query, up to 255.
joinAlgorithmCounts [exec.NumJoinAlgorithms]uint8

// scanCounts records the number of times scans were used in the query.
scanCounts [exec.NumScanCountTypes]int
Expand Down
19 changes: 12 additions & 7 deletions pkg/sql/opt/exec/execbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strconv"
"time"

"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec"
Expand All @@ -38,7 +37,13 @@ var parallelScanResultThreshold = uint64(metamorphic.ConstantWithTestRange(
parallelScanResultThresholdProductionValue, /* max */
))

const parallelScanResultThresholdProductionValue = 10000
const (
parallelScanResultThresholdProductionValue = 10000

// numJoinTypes includes all join types except for descpb.RightSemiJoin and
// descpb.RightAntiJoin, which are recorded as left joins.
NumRecordedJoinTypes = 8
)

func getParallelScanResultThreshold(forceProductionValue bool) uint64 {
if forceProductionValue {
Expand Down Expand Up @@ -154,12 +159,12 @@ type Builder struct {
NanosSinceStatsForecasted time.Duration

// JoinTypeCounts records the number of times each type of logical join was
// used in the query.
JoinTypeCounts map[descpb.JoinType]int
// used in the query, up to 255.
JoinTypeCounts [NumRecordedJoinTypes]uint8

// JoinAlgorithmCounts records the number of times each type of join algorithm
// was used in the query.
JoinAlgorithmCounts map[exec.JoinAlgorithm]int
// JoinAlgorithmCounts records the number of times each type of join
// algorithm was used in the query, up to 255.
JoinAlgorithmCounts [exec.NumJoinAlgorithms]uint8

// ScanCounts records the number of times scans were used in the query.
ScanCounts [exec.NumScanCountTypes]int
Expand Down
13 changes: 5 additions & 8 deletions pkg/sql/opt/exec/execbuilder/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -3948,10 +3948,6 @@ func (b *Builder) statementTag(expr memo.RelExpr) string {
// recordJoinType increments the counter for the given join type for telemetry
// reporting.
func (b *Builder) recordJoinType(joinType descpb.JoinType) {
if b.JoinTypeCounts == nil {
const numJoinTypes = 7
b.JoinTypeCounts = make(map[descpb.JoinType]int, numJoinTypes)
}
// Don't bother distinguishing between left and right.
switch joinType {
case descpb.RightOuterJoin:
Expand All @@ -3961,16 +3957,17 @@ func (b *Builder) recordJoinType(joinType descpb.JoinType) {
case descpb.RightAntiJoin:
joinType = descpb.LeftAntiJoin
}
b.JoinTypeCounts[joinType]++
if b.JoinTypeCounts[joinType]+1 > b.JoinTypeCounts[joinType] {
b.JoinTypeCounts[joinType]++
}
}

// recordJoinAlgorithm increments the counter for the given join algorithm for
// telemetry reporting.
func (b *Builder) recordJoinAlgorithm(joinAlgorithm exec.JoinAlgorithm) {
if b.JoinAlgorithmCounts == nil {
b.JoinAlgorithmCounts = make(map[exec.JoinAlgorithm]int, exec.NumJoinAlgorithms)
if b.JoinAlgorithmCounts[joinAlgorithm]+1 > b.JoinAlgorithmCounts[joinAlgorithm] {
b.JoinAlgorithmCounts[joinAlgorithm]++
}
b.JoinAlgorithmCounts[joinAlgorithm]++
}

// boundedStalenessAllowList contains the operators that may be used with
Expand Down

0 comments on commit 848f485

Please sign in to comment.