Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: separate cost model ver1/ver2 into different files (part 2) #38273

Merged
merged 6 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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.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