session: Move OptimizerUseInvisibleIndexes from SmtmCtx to SessionVars (#17040)
This commit is contained in:
@ -1714,7 +1714,6 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
|
||||
errCount, warnCount := vars.StmtCtx.NumErrorWarnings()
|
||||
vars.SysErrorCount = errCount
|
||||
vars.SysWarningCount = warnCount
|
||||
sc.OptimizerUseInvisibleIndexes = vars.StmtCtx.OptimizerUseInvisibleIndexes
|
||||
vars.StmtCtx = sc
|
||||
vars.PrevFoundInPlanCache = vars.FoundInPlanCache
|
||||
vars.FoundInPlanCache = false
|
||||
|
||||
@ -723,7 +723,7 @@ func (b *PlanBuilder) getPossibleAccessPaths(indexHints []*ast.IndexHint, tbl ta
|
||||
if tblInfo.TiFlashReplica != nil && tblInfo.TiFlashReplica.Available {
|
||||
publicPaths = append(publicPaths, &util.AccessPath{IsTablePath: true, StoreType: kv.TiFlash})
|
||||
}
|
||||
optimizerUseInvisibleIndexes := b.ctx.GetSessionVars().StmtCtx.OptimizerUseInvisibleIndexes
|
||||
optimizerUseInvisibleIndexes := b.ctx.GetSessionVars().OptimizerUseInvisibleIndexes
|
||||
for _, index := range tblInfo.Indices {
|
||||
if index.State == model.StatePublic {
|
||||
// Filter out invisible index, because they are not visible for optimizer
|
||||
|
||||
@ -762,9 +762,9 @@ func (s *session) ExecRestrictedSQLWithContext(ctx context.Context, sql string)
|
||||
se.sessionVars.InspectionTableCache = cache
|
||||
defer func() { se.sessionVars.InspectionTableCache = nil }()
|
||||
}
|
||||
if ok := s.sessionVars.StmtCtx.OptimizerUseInvisibleIndexes; ok {
|
||||
se.sessionVars.StmtCtx.OptimizerUseInvisibleIndexes = true
|
||||
defer func() { se.sessionVars.StmtCtx.OptimizerUseInvisibleIndexes = false }()
|
||||
if ok := s.sessionVars.OptimizerUseInvisibleIndexes; ok {
|
||||
se.sessionVars.OptimizerUseInvisibleIndexes = true
|
||||
defer func() { se.sessionVars.OptimizerUseInvisibleIndexes = false }()
|
||||
}
|
||||
defer func() {
|
||||
if se != nil && se.GetSessionVars().StmtCtx.WarningCount() > 0 {
|
||||
@ -826,9 +826,9 @@ func (s *session) ExecRestrictedSQLWithSnapshot(sql string) ([]chunk.Row, []*ast
|
||||
se.sessionVars.SnapshotInfoschema = nil
|
||||
}()
|
||||
}
|
||||
if ok := s.sessionVars.StmtCtx.OptimizerUseInvisibleIndexes; ok {
|
||||
se.sessionVars.StmtCtx.OptimizerUseInvisibleIndexes = true
|
||||
defer func() { se.sessionVars.StmtCtx.OptimizerUseInvisibleIndexes = false }()
|
||||
if ok := s.sessionVars.OptimizerUseInvisibleIndexes; ok {
|
||||
se.sessionVars.OptimizerUseInvisibleIndexes = true
|
||||
defer func() { se.sessionVars.OptimizerUseInvisibleIndexes = false }()
|
||||
}
|
||||
return execRestrictedSQL(ctx, se, sql)
|
||||
}
|
||||
|
||||
@ -71,8 +71,6 @@ type StatementContext struct {
|
||||
BatchCheck bool
|
||||
InNullRejectCheck bool
|
||||
AllowInvalidDate bool
|
||||
// OptimizerUseInvisibleIndexes indicates whether optimizer can use invisible index
|
||||
OptimizerUseInvisibleIndexes bool
|
||||
|
||||
// mu struct holds variables that change during execution.
|
||||
mu struct {
|
||||
|
||||
@ -599,6 +599,9 @@ type SessionVars struct {
|
||||
FoundInPlanCache bool
|
||||
// PrevFoundInPlanCache indicates whether the last statement was found in plan cache.
|
||||
PrevFoundInPlanCache bool
|
||||
|
||||
// OptimizerUseInvisibleIndexes indicates whether optimizer can use invisible index
|
||||
OptimizerUseInvisibleIndexes bool
|
||||
}
|
||||
|
||||
// PreparedParams contains the parameters of the current prepared statement when executing it.
|
||||
|
||||
@ -310,7 +310,7 @@ const (
|
||||
// otherwise it returns an error and the corresponding index's offset.
|
||||
func CheckIndicesCount(ctx sessionctx.Context, dbName, tableName string, indices []string) (byte, int, error) {
|
||||
// Here we need check all indexes, includes invisible index
|
||||
ctx.GetSessionVars().StmtCtx.OptimizerUseInvisibleIndexes = true
|
||||
ctx.GetSessionVars().OptimizerUseInvisibleIndexes = true
|
||||
// Add `` for some names like `table name`.
|
||||
sql := fmt.Sprintf("SELECT COUNT(*) FROM `%s`.`%s` USE INDEX()", dbName, tableName)
|
||||
tblCnt, err := getCount(ctx, sql)
|
||||
|
||||
Reference in New Issue
Block a user