Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics/handle: fix trigger auto analyze for partition table after adding index #41639

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions statistics/handle/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,14 +1230,11 @@ func (h *Handle) autoAnalyzePartitionTableInDynamicMode(tblInfo *model.TableInfo
sqlBuilder.WriteString(suffix)
return sqlBuilder.String()
}
if len(partitionNames) < 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the if branch exists, we would not go to the part of checking index stats(old Line1258-1289).

return false
}
logutil.BgLogger().Info("[stats] start to auto analyze",
zap.String("table", tblInfo.Name.String()),
zap.Any("partitions", partitionNames),
zap.Int("analyze partition batch size", analyzePartitionBatchSize))
if len(partitionNames) > 0 {
logutil.BgLogger().Info("[stats] start to auto analyze",
zap.String("table", tblInfo.Name.String()),
zap.Any("partitions", partitionNames),
zap.Int("analyze partition batch size", analyzePartitionBatchSize))
statsTbl := h.GetTableStats(tblInfo)
statistics.CheckAnalyzeVerOnTable(statsTbl, &tableStatsVer)
for i := 0; i < len(partitionNames); i += analyzePartitionBatchSize {
Expand Down
35 changes: 35 additions & 0 deletions statistics/handle/updatetest/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2707,3 +2707,38 @@ func TestNotDumpSysTable(t *testing.T) {
tblID := tbl.Meta().ID
tk.MustQuery(fmt.Sprintf("select * from mysql.stats_meta where table_id = %v", tblID)).Check(testkit.Rows())
}

func TestAutoAnalyzePartitionTableAfterAddingIndex(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
oriMinCnt := handle.AutoAnalyzeMinCnt
oriStart := tk.MustQuery("select @@tidb_auto_analyze_start_time").Rows()[0][0].(string)
oriEnd := tk.MustQuery("select @@tidb_auto_analyze_end_time").Rows()[0][0].(string)
defer func() {
handle.AutoAnalyzeMinCnt = oriMinCnt
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_start_time='%v'", oriStart))
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_end_time='%v'", oriEnd))
}()
handle.AutoAnalyzeMinCnt = 0
tk.MustExec("set global tidb_auto_analyze_start_time='00:00 +0000'")
tk.MustExec("set global tidb_auto_analyze_end_time='23:59 +0000'")
tk.MustExec("set global tidb_analyze_version = 2")
tk.MustExec("set global tidb_partition_prune_mode = 'dynamic'")
tk.MustExec("use test")
tk.MustExec("create table t (a int, b int) partition by range (a) (PARTITION p0 VALUES LESS THAN (10), PARTITION p1 VALUES LESS THAN MAXVALUE)")
h := dom.StatsHandle()
require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh()))
tk.MustExec("insert into t values (1,2), (3,4), (11,12),(13,14)")
tk.MustExec("set session tidb_analyze_version = 2")
tk.MustExec("set session tidb_partition_prune_mode = 'dynamic'")
tk.MustExec("analyze table t")
require.False(t, h.HandleAutoAnalyze(dom.InfoSchema()))
tk.MustExec("alter table t add index idx(a)")
tbl, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
tblInfo := tbl.Meta()
idxInfo := tblInfo.Indices[0]
require.Nil(t, h.GetTableStats(tblInfo).Indices[idxInfo.ID])
require.True(t, h.HandleAutoAnalyze(dom.InfoSchema()))
require.NotNil(t, h.GetTableStats(tblInfo).Indices[idxInfo.ID])
}