Skip to content

Commit

Permalink
planner: introduce a new variable to adjust the penalty double read c…
Browse files Browse the repository at this point in the history
…ost of IndexJoin (#40573)

close #40556
  • Loading branch information
qw4990 authored Jan 15, 2023
1 parent ee5d6a1 commit d6f633c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion planner/core/plan_cost_ver2.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ func (p *PhysicalIndexJoin) getIndexJoinCostVer2(taskType property.TaskType, opt
probeConcurrency := float64(p.ctx.GetSessionVars().IndexLookupJoinConcurrency())
cpuFactor := getTaskCPUFactorVer2(p, taskType)
memFactor := getTaskMemFactorVer2(p, taskType)
requestFactor := getTaskRequestFactorVer2(p, taskType)

buildFilterCost := filterCostVer2(option, buildRows, buildFilters, cpuFactor)
buildChildCost, err := build.getPlanCostVer2(taskType, option)
Expand Down Expand Up @@ -595,7 +596,18 @@ func (p *PhysicalIndexJoin) getIndexJoinCostVer2(taskType property.TaskType, opt
batchRatio := 6.0
probeCost := divCostVer2(mulCostVer2(probeChildCost, buildRows), batchRatio)

p.planCostVer2 = sumCostVer2(startCost, buildChildCost, buildFilterCost, buildTaskCost, divCostVer2(sumCostVer2(probeCost, probeFilterCost, hashTableCost), probeConcurrency))
// Double Read Cost
doubleReadCost := newZeroCostVer2(traceCost(option))
if p.ctx.GetSessionVars().IndexJoinDoubleReadPenaltyCostRate > 0 &&
buildRows > 10000 { // ignore double-read costs for small IndexJoin
batchSize := float64(p.ctx.GetSessionVars().IndexJoinBatchSize)
taskPerBatch := 1024.0 // TODO: remove this magic number
doubleReadTasks := buildRows / batchSize * taskPerBatch
doubleReadCost = doubleReadCostVer2(option, doubleReadTasks, requestFactor)
doubleReadCost = mulCostVer2(doubleReadCost, p.ctx.GetSessionVars().IndexJoinDoubleReadPenaltyCostRate)
}

p.planCostVer2 = sumCostVer2(startCost, buildChildCost, buildFilterCost, buildTaskCost, divCostVer2(sumCostVer2(doubleReadCost, probeCost, probeFilterCost, hashTableCost), probeConcurrency))
p.planCostInit = true
return p.planCostVer2, nil
}
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,9 @@ type SessionVars struct {
EnableNewCostInterface bool
// CostModelVersion is a internal switch to indicates the Cost Model Version.
CostModelVersion int
// IndexJoinDoubleReadPenaltyCostRate indicates whether to add some penalty cost to IndexJoin and how much of it.
IndexJoinDoubleReadPenaltyCostRate float64

// BatchPendingTiFlashCount shows the threshold of pending TiFlash tables when batch adding.
BatchPendingTiFlashCount int
// RcWriteCheckTS indicates whether some special write statements don't get latest tso from PD at RC
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,12 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBIndexJoinDoubleReadPenaltyCostRate, Value: strconv.Itoa(1), Hidden: false, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64,
SetSession: func(vars *SessionVars, s string) error {
vars.IndexJoinDoubleReadPenaltyCostRate = tidbOptFloat64(s, 0)
return nil
},
},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBRCWriteCheckTs, Type: TypeBool, Value: BoolToOnOff(DefTiDBRcWriteCheckTs), SetSession: func(s *SessionVars, val string) error {
s.RcWriteCheckTS = TiDBOptOn(val)
return nil
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,12 @@ const (
// TiDBCostModelVersion is a internal switch to indicates the cost model version.
TiDBCostModelVersion = "tidb_cost_model_version"

// TiDBIndexJoinDoubleReadPenaltyCostRate indicates whether to add some penalty cost to IndexJoin and how much of it.
// IndexJoin can cause plenty of extra double read tasks, which consume lots of resources and take a long time.
// Since the number of double read tasks is hard to estimated accurately, we leave this variable to let us can adjust this
// part of cost manually.
TiDBIndexJoinDoubleReadPenaltyCostRate = "tidb_index_join_double_read_penalty_cost_rate"

// TiDBBatchPendingTiFlashCount indicates the maximum count of non-available TiFlash tables.
TiDBBatchPendingTiFlashCount = "tidb_batch_pending_tiflash_count"

Expand Down

0 comments on commit d6f633c

Please sign in to comment.