Skip to content

Commit

Permalink
planner: separate cost model ver1/ver2 into different files (part 2) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Sep 30, 2022
1 parent d70b022 commit ee8ebcb
Show file tree
Hide file tree
Showing 3 changed files with 469 additions and 25 deletions.
44 changes: 36 additions & 8 deletions planner/core/plan_cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,9 @@ func (p *PhysicalIndexJoin) GetPlanCost(taskType property.TaskType, option *Plan
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
outerChild, innerChild := p.children[1-p.InnerChildIdx], p.children[p.InnerChildIdx]
outerCost, err := outerChild.GetPlanCost(taskType, option)
if err != nil {
Expand Down Expand Up @@ -658,6 +661,9 @@ func (p *PhysicalIndexHashJoin) GetPlanCost(taskType property.TaskType, option *
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
outerChild, innerChild := p.children[1-p.InnerChildIdx], p.children[p.InnerChildIdx]
outerCost, err := outerChild.GetPlanCost(taskType, option)
if err != nil {
Expand Down Expand Up @@ -749,6 +755,9 @@ func (p *PhysicalIndexMergeJoin) GetPlanCost(taskType property.TaskType, option
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
outerChild, innerChild := p.children[1-p.InnerChildIdx], p.children[p.InnerChildIdx]
outerCost, err := outerChild.GetPlanCost(taskType, option)
if err != nil {
Expand Down Expand Up @@ -802,6 +811,9 @@ func (p *PhysicalApply) GetPlanCost(taskType property.TaskType, option *PlanCost
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
outerChild, innerChild := p.children[1-p.InnerChildIdx], p.children[p.InnerChildIdx]
outerCost, err := outerChild.GetPlanCost(taskType, option)
if err != nil {
Expand Down Expand Up @@ -876,6 +888,9 @@ func (p *PhysicalMergeJoin) GetPlanCost(taskType property.TaskType, option *Plan
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
p.planCost = 0
for _, child := range p.children {
childCost, err := child.GetPlanCost(taskType, option)
Expand Down Expand Up @@ -994,6 +1009,9 @@ func (p *PhysicalHashJoin) GetPlanCost(taskType property.TaskType, option *PlanC
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
p.planCost = 0
for _, child := range p.children {
childCost, err := child.GetPlanCost(taskType, option)
Expand Down Expand Up @@ -1036,6 +1054,9 @@ func (p *PhysicalStreamAgg) GetPlanCost(taskType property.TaskType, option *Plan
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
childCost, err := p.children[0].GetPlanCost(taskType, option)
if err != nil {
return 0, err
Expand Down Expand Up @@ -1084,6 +1105,9 @@ func (p *PhysicalHashAgg) GetPlanCost(taskType property.TaskType, option *PlanCo
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
childCost, err := p.children[0].GetPlanCost(taskType, option)
if err != nil {
return 0, err
Expand Down Expand Up @@ -1216,11 +1240,14 @@ func (p *BatchPointGetPlan) GetCost(opt *physicalOptimizeOp) float64 {
}

// GetPlanCost calculates the cost of the plan if it has not been calculated yet and returns the cost.
func (p *BatchPointGetPlan) GetPlanCost(_ property.TaskType, option *PlanCostOption) (float64, error) {
func (p *BatchPointGetPlan) GetPlanCost(taskType property.TaskType, option *PlanCostOption) (float64, error) {
costFlag := option.CostFlag
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx != nil && p.ctx.GetSessionVars() != nil && p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
p.planCost = p.GetCost(option.tracer)
p.planCostInit = true
return p.planCost, nil
Expand Down Expand Up @@ -1264,11 +1291,14 @@ func (p *PointGetPlan) GetCost(opt *physicalOptimizeOp) float64 {
}

// GetPlanCost calculates the cost of the plan if it has not been calculated yet and returns the cost.
func (p *PointGetPlan) GetPlanCost(_ property.TaskType, option *PlanCostOption) (float64, error) {
func (p *PointGetPlan) GetPlanCost(taskType property.TaskType, option *PlanCostOption) (float64, error) {
costFlag := option.CostFlag
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx != nil && p.ctx.GetSessionVars() != nil && p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
p.planCost = p.GetCost(option.tracer)
p.planCostInit = true
return p.planCost, nil
Expand Down Expand Up @@ -1311,18 +1341,16 @@ func (p *PhysicalExchangeReceiver) GetPlanCost(taskType property.TaskType, optio
if p.planCostInit && !hasCostFlag(costFlag, CostFlagRecalculate) {
return p.planCost, nil
}
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
return p.getPlanCostVer2(taskType, option)
}
childCost, err := p.children[0].GetPlanCost(taskType, option)
if err != nil {
return 0, err
}
p.planCost = childCost
// accumulate net cost
if p.ctx.GetSessionVars().CostModelVersion == modelVer1 {
p.planCost += getCardinality(p.children[0], costFlag) * p.ctx.GetSessionVars().GetNetworkFactor(nil)
} else { // to avoid regression, only consider row-size on model ver2
rowSize := getTblStats(p.children[0]).GetAvgRowSize(p.ctx, p.children[0].Schema().Columns, false, false)
p.planCost += getCardinality(p.children[0], costFlag) * rowSize * p.ctx.GetSessionVars().GetNetworkFactor(nil)
}
p.planCost += getCardinality(p.children[0], costFlag) * p.ctx.GetSessionVars().GetNetworkFactor(nil)
p.planCostInit = true
return p.planCost, nil
}
Expand Down
Loading

0 comments on commit ee8ebcb

Please sign in to comment.