Skip to content

Commit

Permalink
sql: incorporate lookupExpr cpu cost into cost model
Browse files Browse the repository at this point in the history
Release note (sql): improve cost model to include cpu cost of lookupExprs
used by lookup joins.
  • Loading branch information
cucaroach committed Jun 25, 2021
1 parent 63ac8bd commit da760c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 16 additions & 4 deletions pkg/sql/opt/xform/coster.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ func (c *coster) computeMergeJoinCost(join *memo.MergeJoinExpr) memo.Cost {
func (c *coster) computeIndexJoinCost(
join *memo.IndexJoinExpr, required *physical.Required,
) memo.Cost {
return c.computeIndexLookupJoinCost(
cost, _ := c.computeIndexLookupJoinCost(
join,
required,
true, /* lookupColsAreTableKey */
Expand All @@ -771,6 +771,7 @@ func (c *coster) computeIndexJoinCost(
memo.JoinFlags(0),
false, /* localityOptimized */
)
return cost
}

func (c *coster) computeLookupJoinCost(
Expand All @@ -779,7 +780,8 @@ func (c *coster) computeLookupJoinCost(
if join.LookupJoinPrivate.Flags.Has(memo.DisallowLookupJoinIntoRight) {
return hugeCost
}
return c.computeIndexLookupJoinCost(

cost, lookupCount := c.computeIndexLookupJoinCost(
join,
required,
join.LookupColsAreTableKey,
Expand All @@ -790,6 +792,16 @@ func (c *coster) computeLookupJoinCost(
join.Flags,
join.LocalityOptimized,
)

// If we have extra lookupExprs beyond whats in the on conditions add those
// as an extra cpu cost since the joinreader machinery isn't free.
extraLookupJoinConditions := join.LookupExpr.Difference(join.On)
if len(extraLookupJoinConditions) > 0 {
extraCost := memo.Cost(lookupCount) * cpuCostFactor * memo.Cost(extraLookupJoinConditions.ChildCount())
cost += extraCost
}

return cost
}

func (c *coster) computeIndexLookupJoinCost(
Expand All @@ -802,7 +814,7 @@ func (c *coster) computeIndexLookupJoinCost(
index cat.IndexOrdinal,
flags memo.JoinFlags,
localityOptimized bool,
) memo.Cost {
) (memo.Cost, float64) {
input := join.Child(0).(memo.RelExpr)
lookupCount := input.Relational().Stats.RowCount

Expand Down Expand Up @@ -880,7 +892,7 @@ func (c *coster) computeIndexLookupJoinCost(
if localityOptimized {
cost /= 2.5
}
return cost
return cost, lookupCount
}

func (c *coster) computeInvertedJoinCost(
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/xform/testdata/coster/zone
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ anti-join (lookup abc_part@bc_idx [as=a2])
│ └── a2.r:7 = 'west' [outer=(7), constraints=(/7: [/'west' - /'west']; tight), fd=()-->(7)]
├── cardinality: [0 - 1]
├── stats: [rows=1e-10]
├── cost: 18.0617898
├── cost: 18.0980078
├── key: ()
├── fd: ()-->(1-4)
├── anti-join (lookup abc_part@bc_idx [as=a2])
Expand All @@ -763,7 +763,7 @@ anti-join (lookup abc_part@bc_idx [as=a2])
│ │ └── a2.r:7 = 'east' [outer=(7), constraints=(/7: [/'east' - /'east']; tight), fd=()-->(7)]
│ ├── cardinality: [0 - 1]
│ ├── stats: [rows=0.900900001, distinct(1)=0.89738934, null(1)=0, distinct(2)=0.900900001, null(2)=0, distinct(3)=0.900900001, null(3)=0, distinct(4)=0.900900001, null(4)=0]
│ ├── cost: 10.7686007
│ ├── cost: 10.7868007
│ ├── key: ()
│ ├── fd: ()-->(1-4)
│ ├── locality-optimized-search
Expand Down

0 comments on commit da760c3

Please sign in to comment.