Skip to content

Commit 95801d8

Browse files
authored
fix stats for block filters (#15532)
fix stats for block filters Approved by: @ouyuanning, @sukki37
1 parent 4eeb015 commit 95801d8

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

pkg/sql/plan/stats.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
const DefaultBlockMaxRows = 8192
3737
const BlockNumForceOneCN = 200
3838
const blockSelectivityThreshHold = 0.95
39+
const blockNDVThreshHold = 100
3940
const highNDVcolumnThreshHold = 0.95
4041

4142
// stats cache is small, no need to use LRU for now
@@ -544,10 +545,13 @@ func estimateFilterWeight(expr *plan.Expr, w float64) float64 {
544545
}
545546

546547
// harsh estimate of block selectivity, will improve it in the future
547-
func estimateFilterBlockSelectivity(ctx context.Context, expr *plan.Expr, tableDef *plan.TableDef, tableCnt float64) float64 {
548+
func estimateFilterBlockSelectivity(ctx context.Context, expr *plan.Expr, tableDef *plan.TableDef, builder *QueryBuilder) float64 {
548549
if !ExprIsZonemappable(ctx, expr) {
549550
return 1
550551
}
552+
if expr.Selectivity < 0.01 {
553+
return expr.Selectivity * 90
554+
}
551555
col := extractColRefInFilter(expr)
552556
if col != nil {
553557
switch GetSortOrder(tableDef, col.Name) {
@@ -559,11 +563,11 @@ func estimateFilterBlockSelectivity(ctx context.Context, expr *plan.Expr, tableD
559563
return math.Min(expr.Selectivity*10, 0.9)
560564
}
561565
}
562-
if tableCnt > 10000 {
563-
return 0.9
564-
} else {
566+
if getExprNdv(expr, builder) < blockNDVThreshHold {
565567
return 1
566568
}
569+
// do not know selectivity for this expr, default 0.5
570+
return 0.5
567571
}
568572

569573
func rewriteFilterListByStats(ctx context.Context, nodeID int32, builder *QueryBuilder) {
@@ -993,7 +997,7 @@ func calcScanStats(node *plan.Node, builder *QueryBuilder) *plan.Stats {
993997
var blockExprList []*plan.Expr
994998
for i := range node.FilterList {
995999
node.FilterList[i].Selectivity = estimateExprSelectivity(node.FilterList[i], builder)
996-
currentBlockSel := estimateFilterBlockSelectivity(builder.GetContext(), node.FilterList[i], node.TableDef, stats.TableCnt)
1000+
currentBlockSel := estimateFilterBlockSelectivity(builder.GetContext(), node.FilterList[i], node.TableDef, builder)
9971001
if currentBlockSel < blockSelectivityThreshHold {
9981002
copyOfExpr := DeepCopyExpr(node.FilterList[i])
9991003
copyOfExpr.Selectivity = currentBlockSel

0 commit comments

Comments
 (0)