From b795817e80ad915aca2eb80777a6a0cd6a036b21 Mon Sep 17 00:00:00 2001 From: yisaer Date: Wed, 17 Aug 2022 13:49:45 +0800 Subject: [PATCH] address the comment Signed-off-by: yisaer --- planner/core/plan_cost.go | 22 +++++++++++++++------- planner/core/plan_cost_detail.go | 30 ++++++++++++++---------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/planner/core/plan_cost.go b/planner/core/plan_cost.go index 170f98b6f4813..7e7277c653e67 100644 --- a/planner/core/plan_cost.go +++ b/planner/core/plan_cost.go @@ -938,9 +938,13 @@ func (p *PhysicalHashJoin) GetCost(lCnt, rCnt float64, isMPP bool, costFlag uint if isMPP && p.ctx.GetSessionVars().CostModelVersion == modelVer2 { cpuFactor = sessVars.GetTiFlashCPUFactor() // use the dedicated TiFlash CPU Factor on modelVer2 } + diskFactor := sessVars.GetMemoryFactor() + memoryFactor := sessVars.GetMemoryFactor() + concurrencyFactor := sessVars.GetConcurrencyFactor() + cpuCost := buildCnt * cpuFactor - memoryCost := buildCnt * sessVars.GetMemoryFactor() - diskCost := buildCnt * sessVars.GetDiskFactor() * rowSize + memoryCost := buildCnt * memoryFactor + diskCost := buildCnt * diskFactor * rowSize // Number of matched row pairs regarding the equal join conditions. helper := &fullJoinRowCountHelper{ cartesian: false, @@ -974,7 +978,7 @@ func (p *PhysicalHashJoin) GetCost(lCnt, rCnt float64, isMPP bool, costFlag uint // Cost of querying hash table is cheap actually, so we just compute the cost of // evaluating `OtherConditions` and joining row pairs. probeCost := numPairs * cpuFactor - probeDiskCost := numPairs * sessVars.GetDiskFactor() * rowSize + probeDiskCost := numPairs * diskFactor * rowSize // Cost of evaluating outer filter. if len(p.LeftConditions)+len(p.RightConditions) > 0 { // Input outer count for the above compution should be adjusted by SelectionFactor. @@ -985,7 +989,7 @@ func (p *PhysicalHashJoin) GetCost(lCnt, rCnt float64, isMPP bool, costFlag uint diskCost += probeDiskCost probeCost /= float64(p.Concurrency) // Cost of additional concurrent goroutines. - cpuCost += probeCost + float64(p.Concurrency+1)*sessVars.GetConcurrencyFactor() + cpuCost += probeCost + float64(p.Concurrency+1)*concurrencyFactor // Cost of traveling the hash table to resolve missing matched cases when building the hash table from the outer table if p.UseOuterToBuild { if spill { @@ -994,7 +998,7 @@ func (p *PhysicalHashJoin) GetCost(lCnt, rCnt float64, isMPP bool, costFlag uint } else { cpuCost += buildCnt * cpuFactor / float64(p.Concurrency) } - diskCost += buildCnt * sessVars.GetDiskFactor() * rowSize + diskCost += buildCnt * diskFactor * rowSize } if spill { @@ -1002,8 +1006,12 @@ func (p *PhysicalHashJoin) GetCost(lCnt, rCnt float64, isMPP bool, costFlag uint } else { diskCost = 0 } - setPhysicalHashJoinCostDetail(p, op, spill, buildCnt, probeCnt, cpuFactor, rowSize, numPairs, - cpuCost, probeCost, memoryCost, diskCost, probeDiskCost, memQuota) + if op != nil { + setPhysicalHashJoinCostDetail(p, op, spill, buildCnt, probeCnt, cpuFactor, rowSize, numPairs, + cpuCost, probeCost, memoryCost, diskCost, probeDiskCost, + diskFactor, memoryFactor, concurrencyFactor, + memQuota) + } return cpuCost + memoryCost + diskCost } diff --git a/planner/core/plan_cost_detail.go b/planner/core/plan_cost_detail.go index 2bebb9c61ad4d..e5f2eaec16c07 100644 --- a/planner/core/plan_cost_detail.go +++ b/planner/core/plan_cost_detail.go @@ -179,18 +179,18 @@ func setPhysicalIndexReaderCostDetail(p *PhysicalIndexReader, opt *physicalOptim func setPhysicalHashJoinCostDetail(p *PhysicalHashJoin, opt *physicalOptimizeOp, spill bool, buildCnt, probeCnt, cpuFactor, rowSize, numPairs, - cpuCost, probeCPUCost, memCost, diskCost, probeDiskCost float64, + cpuCost, probeCPUCost, memCost, diskCost, probeDiskCost, + diskFactor, memoryFactor, concurrencyFactor float64, memQuota int64) { if opt == nil { return } detail := tracing.NewPhysicalPlanCostDetail(p.ID(), p.TP()) - sessVars := p.ctx.GetSessionVars() diskCostDetail := &HashJoinDiskCostDetail{ Spill: spill, UseOuterToBuild: p.UseOuterToBuild, BuildRowCount: buildCnt, - DiskFactor: sessVars.GetDiskFactor(), + DiskFactor: diskFactor, RowSize: rowSize, ProbeDiskCost: &HashJoinProbeDiskCostDetail{ SelectionFactor: SelectionFactor, @@ -205,13 +205,13 @@ func setPhysicalHashJoinCostDetail(p *PhysicalHashJoin, opt *physicalOptimizeOp, MemQuota: memQuota, RowSize: rowSize, BuildRowCount: buildCnt, - MemoryFactor: sessVars.GetMemoryFactor(), + MemoryFactor: memoryFactor, Cost: memCost, } cpuCostDetail := &HashJoinCPUCostDetail{ BuildRowCount: buildCnt, CPUFactor: cpuFactor, - ConcurrencyFactor: sessVars.GetConcurrencyFactor(), + ConcurrencyFactor: concurrencyFactor, ProbeCost: &HashJoinProbeCostDetail{ NumPairs: numPairs, HasConditions: len(p.LeftConditions)+len(p.RightConditions) > 0, @@ -258,21 +258,19 @@ type HashJoinCPUCostDetail struct { HashJoinConcurrency uint `json:"hashJoinConcurrency"` Spill bool `json:"spill"` Cost float64 `json:"cost"` + UseOuterToBuild bool `json:"useOuterToBuild"` } func (h *HashJoinCPUCostDetail) desc() string { var cpuCostDesc string - buildCostDesc := fmt.Sprintf("%s*%s", BuildRowCountLbl, CPUFactorLbl) - if h.Spill { - cpuCostDesc = fmt.Sprintf("%s+%s+(%s+1)*%s)+%s", - buildCostDesc, - ProbeCostDetailLbl, HashJoinConcurrencyLbl, ConcurrencyFactorLbl, - buildCostDesc) - } else { - cpuCostDesc = fmt.Sprintf("%s+%s+(%s+1)*%s)+%s/%s", - buildCostDesc, - ProbeCostDetailLbl, HashJoinConcurrencyLbl, ConcurrencyFactorLbl, - buildCostDesc, HashJoinConcurrencyLbl) + buildCostDesc := fmt.Sprintf("%s+(%s+1)*%s)+%s*%s", + ProbeCostDetailLbl, HashJoinConcurrencyLbl, ConcurrencyFactorLbl, BuildRowCountLbl, CPUFactorLbl) + if h.UseOuterToBuild { + if h.Spill { + buildCostDesc = fmt.Sprintf("%s+%s*%s", buildCostDesc, BuildRowCountLbl, CPUFactorLbl) + } else { + buildCostDesc = fmt.Sprintf("%s+%s*%s/%s", buildCostDesc, BuildRowCountLbl, CPUFactorLbl, HashJoinConcurrencyLbl) + } } return cpuCostDesc }