Skip to content

Commit c7748f7

Browse files
committed
incorporate index coverage adjustment into lookup join estimate
1 parent 86cbaca commit c7748f7

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

sql/memo/coster.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,16 @@ func (c *coster) costRel(ctx *sql.Context, n RelExpr, s sql.StatsProvider) (floa
131131
if n.Injective || matchRate == 0 {
132132
return lBest*seqIOCostFactor + lBest*(seqIOCostFactor+randIOCostFactor), nil
133133
}
134-
// Fanout is the average number of right-side rows that match each left-side row
135-
fanout := 1.5
134+
136135
// The total expected number of right row lookups
137-
expectedRightRows := selfJoinCard * matchRate * fanout
136+
expectedRightRows := selfJoinCard * matchRate
137+
138+
if expectedRightRows < lBest {
139+
return lBest*(seqIOCostFactor) + (lBest + indexCoverageAdjustment(n.Lookup)*(cpuCostFactor+randIOCostFactor)), nil
140+
}
141+
138142
// Estimate for reading each left row and each expected right row
139-
return lBest*seqIOCostFactor + expectedRightRows*(seqIOCostFactor+randIOCostFactor), nil
143+
return lBest*seqIOCostFactor + expectedRightRows*(cpuCostFactor+randIOCostFactor), nil
140144
case *ConcatJoin:
141145
return c.costConcatJoin(ctx, n, s)
142146
}

0 commit comments

Comments
 (0)