plan: make agg's cost formula more reasonable. (#6307)

This commit is contained in:
Yiding Cui
2018-04-19 11:58:15 +08:00
committed by Jack Yu
parent 9e3f53c8a7
commit d6c04423f6
4 changed files with 28 additions and 12 deletions

View File

@ -248,7 +248,7 @@ func (s *testAnalyzeSuite) TestIndexRead(c *C) {
},
{
sql: "select count(*) from t where e > 1 group by b",
best: "TableReader(Table(t)->Sel([gt(test.t.e, 1)])->HashAgg)->HashAgg",
best: "IndexLookUp(Index(t.b)[[<nil>,+inf]], Table(t)->Sel([gt(test.t.e, 1)]))->StreamAgg",
},
{
sql: "select count(e) from t where t.b <= 20",

View File

@ -33,10 +33,13 @@ const (
scanFactor = 2.0
descScanFactor = 5 * scanFactor
memoryFactor = 5.0
hashAggMemFactor = 2.0
// 0.5 is the looking up agg context factor.
hashAggFactor = 1.2 + 0.5
selectionFactor = 0.8
distinctFactor = 0.8
cpuFactor = 0.9
distinctAggFactor = 1.6
createAggCtxFactor = 6
)
// wholeTaskTypes records all possible kinds of task that a plan can return. For Agg, TopN and Limit, we will try to get

View File

@ -304,6 +304,15 @@ type basePhysicalAgg struct {
GroupByItems []expression.Expression
}
func (p *basePhysicalAgg) hasDistinctFunc() bool {
for _, fun := range p.AggFuncs {
if fun.HasDistinct {
return true
}
}
return false
}
// PhysicalHashAgg is hash operator of aggregate.
type PhysicalHashAgg struct {
basePhysicalAgg

View File

@ -520,10 +520,12 @@ func (p *PhysicalStreamAgg) attach2Task(tasks ...task) task {
}
t = finishCopTask(p.ctx, cop)
attachPlan2Task(finalAgg, t)
t.addCost(t.count() * cpuFactor)
} else {
attachPlan2Task(p, t)
t.addCost(t.count() * cpuFactor)
}
t.addCost(t.count() * cpuFactor)
if p.hasDistinctFunc() {
t.addCost(t.count() * cpuFactor * distinctAggFactor)
}
return t
}
@ -534,8 +536,8 @@ func (p *PhysicalHashAgg) attach2Task(tasks ...task) task {
return invalidTask
}
cardinality := p.StatsInfo().count
task := tasks[0].copy()
if cop, ok := task.(*copTask); ok {
t := tasks[0].copy()
if cop, ok := t.(*copTask); ok {
partialAgg, finalAgg := p.newPartialAggregate()
if partialAgg != nil {
if cop.tablePlan != nil {
@ -547,12 +549,14 @@ func (p *PhysicalHashAgg) attach2Task(tasks ...task) task {
cop.indexPlan = partialAgg
}
}
task = finishCopTask(p.ctx, cop)
attachPlan2Task(finalAgg, task)
task.addCost(task.count()*cpuFactor + cardinality*hashAggMemFactor)
t = finishCopTask(p.ctx, cop)
attachPlan2Task(finalAgg, t)
} else {
attachPlan2Task(p, task)
task.addCost(task.count()*cpuFactor + cardinality*hashAggMemFactor)
attachPlan2Task(p, t)
}
return task
t.addCost(t.count()*cpuFactor*hashAggFactor + cardinality*createAggCtxFactor)
if p.hasDistinctFunc() {
t.addCost(t.count() * cpuFactor * distinctAggFactor)
}
return t
}