Skip to content

Commit

Permalink
planner: introduce new cost formula for IndexLookup (pingcap#35408)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 committed Jul 12, 2022
1 parent c8596e7 commit ad37170
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions planner/core/plan_cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ func (p *PhysicalIndexLookUpReader) GetPlanCost(taskType property.TaskType, cost
for tmp = p.tablePlan; len(tmp.Children()) > 0; tmp = tmp.Children()[0] {
}
ts := tmp.(*PhysicalTableScan)
tblCost, err := ts.GetPlanCost(property.CopDoubleReadTaskType, costFlag)
if err != nil {
return 0, err
if p.ctx.GetSessionVars().CostModelVersion == modelVer1 {
tblCost, err := ts.GetPlanCost(property.CopDoubleReadTaskType, costFlag)
if err != nil {
return 0, err
}
p.planCost -= tblCost
p.planCost += getCardinality(p.indexPlan, costFlag) * ts.getScanRowSize() * p.SCtx().GetSessionVars().GetScanFactor(ts.Table)
}
p.planCost -= tblCost
p.planCost += getCardinality(p.indexPlan, costFlag) * ts.getScanRowSize() * p.SCtx().GetSessionVars().GetScanFactor(ts.Table)

// index-side net I/O cost: rows * row-size * net-factor
netFactor := getTableNetFactor(p.tablePlan)
Expand All @@ -221,6 +223,12 @@ func (p *PhysicalIndexLookUpReader) GetPlanCost(taskType property.TaskType, cost
// table-side seek cost
p.planCost += estimateNetSeekCost(p.tablePlan)

if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
// accumulate the real double-read cost: numDoubleReadTasks * seekFactor
numDoubleReadTasks := p.estNumDoubleReadTasks(costFlag)
p.planCost += numDoubleReadTasks * p.ctx.GetSessionVars().GetSeekFactor(ts.Table)
}

// consider concurrency
p.planCost /= float64(p.ctx.GetSessionVars().DistSQLScanConcurrency())

Expand All @@ -230,6 +238,16 @@ func (p *PhysicalIndexLookUpReader) GetPlanCost(taskType property.TaskType, cost
return p.planCost, nil
}

func (p *PhysicalIndexLookUpReader) estNumDoubleReadTasks(costFlag uint64) float64 {
doubleReadRows := p.indexPlan.StatsCount()
batchSize := float64(p.ctx.GetSessionVars().IndexLookupSize)
// distRatio indicates how many requests corresponding to a batch, current value is from experiments.
// TODO: estimate it by using index correlation or make it configurable.
distRatio := 40.0
numDoubleReadTasks := (doubleReadRows / batchSize) * distRatio
return numDoubleReadTasks // use Float64 instead of Int like `Ceil(...)` to make the cost continuous
}

// GetPlanCost calculates the cost of the plan if it has not been calculated yet and returns the cost.
func (p *PhysicalIndexReader) GetPlanCost(taskType property.TaskType, costFlag uint64) (float64, error) {
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
Expand Down

0 comments on commit ad37170

Please sign in to comment.