plan: adjust cost factor to make right agg plan. (#1962)

This commit is contained in:
Han Fei
2016-11-08 14:18:44 +08:00
committed by GitHub
parent eca8474820
commit 2e8941aa4d
2 changed files with 11 additions and 4 deletions

View File

@ -31,7 +31,7 @@ const (
selectionFactor = 0.8
distinctFactor = 0.7
cpuFactor = 0.9
aggFactor = 0.2
aggFactor = 0.1
joinFactor = 0.3
)
@ -359,7 +359,7 @@ func enforceProperty(prop *requiredProperty, info *physicalPlanInfo) *physicalPl
if prop.limit != nil {
count = prop.limit.Offset + prop.limit.Count
}
info.cost += float64(info.count)*cpuFactor + float64(count)*memoryFactor
info.cost += sortCost(count)
} else if prop.limit != nil {
limit := prop.limit.Copy()
limit.SetSchema(info.p.GetSchema())
@ -371,6 +371,10 @@ func enforceProperty(prop *requiredProperty, info *physicalPlanInfo) *physicalPl
return info
}
func sortCost(cnt uint64) float64 {
return float64(cnt)*math.Log2(float64(cnt))*cpuFactor + memoryFactor*float64(cnt)
}
// removeLimit removes the limit from prop.
func removeLimit(prop *requiredProperty) *requiredProperty {
ret := &requiredProperty{
@ -948,8 +952,7 @@ func (p *Sort) convert2PhysicalPlan(prop *requiredProperty) (*physicalPlanInfo,
if err != nil {
return nil, errors.Trace(err)
}
cnt := float64(unSortedPlanInfo.count)
sortCost := cnt*math.Log2(cnt)*cpuFactor + memoryFactor*cnt
sortCost := sortCost(unSortedPlanInfo.count)
if len(selfProp.props) == 0 {
np := p.Copy().(*Sort)
np.ExecLimit = prop.limit

View File

@ -922,6 +922,10 @@ func (s *testPlanSuite) TestCBO(c *C) {
sql: "select count(*) from t t1 having 1 = 0",
best: "Dummy->HashAgg->Selection",
},
{
sql: "select sum(a.b), sum(b.b) from t a join t b on a.c = b.c group by a.d order by a.d",
best: "LeftHashJoin{Table(t)->Table(t)}(a.c,b.c)->HashAgg->Sort->Trim",
},
{
sql: "select count(*) from t group by c",
best: "Index(t.c_d_e)[[<nil>,+inf]]->StreamAgg",