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: use histogram count rather than realtime count to calculate stats healthy #41031

Merged
merged 4 commits into from
Feb 3, 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
2 changes: 1 addition & 1 deletion executor/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func TestShowStatsHealthy(t *testing.T) {
require.NoError(t, err)
err = do.StatsHandle().Update(do.InfoSchema())
require.NoError(t, err)
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 19"))
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 0"))
tk.MustExec("analyze table t")
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 100"))
tk.MustExec("delete from t")
Expand Down
2 changes: 1 addition & 1 deletion statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ partition by range (a) (
require.NoError(t, dom.StatsHandle().DumpStatsDeltaToKV(handle.DumpAll))
require.NoError(t, dom.StatsHandle().Update(dom.InfoSchema()))
checkModifyAndCount(4, 10, 2, 4, 2, 6)
checkHealthy(60, 50, 66)
checkHealthy(33, 0, 50)
}

func TestGlobalStatsData(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions statistics/handle/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2212,19 +2212,30 @@ func TestAutoAnalyzeRatio(t *testing.T) {
tk.MustExec("set global tidb_auto_analyze_start_time='00:00 +0000'")
tk.MustExec("set global tidb_auto_analyze_end_time='23:59 +0000'")

getStatsHealthy := func() int {
rows := tk.MustQuery("show stats_healthy where db_name = 'test' and table_name = 't'").Rows()
require.Len(t, rows, 1)
healthy, err := strconv.Atoi(rows[0][3].(string))
require.NoError(t, err)
return healthy
}

tk.MustExec("insert into t values (1)" + strings.Repeat(", (1)", 10))
require.NoError(t, h.DumpStatsDeltaToKV(handle.DumpAll))
require.NoError(t, h.Update(is))
require.Equal(t, getStatsHealthy(), 44)
require.True(t, h.HandleAutoAnalyze(is))

tk.MustExec("delete from t limit 12")
require.NoError(t, h.DumpStatsDeltaToKV(handle.DumpAll))
require.NoError(t, h.Update(is))
require.Equal(t, getStatsHealthy(), 61)
require.False(t, h.HandleAutoAnalyze(is))

tk.MustExec("delete from t limit 4")
require.NoError(t, h.DumpStatsDeltaToKV(handle.DumpAll))
require.NoError(t, h.Update(is))
require.Equal(t, getStatsHealthy(), 48)
require.True(t, h.HandleAutoAnalyze(dom.InfoSchema()))
}

Expand Down
13 changes: 12 additions & 1 deletion statistics/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,26 +520,37 @@ func TestOutdatedStatsCheck(t *testing.T) {
tk.MustExec("explain select * from t where a = 1")
require.NoError(t, h.LoadNeededHistograms())

getStatsHealthy := func() int {
rows := tk.MustQuery("show stats_healthy where db_name = 'test' and table_name = 't'").Rows()
require.Len(t, rows, 1)
healthy, err := strconv.Atoi(rows[0][3].(string))
require.NoError(t, err)
return healthy
}

tk.MustExec("insert into t values (1)" + strings.Repeat(", (1)", 13)) // 34 rows
require.NoError(t, h.DumpStatsDeltaToKV(handle.DumpAll))
require.NoError(t, h.Update(is))
require.Equal(t, getStatsHealthy(), 30)
require.False(t, hasPseudoStats(tk.MustQuery("explain select * from t where a = 1").Rows()))

tk.MustExec("insert into t values (1)") // 35 rows
require.NoError(t, h.DumpStatsDeltaToKV(handle.DumpAll))
require.NoError(t, h.Update(is))
require.Equal(t, getStatsHealthy(), 25)
require.True(t, hasPseudoStats(tk.MustQuery("explain select * from t where a = 1").Rows()))

tk.MustExec("analyze table t")

tk.MustExec("delete from t limit 24") // 11 rows
require.NoError(t, h.DumpStatsDeltaToKV(handle.DumpAll))
require.NoError(t, h.Update(is))
require.Equal(t, getStatsHealthy(), 31)
require.False(t, hasPseudoStats(tk.MustQuery("explain select * from t where a = 1").Rows()))

tk.MustExec("delete from t limit 1") // 10 rows
require.NoError(t, h.DumpStatsDeltaToKV(handle.DumpAll))
require.NoError(t, h.Update(is))
require.Equal(t, getStatsHealthy(), 28)
require.True(t, hasPseudoStats(tk.MustQuery("explain select * from t where a = 1").Rows()))
}

Expand Down
8 changes: 6 additions & 2 deletions statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,12 @@ func (t *Table) GetStatsHealthy() (int64, bool) {
return 0, false
}
var healthy int64
if t.ModifyCount < t.Count {
healthy = int64((1.0 - float64(t.ModifyCount)/float64(t.Count)) * 100.0)
count := float64(t.Count)
if histCount := t.GetColRowCount(); histCount > 0 {
count = histCount
}
if float64(t.ModifyCount) < count {
healthy = int64((1.0 - float64(t.ModifyCount)/count) * 100.0)
} else if t.ModifyCount == 0 {
healthy = 100
}
Expand Down