diff --git a/plan/plan.go b/plan/plan.go index 79bb69e819..14c5e1a3ce 100644 --- a/plan/plan.go +++ b/plan/plan.go @@ -30,7 +30,7 @@ import ( // UseDAGPlanBuilder checks if we use new DAG planner. func UseDAGPlanBuilder(ctx context.Context) bool { - return ctx.GetClient().IsRequestTypeSupported(kv.ReqTypeDAG, kv.ReqSubTypeBasic) + return ctx.GetClient().IsRequestTypeSupported(kv.ReqTypeDAG, kv.ReqSubTypeBasic) && ctx.GetSessionVars().CBO } // Plan is the description of an execution flow. diff --git a/session.go b/session.go index 80e4f12d73..7d9969c2b1 100644 --- a/session.go +++ b/session.go @@ -1029,6 +1029,7 @@ const loadCommonGlobalVarsSQL = "select * from mysql.global_variables where vari variable.TiDBIndexLookupConcurrency + quoteCommaQuote + variable.TiDBIndexSerialScanConcurrency + quoteCommaQuote + variable.TiDBMaxRowCountForINLJ + quoteCommaQuote + + variable.TiDBCBO + quoteCommaQuote + variable.TiDBDistSQLScanConcurrency + "')" // loadCommonGlobalVariablesIfNeeded loads and applies commonly used global variables for the session. diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index 37a86e09cd..7804e2f799 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -208,6 +208,9 @@ type SessionVars struct { // MaxRowCountForINLJ defines max row count that the outer table of index nested loop join could be without force hint. MaxRowCountForINLJ int + + // CBO indicates if we use new planner with cbo. + CBO bool } // NewSessionVars creates a session vars object. @@ -229,6 +232,7 @@ func NewSessionVars() *SessionVars { IndexSerialScanConcurrency: DefIndexSerialScanConcurrency, DistSQLScanConcurrency: DefDistSQLScanConcurrency, MaxRowCountForINLJ: DefMaxRowCountForINLJ, + CBO: true, } } diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 97423bb8c8..fb3533f445 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -607,6 +607,7 @@ var defaultSysVars = []*SysVar{ {ScopeGlobal | ScopeSession, TiDBIndexLookupConcurrency, strconv.Itoa(DefIndexLookupConcurrency)}, {ScopeGlobal | ScopeSession, TiDBIndexSerialScanConcurrency, strconv.Itoa(DefIndexSerialScanConcurrency)}, {ScopeGlobal | ScopeSession, TiDBMaxRowCountForINLJ, strconv.Itoa(DefMaxRowCountForINLJ)}, + {ScopeGlobal | ScopeSession, TiDBCBO, "ON"}, {ScopeGlobal | ScopeSession, TiDBSkipUTF8Check, boolToIntStr(DefSkipUTF8Check)}, {ScopeSession, TiDBBatchInsert, boolToIntStr(DefBatchInsert)}, {ScopeSession, TiDBCurrentTS, strconv.Itoa(DefCurretTS)}, diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 9d5ace3333..fe2798a8cf 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -90,6 +90,9 @@ const ( // It controls the max row count of outer table when do index nested loop join without hint. // After the row count of the inner table is accurate, this variable will be removed. TiDBMaxRowCountForINLJ = "tidb_max_row_count_for_inlj" + + // tidb_cbo uses new planner with cost based optimizer. + TiDBCBO = "tidb_cbo" ) // Default TiDB system variable values. diff --git a/sessionctx/varsutil/varsutil.go b/sessionctx/varsutil/varsutil.go index 6e4dc60fbe..721ecdbbdb 100644 --- a/sessionctx/varsutil/varsutil.go +++ b/sessionctx/varsutil/varsutil.go @@ -144,6 +144,8 @@ func SetSessionSystemVar(vars *variable.SessionVars, name string, value types.Da vars.BatchInsert = tidbOptOn(sVal) case variable.TiDBMaxRowCountForINLJ: vars.MaxRowCountForINLJ = tidbOptPositiveInt(sVal, variable.DefMaxRowCountForINLJ) + case variable.TiDBCBO: + vars.CBO = tidbOptOn(sVal) case variable.TiDBCurrentTS: return variable.ErrReadOnly } diff --git a/store/tikv/coprocessor.go b/store/tikv/coprocessor.go index c800eeaab7..0d92cf95d9 100644 --- a/store/tikv/coprocessor.go +++ b/store/tikv/coprocessor.go @@ -45,7 +45,7 @@ func (c *CopClient) IsRequestTypeSupported(reqType, subType int64) bool { return supportExpr(tipb.ExprType(subType)) } case kv.ReqTypeDAG: - return c.store.mock + return true } return false }