Skip to content

Commit

Permalink
address the comment
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <disxiaofei@163.com>
  • Loading branch information
Yisaer committed Aug 17, 2022
1 parent e64f93c commit b795817
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
22 changes: 15 additions & 7 deletions planner/core/plan_cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -994,16 +998,20 @@ 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 {
memoryCost *= float64(memQuota) / (rowSize * buildCnt)
} 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
}

Expand Down
30 changes: 14 additions & 16 deletions planner/core/plan_cost_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b795817

Please sign in to comment.