Skip to content

Commit b1b5ccc

Browse files
committed
make admin check for global index stable
Signed-off-by: Ruihao Chen <[email protected]>
1 parent 1ee37e6 commit b1b5ccc

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

pkg/executor/check_table_index.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,13 @@ func (w *checkIndexWorker) initSessCtx() (sessionctx.Context, func(), error) {
370370
sessVars := se.GetSessionVars()
371371
originOptUseInvisibleIdx := sessVars.OptimizerUseInvisibleIndexes
372372
originMemQuotaQuery := sessVars.MemQuotaQuery
373+
originSkipMissingPartitionStats := sessVars.SkipMissingPartitionStats
373374

374375
sessVars.OptimizerUseInvisibleIndexes = true
375376
sessVars.MemQuotaQuery = w.sctx.GetSessionVars().MemQuotaQuery
377+
// Make sure using index scan for global index.
378+
// See https://github.com/pingcap/tidb/blob/2010151c503c1a0b74d63e52a6019e03afada21d/pkg/planner/core/logical_plan_builder.go#L4506-L4518
379+
sessVars.SkipMissingPartitionStats = true
376380
snapshot := w.e.Ctx().GetSessionVars().SnapshotTS
377381
if snapshot != 0 {
378382
_, err := se.GetSQLExecutor().ExecuteInternal(w.e.contextCtx, fmt.Sprintf("set session tidb_snapshot = %d", snapshot))
@@ -384,6 +388,7 @@ func (w *checkIndexWorker) initSessCtx() (sessionctx.Context, func(), error) {
384388
return se, func() {
385389
sessVars.OptimizerUseInvisibleIndexes = originOptUseInvisibleIdx
386390
sessVars.MemQuotaQuery = originMemQuotaQuery
391+
sessVars.SkipMissingPartitionStats = originSkipMissingPartitionStats
387392
if snapshot != 0 {
388393
_, err := se.GetSQLExecutor().ExecuteInternal(w.e.contextCtx, "set session tidb_snapshot = 0")
389394
if err != nil {
@@ -732,8 +737,7 @@ func (w *checkIndexWorker) handleTask(task checkIndexTask) error {
732737

733738
tableQuery = fmt.Sprintf(tableChecksumSQL, groupByKey, whereKey, groupByKey)
734739
indexQuery = fmt.Sprintf(indexChecksumSQL, groupByKey, whereKey, groupByKey)
735-
verifyCheckQuery(ctx, se, tableQuery, true)
736-
verifyCheckQuery(ctx, se, indexQuery, false)
740+
verifyIndexSideQuery(ctx, se, indexQuery)
737741

738742
logutil.BgLogger().Info(
739743
"fast check table by group",
@@ -813,8 +817,7 @@ func (w *checkIndexWorker) handleTask(task checkIndexTask) error {
813817
groupByKey := fmt.Sprintf("((CAST(%s AS SIGNED) - %d) MOD %d)", md5Handle, offset, mod)
814818
tableQuery = fmt.Sprintf(tableCheckSQL, groupByKey)
815819
indexQuery = fmt.Sprintf(indexCheckSQL, groupByKey)
816-
verifyCheckQuery(ctx, se, tableQuery, true)
817-
verifyCheckQuery(ctx, se, indexQuery, false)
820+
verifyIndexSideQuery(ctx, se, indexQuery)
818821

819822
var (
820823
lastTableRecord *recordWithChecksum
@@ -901,7 +904,7 @@ type recordWithChecksum struct {
901904
checksum uint64
902905
}
903906

904-
func verifyCheckQuery(ctx context.Context, se sessionctx.Context, sql string, useTableScan bool) {
907+
func verifyIndexSideQuery(ctx context.Context, se sessionctx.Context, sql string) {
905908
rows, err := queryToRow(ctx, se, "explain "+sql)
906909
if err != nil {
907910
panic(err)
@@ -918,7 +921,7 @@ func verifyCheckQuery(ctx context.Context, se sessionctx.Context, sql string, us
918921
}
919922
}
920923

921-
if (useTableScan && !isTableScan) || (!useTableScan && !isIndexScan) {
924+
if isTableScan || !isIndexScan {
922925
panic(fmt.Sprintf("check query %s error, table scan: %t, index scan: %t",
923926
sql, isTableScan, isIndexScan))
924927
}

pkg/executor/test/admintest/admin_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,6 @@ func TestAdminCheckGlobalIndex(t *testing.T) {
19741974
require.NoError(t, err)
19751975
err = txn.Commit(context.Background())
19761976
require.NoError(t, err)
1977-
tk.MustExec("analyze table admin_test")
19781977
err = tk.ExecToErr("admin check table admin_test")
19791978
require.Error(t, err)
19801979
require.True(t, consistency.ErrAdminCheckInconsistent.Equal(err))
@@ -1989,7 +1988,6 @@ func TestAdminCheckGlobalIndex(t *testing.T) {
19891988
require.NoError(t, err)
19901989
err = txn.Commit(context.Background())
19911990
require.NoError(t, err)
1992-
tk.MustExec("analyze table admin_test")
19931991
tk.MustExec("admin check table admin_test")
19941992

19951993
indexOpr = tables.NewIndex(tblInfo.GetPartitionInfo().Definitions[2].ID, tblInfo, idxInfo)
@@ -2002,7 +2000,6 @@ func TestAdminCheckGlobalIndex(t *testing.T) {
20022000
require.NoError(t, err)
20032001
err = txn.Commit(context.Background())
20042002
require.NoError(t, err)
2005-
tk.MustExec("analyze table admin_test")
20062003
err = tk.ExecToErr("admin check table admin_test")
20072004
require.Error(t, err)
20082005
require.True(t, consistency.ErrAdminCheckInconsistent.Equal(err))
@@ -2016,7 +2013,6 @@ func TestAdminCheckGlobalIndex(t *testing.T) {
20162013
require.NoError(t, err)
20172014
err = txn.Commit(context.Background())
20182015
require.NoError(t, err)
2019-
tk.MustExec("analyze table admin_test")
20202016
err = tk.ExecToErr("admin check table admin_test")
20212017
require.Error(t, err)
20222018
if !enabled {
@@ -2072,9 +2068,6 @@ func TestAdminCheckGlobalIndexWithClusterIndex(t *testing.T) {
20722068
txn.Delete(tablecodec.EncodeRowKey(df.ID, kv.IntHandle(0).Encoded()))
20732069
err = txn.Commit(context.Background())
20742070
require.NoError(t, err)
2075-
// For global index, we must ensure the global stats is ready, otherwise index path will be pruned.
2076-
// See https://github.com/pingcap/tidb/blob/2010151c503c1a0b74d63e52a6019e03afada21d/pkg/planner/core/logical_plan_builder.go#L4506-L4518
2077-
tk.MustExec("analyze table admin_test")
20782071
err = tk.ExecToErr("admin check table admin_test")
20792072
require.Error(t, err)
20802073
require.True(t, consistency.ErrAdminCheckInconsistent.Equal(err))
@@ -2089,7 +2082,6 @@ func TestAdminCheckGlobalIndexWithClusterIndex(t *testing.T) {
20892082
require.NoError(t, err)
20902083
err = txn.Commit(context.Background())
20912084
require.NoError(t, err)
2092-
tk.MustExec("analyze table admin_test")
20932085
tk.MustExec("admin check table admin_test")
20942086

20952087
indexOpr = tables.NewIndex(tblInfo.GetPartitionInfo().Definitions[2].ID, tblInfo, idxInfo)
@@ -2101,7 +2093,6 @@ func TestAdminCheckGlobalIndexWithClusterIndex(t *testing.T) {
21012093
require.NoError(t, err)
21022094
err = txn.Commit(context.Background())
21032095
require.NoError(t, err)
2104-
tk.MustExec("analyze table admin_test")
21052096
err = tk.ExecToErr("admin check table admin_test")
21062097
require.Error(t, err)
21072098
require.True(t, consistency.ErrAdminCheckInconsistent.Equal(err))
@@ -2115,7 +2106,6 @@ func TestAdminCheckGlobalIndexWithClusterIndex(t *testing.T) {
21152106
require.NoError(t, err)
21162107
err = txn.Commit(context.Background())
21172108
require.NoError(t, err)
2118-
tk.MustExec("analyze table admin_test")
21192109
err = tk.ExecToErr("admin check table admin_test")
21202110
require.Error(t, err)
21212111
if !enabled {

0 commit comments

Comments
 (0)