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: trigger auto-analyze based on histogram row count (#24382) #26707

Merged
merged 4 commits into from
Aug 30, 2021
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
8 changes: 6 additions & 2 deletions statistics/handle/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,14 @@ func NeedAnalyzeTable(tbl *statistics.Table, limit time.Duration, autoAnalyzeRat
return false, ""
}
// No need to analyze it.
if float64(tbl.ModifyCount)/float64(tbl.Count) <= autoAnalyzeRatio {
tblCnt := float64(tbl.Count)
if histCnt := tbl.ColHistCount(); histCnt > 0 {
tblCnt = histCnt
}
if float64(tbl.ModifyCount)/tblCnt <= autoAnalyzeRatio {
return false, ""
}
return true, fmt.Sprintf("too many modifications(%v/%v>%v)", tbl.ModifyCount, tbl.Count, autoAnalyzeRatio)
return true, fmt.Sprintf("too many modifications(%v/%v>%v)", tbl.ModifyCount, tblCnt, autoAnalyzeRatio)
}

func (h *Handle) getAutoAnalyzeParameters() map[string]string {
Expand Down
44 changes: 44 additions & 0 deletions statistics/handle/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2233,3 +2233,47 @@ func (s *testSerialStatsSuite) TestAutoUpdatePartitionInDynamicOnlyMode(c *C) {
c.Assert(partitionStats.ModifyCount, Equals, int64(0))
})
}

func (s *testSerialStatsSuite) TestAutoAnalyzeRatio(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)

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)
handle.AutoAnalyzeMinCnt = 0
defer func() {
handle.AutoAnalyzeMinCnt = 1000
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))
}()

h := s.do.StatsHandle()
tk.MustExec("use test")
tk.MustExec("create table t (a int)")
c.Assert(h.HandleDDLEvent(<-h.DDLEventCh()), IsNil)
tk.MustExec("insert into t values (1)" + strings.Repeat(", (1)", 19))
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
is := s.do.InfoSchema()
c.Assert(h.Update(is), IsNil)
// To pass the stats.Pseudo check in autoAnalyzeTable
tk.MustExec("analyze table t")
tk.MustExec("explain select * from t where a = 1")
c.Assert(h.LoadNeededHistograms(), IsNil)
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("insert into t values (1)" + strings.Repeat(", (1)", 10))
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
c.Assert(h.HandleAutoAnalyze(is), IsTrue)

tk.MustExec("delete from t limit 12")
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
c.Assert(h.HandleAutoAnalyze(is), IsFalse)

tk.MustExec("delete from t limit 4")
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
c.Assert(h.HandleAutoAnalyze(s.do.InfoSchema()), IsTrue)
}
10 changes: 10 additions & 0 deletions statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ func (t *Table) GetStatsInfo(ID int64, isIndex bool) (int64, *Histogram, *CMSket
return int64(colStatsInfo.TotalRowCount()), colStatsInfo.Histogram.Copy(), colStatsInfo.CMSketch.Copy(), colStatsInfo.TopN.Copy(), colStatsInfo.FMSketch.Copy()
}

// ColHistCount returns the count of the column histograms.
func (t *Table) ColHistCount() float64 {
for _, col := range t.Columns {
if col != nil {
return col.TotalRowCount()
}
}
return -1
}

type tableColumnID struct {
TableID int64
ColumnID int64
Expand Down