@@ -36,6 +36,7 @@ import (
36
36
const DefaultBlockMaxRows = 8192
37
37
const BlockNumForceOneCN = 200
38
38
const blockSelectivityThreshHold = 0.95
39
+ const blockNDVThreshHold = 100
39
40
const highNDVcolumnThreshHold = 0.95
40
41
41
42
// stats cache is small, no need to use LRU for now
@@ -544,10 +545,13 @@ func estimateFilterWeight(expr *plan.Expr, w float64) float64 {
544
545
}
545
546
546
547
// 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 {
548
549
if ! ExprIsZonemappable (ctx , expr ) {
549
550
return 1
550
551
}
552
+ if expr .Selectivity < 0.01 {
553
+ return expr .Selectivity * 90
554
+ }
551
555
col := extractColRefInFilter (expr )
552
556
if col != nil {
553
557
switch GetSortOrder (tableDef , col .Name ) {
@@ -559,11 +563,11 @@ func estimateFilterBlockSelectivity(ctx context.Context, expr *plan.Expr, tableD
559
563
return math .Min (expr .Selectivity * 10 , 0.9 )
560
564
}
561
565
}
562
- if tableCnt > 10000 {
563
- return 0.9
564
- } else {
566
+ if getExprNdv (expr , builder ) < blockNDVThreshHold {
565
567
return 1
566
568
}
569
+ // do not know selectivity for this expr, default 0.5
570
+ return 0.5
567
571
}
568
572
569
573
func rewriteFilterListByStats (ctx context.Context , nodeID int32 , builder * QueryBuilder ) {
@@ -993,7 +997,7 @@ func calcScanStats(node *plan.Node, builder *QueryBuilder) *plan.Stats {
993
997
var blockExprList []* plan.Expr
994
998
for i := range node .FilterList {
995
999
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 )
997
1001
if currentBlockSel < blockSelectivityThreshHold {
998
1002
copyOfExpr := DeepCopyExpr (node .FilterList [i ])
999
1003
copyOfExpr .Selectivity = currentBlockSel
0 commit comments