Skip to content

Commit 1469fcf

Browse files
authored
sessionctx: add tidb_analyze_column_options global variable (#54200)
ref #53567
1 parent 374f7b0 commit 1469fcf

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

docs/design/2024-05-23-predicate-columns.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
- [Performance Tests](#performance-tests)
2727
- [Impacts \& Risks](#impacts--risks)
2828
- [If new predicate columns appear, they cannot be analyzed in time](#if-new-predicate-columns-appear-they-cannot-be-analyzed-in-time)
29-
- [Use PREDICATE COLUMNS when your workload's query pattern is relatively stable](#use-predicate-columns-when-your-workloads-query-pattern-is--relatively-stable)
29+
- [Use PREDICATE COLUMNS when your workload's query pattern is relatively stable](#use-predicate-columns-when-your-workloads-query-pattern-is-relatively-stable)
3030
- [Investigation \& Alternatives](#investigation--alternatives)
3131
- [CRDB](#crdb)
3232
- [Summary](#summary)
@@ -214,14 +214,14 @@ In the experimental implementation, we introduce a new global variable `tidb_ena
214214

215215
But because we decided to track all columns by default, so it becomes unnecessary to use this variable. We will mark it deprecated and remove it in the future.
216216

217-
In this feature, we introduce a new global variable `tidb_analyze_default_column_choice` to control whether to use predicate columns or all columns in the analyze process.
217+
In this feature, we introduce a new global variable `tidb_analyze_column_options` to control whether to use predicate columns or all columns in the analyze process.
218218

219219
Users can set this variable to `ALL` or `PREDICATE` to analyze all columns or only predicate columns. The default value will be `PREDICATE` after this feature is fully implemented.
220220

221221
```sql
222-
SET GLOBAL tidb_analyze_default_column_choice = 'PREDICATE';
222+
SET GLOBAL tidb_analyze_column_options = 'PREDICATE';
223223

224-
SET GLOBAL tidb_analyze_default_column_choice = 'ALL';
224+
SET GLOBAL tidb_analyze_column_options = 'ALL';
225225
```
226226

227227
| Value | Description |

pkg/executor/set_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,14 @@ func TestSetVar(t *testing.T) {
681681
require.Error(t, tk.ExecToErr("set tidb_enable_column_tracking = 0"))
682682
require.Error(t, tk.ExecToErr("set global tidb_enable_column_tracking = -1"))
683683

684+
// test for tidb_analyze_column_options
685+
tk.MustQuery("select @@tidb_analyze_column_options").Check(testkit.Rows("ALL"))
686+
tk.MustExec("set global tidb_analyze_column_options = 'PREDICATE'")
687+
tk.MustQuery("select @@tidb_analyze_column_options").Check(testkit.Rows("PREDICATE"))
688+
tk.MustExec("set global tidb_analyze_column_options = 'all'")
689+
tk.MustQuery("select @@tidb_analyze_column_options").Check(testkit.Rows("ALL"))
690+
require.Error(t, tk.ExecToErr("set global tidb_analyze_column_options = 'UNKNOWN'"))
691+
684692
// test for tidb_ignore_prepared_cache_close_stmt
685693
tk.MustQuery("select @@global.tidb_ignore_prepared_cache_close_stmt").Check(testkit.Rows("0")) // default value is 0
686694
tk.MustExec("set global tidb_ignore_prepared_cache_close_stmt=1")

pkg/sessionctx/variable/sysvar.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/pingcap/tidb/pkg/parser"
3535
"github.com/pingcap/tidb/pkg/parser/ast"
3636
"github.com/pingcap/tidb/pkg/parser/charset"
37+
"github.com/pingcap/tidb/pkg/parser/model"
3738
"github.com/pingcap/tidb/pkg/parser/mysql"
3839
"github.com/pingcap/tidb/pkg/planner/util/fixcontrol"
3940
"github.com/pingcap/tidb/pkg/privilege/privileges/ldap"
@@ -1010,7 +1011,33 @@ var defaultSysVars = []*SysVar{
10101011
RunAutoAnalyze.Store(TiDBOptOn(val))
10111012
return nil
10121013
},
1013-
}, {
1014+
},
1015+
{
1016+
Scope: ScopeGlobal,
1017+
Name: TiDBAnalyzeColumnOptions,
1018+
Value: DefTiDBAnalyzeColumnOptions,
1019+
Type: TypeStr,
1020+
GetGlobal: func(ctx context.Context, s *SessionVars) (string, error) {
1021+
return AnalyzeColumnOptions.Load(), nil
1022+
},
1023+
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
1024+
AnalyzeColumnOptions.Store(strings.ToUpper(val))
1025+
return nil
1026+
},
1027+
Validation: func(s *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
1028+
choice := strings.ToUpper(normalizedValue)
1029+
if choice != model.AllColumns.String() && choice != model.PredicateColumns.String() {
1030+
return "", errors.Errorf(
1031+
"invalid value for %s, it should be either '%s' or '%s'",
1032+
TiDBAnalyzeColumnOptions,
1033+
model.AllColumns.String(),
1034+
model.PredicateColumns.String(),
1035+
)
1036+
}
1037+
return normalizedValue, nil
1038+
},
1039+
},
1040+
{
10141041
Scope: ScopeGlobal, Name: TiDBEnableAutoAnalyzePriorityQueue, Value: BoolToOnOff(DefTiDBEnableAutoAnalyzePriorityQueue), Type: TypeBool,
10151042
GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
10161043
return BoolToOnOff(EnableAutoAnalyzePriorityQueue.Load()), nil

pkg/sessionctx/variable/tidb_vars.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,11 @@ const (
978978
// TiDBEnableColumnTracking enables collecting predicate columns.
979979
// DEPRECATED: This variable is deprecated, please do not use this variable.
980980
TiDBEnableColumnTracking = "tidb_enable_column_tracking"
981+
// TiDBAnalyzeColumnOptions specifies the default column selection strategy for both manual and automatic analyze operations.
982+
// It accepts two values:
983+
// `PREDICATE`: Analyze only the columns that are used in the predicates of the query.
984+
// `ALL`: Analyze all columns in the table.
985+
TiDBAnalyzeColumnOptions = "tidb_analyze_column_options"
981986
// TiDBDisableColumnTrackingTime records the last time TiDBEnableColumnTracking is set off.
982987
// It is used to invalidate the collected predicate columns after turning off TiDBEnableColumnTracking, which avoids physical deletion.
983988
// It doesn't have cache in memory, and we directly get/set the variable value from/to mysql.tidb.
@@ -1373,6 +1378,7 @@ const (
13731378
DefTiDBMemQuotaAnalyze = -1
13741379
DefTiDBEnableAutoAnalyze = true
13751380
DefTiDBEnableAutoAnalyzePriorityQueue = true
1381+
DefTiDBAnalyzeColumnOptions = "ALL"
13761382
DefTiDBMemOOMAction = "CANCEL"
13771383
DefTiDBMaxAutoAnalyzeTime = 12 * 60 * 60
13781384
DefTiDBEnablePrepPlanCache = true
@@ -1499,20 +1505,29 @@ const (
14991505

15001506
// Process global variables.
15011507
var (
1502-
ProcessGeneralLog = atomic.NewBool(false)
1503-
RunAutoAnalyze = atomic.NewBool(DefTiDBEnableAutoAnalyze)
1504-
EnableAutoAnalyzePriorityQueue = atomic.NewBool(DefTiDBEnableAutoAnalyzePriorityQueue)
1505-
GlobalLogMaxDays = atomic.NewInt32(int32(config.GetGlobalConfig().Log.File.MaxDays))
1506-
QueryLogMaxLen = atomic.NewInt32(DefTiDBQueryLogMaxLen)
1507-
EnablePProfSQLCPU = atomic.NewBool(false)
1508-
EnableBatchDML = atomic.NewBool(false)
1509-
EnableTmpStorageOnOOM = atomic.NewBool(DefTiDBEnableTmpStorageOnOOM)
1510-
ddlReorgWorkerCounter int32 = DefTiDBDDLReorgWorkerCount
1511-
ddlReorgBatchSize int32 = DefTiDBDDLReorgBatchSize
1512-
ddlFlashbackConcurrency int32 = DefTiDBDDLFlashbackConcurrency
1513-
ddlErrorCountLimit int64 = DefTiDBDDLErrorCountLimit
1514-
ddlReorgRowFormat int64 = DefTiDBRowFormatV2
1515-
maxDeltaSchemaCount int64 = DefTiDBMaxDeltaSchemaCount
1508+
ProcessGeneralLog = atomic.NewBool(false)
1509+
RunAutoAnalyze = atomic.NewBool(DefTiDBEnableAutoAnalyze)
1510+
EnableAutoAnalyzePriorityQueue = atomic.NewBool(DefTiDBEnableAutoAnalyzePriorityQueue)
1511+
// AnalyzeColumnOptions is a global variable that indicates the default column choice for ANALYZE.
1512+
// The value of this variable is a string that can be one of the following values:
1513+
// "PREDICATE", "ALL".
1514+
// The behavior of the analyze operation depends on the value of `tidb_persist_analyze_options`:
1515+
// 1. If `tidb_persist_analyze_options` is enabled and the column choice from the analyze options record is set to `default`,
1516+
// the value of `tidb_analyze_column_options` determines the behavior of the analyze operation.
1517+
// 2. If `tidb_persist_analyze_options` is disabled, `tidb_analyze_column_options` is used directly to decide
1518+
// whether to analyze all columns or just the predicate columns.
1519+
AnalyzeColumnOptions = atomic.NewString(DefTiDBAnalyzeColumnOptions)
1520+
GlobalLogMaxDays = atomic.NewInt32(int32(config.GetGlobalConfig().Log.File.MaxDays))
1521+
QueryLogMaxLen = atomic.NewInt32(DefTiDBQueryLogMaxLen)
1522+
EnablePProfSQLCPU = atomic.NewBool(false)
1523+
EnableBatchDML = atomic.NewBool(false)
1524+
EnableTmpStorageOnOOM = atomic.NewBool(DefTiDBEnableTmpStorageOnOOM)
1525+
ddlReorgWorkerCounter int32 = DefTiDBDDLReorgWorkerCount
1526+
ddlReorgBatchSize int32 = DefTiDBDDLReorgBatchSize
1527+
ddlFlashbackConcurrency int32 = DefTiDBDDLFlashbackConcurrency
1528+
ddlErrorCountLimit int64 = DefTiDBDDLErrorCountLimit
1529+
ddlReorgRowFormat int64 = DefTiDBRowFormatV2
1530+
maxDeltaSchemaCount int64 = DefTiDBMaxDeltaSchemaCount
15161531
// DDLSlowOprThreshold is the threshold for ddl slow operations, uint is millisecond.
15171532
DDLSlowOprThreshold = config.GetGlobalConfig().Instance.DDLSlowOprThreshold
15181533
ForcePriority = int32(DefTiDBForcePriority)

0 commit comments

Comments
 (0)