Skip to content

Commit

Permalink
opt: optimize FuncDepSet.ReduceCols
Browse files Browse the repository at this point in the history
`FuncDepSet.ReduceCols` is used frequently to manipulate `FuncDepSet`s.
In queries with many tables, columns, and joins, it can contribute
significantly to the latency of query planning, due to the `O(n²)` time
complexity of calculating a transitive closure (see
`FuncDepSet.inClosureOf`), where `n` is the number of dependencies in
the set, and the relatively expensive `SubsetOf` set operation performed
multiple times for each of the `n²` iterations.

This commit optimizes the `ReduceCols` by adding a fast path that can
more quickly determine if a column cannot be reduced by checking if it
exists in none of the `to` sets of the functional dependencies. This
avoids having to calculate the transitive closure in many cases,
significantly speeding up query planning time.

Release note (performance improvement): Query planning time has been
reduced significantly for some queries in which many tables are joined.
  • Loading branch information
mgartner committed Nov 2, 2023
1 parent 0df2406 commit 846fc7b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/sql/opt/props/func_dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,23 @@ func (f *FuncDepSet) ReduceCols(cols opt.ColSet) opt.ColSet {
var removed opt.ColSet
cols = cols.Copy()
for i, ok := cols.Next(0); ok; i, ok = cols.Next(i + 1) {
// First check if the column is present in any "to" set of a dependency.
// If not, then it is not redundant and must remain in the set. This is
// a fast-path to avoid the more expensive functional-dependency-closure
// test below, when possible.
inToSet := false
for j := range f.deps {
if f.deps[j].to.Contains(i) {
inToSet = true
break
}
}
if !inToSet {
continue
}

// Determine if the column is functionally determined by the other
// columns.
cols.Remove(i)
removed.Add(i)
if !f.inClosureOf(removed, cols, true /* strict */) {
Expand Down

0 comments on commit 846fc7b

Please sign in to comment.