Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Jan 2, 2019
1 parent 70099c9 commit dd7cd1a
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func (er *expressionRewriter) handleInSubquery(v *ast.PatternInExpr) (ast.Node,
// We need to try to eliminate the agg and the projection produced by this operation.
er.b.optFlag |= flagEliminateAgg
er.b.optFlag |= flagEliminateProjection
er.b.optFlag |= flagJoinReOrderGreedy
er.b.optFlag |= flagJoinReOrder
// Build distinct for the inner query.
agg := er.b.buildDistinct(np, np.Schema().Len())
for _, col := range agg.schema.Columns {
Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (b *PlanBuilder) buildJoin(joinNode *ast.Join) (LogicalPlan, error) {
joinPlan.JoinType = RightOuterJoin
resetNotNullFlag(joinPlan.schema, 0, leftPlan.Schema().Len())
default:
b.optFlag = b.optFlag | flagJoinReOrderGreedy
b.optFlag = b.optFlag | flagJoinReOrder
joinPlan.JoinType = InnerJoin
}

Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func (s *testPlanSuite) TestJoinReOrder(c *C) {

p, err := BuildLogicalPlan(s.ctx, stmt, s.is)
c.Assert(err, IsNil)
p, err = logicalOptimize(flagPredicatePushDown|flagJoinReOrderGreedy, p.(LogicalPlan))
p, err = logicalOptimize(flagPredicatePushDown|flagJoinReOrder, p.(LogicalPlan))
c.Assert(err, IsNil)
c.Assert(ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
Expand Down
2 changes: 1 addition & 1 deletion planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const (
flagPartitionProcessor
flagPushDownAgg
flagPushDownTopN
flagJoinReOrderGreedy
flagJoinReOrder
)

var optRuleList = []logicalOptRule{
Expand Down
2 changes: 1 addition & 1 deletion planner/core/rule_join_reorder_greedy.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (s *joinReOrderSolver) optimizeRecursive(ctx sessionctx.Context, p LogicalP
return nil, err
}
}
if len(curJoinGroup) > 10 {
if len(curJoinGroup) > ctx.GetSessionVars().TiDBOptJoinOrderAlgoThreshold {
greedySolver := &joinReorderGreedySingleGroupSolver{
ctx: ctx,
curJoinGroup: curJoinGroup,
Expand Down
47 changes: 27 additions & 20 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,31 +332,36 @@ type SessionVars struct {

// CommandValue indicates which command current session is doing.
CommandValue uint32

// TIDBOptJoinOrderAlgoThreshold defines the threshold less than which
// we'll choose a rather time consuming algorithm to calculate the join order.
TiDBOptJoinOrderAlgoThreshold int
}

// NewSessionVars creates a session vars object.
func NewSessionVars() *SessionVars {
vars := &SessionVars{
Users: make(map[string]string),
systems: make(map[string]string),
PreparedStmts: make(map[uint32]*ast.Prepared),
PreparedStmtNameToID: make(map[string]uint32),
PreparedParams: make([]types.Datum, 0, 10),
TxnCtx: &TransactionContext{},
KVVars: kv.NewVariables(),
RetryInfo: &RetryInfo{},
StrictSQLMode: true,
Status: mysql.ServerStatusAutocommit,
StmtCtx: new(stmtctx.StatementContext),
AllowAggPushDown: false,
OptimizerSelectivityLevel: DefTiDBOptimizerSelectivityLevel,
RetryLimit: DefTiDBRetryLimit,
DisableTxnAutoRetry: DefTiDBDisableTxnAutoRetry,
DDLReorgPriority: kv.PriorityLow,
AllowInSubqToJoinAndAgg: DefOptInSubqToJoinAndAgg,
EnableRadixJoin: false,
L2CacheSize: cpuid.CPU.Cache.L2,
CommandValue: uint32(mysql.ComSleep),
Users: make(map[string]string),
systems: make(map[string]string),
PreparedStmts: make(map[uint32]*ast.Prepared),
PreparedStmtNameToID: make(map[string]uint32),
PreparedParams: make([]types.Datum, 0, 10),
TxnCtx: &TransactionContext{},
KVVars: kv.NewVariables(),
RetryInfo: &RetryInfo{},
StrictSQLMode: true,
Status: mysql.ServerStatusAutocommit,
StmtCtx: new(stmtctx.StatementContext),
AllowAggPushDown: false,
OptimizerSelectivityLevel: DefTiDBOptimizerSelectivityLevel,
RetryLimit: DefTiDBRetryLimit,
DisableTxnAutoRetry: DefTiDBDisableTxnAutoRetry,
DDLReorgPriority: kv.PriorityLow,
AllowInSubqToJoinAndAgg: DefOptInSubqToJoinAndAgg,
EnableRadixJoin: false,
L2CacheSize: cpuid.CPU.Cache.L2,
CommandValue: uint32(mysql.ComSleep),
TiDBOptJoinOrderAlgoThreshold: DefTiDBOptJoinOrderAlgoThreshold,
}
vars.Concurrency = Concurrency{
IndexLookupConcurrency: DefIndexLookupConcurrency,
Expand Down Expand Up @@ -690,6 +695,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.EnableRadixJoin = TiDBOptOn(val)
case TiDBEnableWindowFunction:
s.EnableWindowFunction = TiDBOptOn(val)
case TiDBOptJoinOrderAlgoThreshold:
s.TiDBOptJoinOrderAlgoThreshold = tidbOptPositiveInt32(val, DefTiDBOptJoinOrderAlgoThreshold)
}
s.systems[name] = val
return nil
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ var defaultSysVars = []*SysVar{
{ScopeSession, TiDBDDLReorgPriority, "PRIORITY_LOW"},
{ScopeSession, TiDBForcePriority, mysql.Priority2Str[DefTiDBForcePriority]},
{ScopeSession, TiDBEnableRadixJoin, boolToIntStr(DefTiDBUseRadixJoin)},
{ScopeGlobal | ScopeSession, TiDBOptJoinOrderAlgoThreshold, strconv.Itoa(DefTiDBOptJoinOrderAlgoThreshold)},
}

// SynonymsSysVariables is synonyms of system variables.
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ const (

// tidb_enable_window_function is used to control whether to enable the window function.
TiDBEnableWindowFunction = "tidb_enable_window_function"

// TIDBOptJoinOrderAlgoThreshold defines the threshold less than which
// we'll choose a rather time consuming algorithm to calculate the join order.
TiDBOptJoinOrderAlgoThreshold = "tidb_opt_join_order_algo_threshold"
)

// Default TiDB system variable values.
Expand Down Expand Up @@ -274,6 +278,7 @@ const (
DefTiDBForcePriority = mysql.NoPriority
DefTiDBUseRadixJoin = false
DefEnableWindowFunction = false
DefTiDBOptJoinOrderAlgoThreshold = 10
)

// Process global variables.
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
TiDBDistSQLScanConcurrency,
TiDBIndexSerialScanConcurrency, TiDBDDLReorgWorkerCount,
TiDBBackoffLockFast, TiDBMaxChunkSize,
TiDBDMLBatchSize, TiDBOptimizerSelectivityLevel:
TiDBDMLBatchSize, TiDBOptimizerSelectivityLevel, TiDBOptJoinOrderAlgoThreshold:
v, err := strconv.Atoi(value)
if err != nil {
return value, ErrWrongTypeForVar.GenWithStackByArgs(name)
Expand Down
9 changes: 9 additions & 0 deletions sessionctx/variable/varsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (s *testVarsutilSuite) TestNewSessionVars(c *C) {
c.Assert(vars.MemQuotaNestedLoopApply, Equals, int64(DefTiDBMemQuotaNestedLoopApply))
c.Assert(vars.EnableRadixJoin, Equals, DefTiDBUseRadixJoin)
c.Assert(vars.AllowWriteRowID, Equals, DefOptWriteRowID)
c.Assert(vars.TiDBOptJoinOrderAlgoThreshold, Equals, DefTiDBOptJoinOrderAlgoThreshold)

assertFieldsGreaterThanZero(c, reflect.ValueOf(vars.Concurrency))
assertFieldsGreaterThanZero(c, reflect.ValueOf(vars.MemQuota))
Expand Down Expand Up @@ -237,4 +238,12 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) {
c.Assert(err, IsNil)
c.Assert(val, Equals, "on")
c.Assert(v.EnableTablePartition, Equals, "on")

c.Assert(v.TiDBOptJoinOrderAlgoThreshold, Equals, DefTiDBOptJoinOrderAlgoThreshold)
err = SetSessionSystemVar(v, TiDBOptJoinOrderAlgoThreshold, types.NewIntDatum(5))
c.Assert(err, IsNil)
val, err = GetSessionSystemVar(v, TiDBOptJoinOrderAlgoThreshold)
c.Assert(err, IsNil)
c.Assert(val, Equals, "5")
c.Assert(v.TiDBOptJoinOrderAlgoThreshold, Equals, 5)
}

0 comments on commit dd7cd1a

Please sign in to comment.