diff --git a/pkg/session/bootstrap.go b/pkg/session/bootstrap.go index 0a24c5cbaa266..3c96e2e82d8e7 100644 --- a/pkg/session/bootstrap.go +++ b/pkg/session/bootstrap.go @@ -1203,11 +1203,15 @@ const ( // version 239 // add modify_params to tidb_global_task and tidb_global_task_history. version239 = 239 + + // version 240 + // Add indexes to mysql.analyze_jobs to speed up the query. + version240 = 240 ) // currentBootstrapVersion is defined as a variable, so we can modify its value for testing. // please make sure this is the largest version -var currentBootstrapVersion int64 = version239 +var currentBootstrapVersion int64 = version240 // DDL owner key's expired time is ManagerSessionTTL seconds, we should wait the time and give more time to have a chance to finish it. var internalSQLTimeout = owner.ManagerSessionTTL + 15 @@ -1382,6 +1386,7 @@ var ( upgradeToVer217, upgradeToVer218, upgradeToVer239, + upgradeToVer240, } ) @@ -3287,6 +3292,14 @@ func upgradeToVer239(s sessiontypes.Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.tidb_global_task_history ADD COLUMN modify_params json AFTER `error`;", infoschema.ErrColumnExists) } +func upgradeToVer240(s sessiontypes.Session, ver int64) { + if ver >= version240 { + return + } + doReentrantDDL(s, "ALTER TABLE mysql.analyze_jobs ADD INDEX idx_schema_table_state (table_schema, table_name, state)", dbterror.ErrDupKeyName) + doReentrantDDL(s, "ALTER TABLE mysql.analyze_jobs ADD INDEX idx_schema_table_partition_state (table_schema, table_name, partition_name, state)", dbterror.ErrDupKeyName) +} + // initGlobalVariableIfNotExists initialize a global variable with specific val if it does not exist. func initGlobalVariableIfNotExists(s sessiontypes.Session, name string, val any) { ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index d4d4f75d4f882..a786ef95b3093 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -2524,3 +2524,48 @@ func TestIndexJoinMultiPatternByUpgrade650To840(t *testing.T) { require.Equal(t, 1, row.Len()) require.Equal(t, int64(0), row.GetInt64(0)) } + +func TestTiDBUpgradeToVer240(t *testing.T) { + ctx := context.Background() + store, dom := CreateStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + ver239 := version239 + seV239 := CreateSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMutator(txn) + err = m.FinishBootstrap(int64(ver239)) + require.NoError(t, err) + revertVersionAndVariables(t, seV239, ver239) + err = txn.Commit(ctx) + require.NoError(t, err) + store.SetOption(StoreBootstrappedKey, nil) + + // Check index not exist. + res := MustExecToRecodeSet(t, seV239, "show create table mysql.analyze_jobs") + chk := res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 1, chk.NumRows()) + require.NotContains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_state") + require.NotContains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_partition_state") + + dom.Close() + domCurVer, err := BootstrapSession(store) + require.NoError(t, err) + defer domCurVer.Close() + seCurVer := CreateSessionAndSetID(t, store) + ver, err := getBootstrapVersion(seCurVer) + require.NoError(t, err) + require.Equal(t, currentBootstrapVersion, ver) + + // Check index exist. + res = MustExecToRecodeSet(t, seCurVer, "show create table mysql.analyze_jobs") + chk = res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 1, chk.NumRows()) + require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_state") + require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_partition_state") +}