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

Merged
merged 14 commits into from
Jul 28, 2021
Merged
8 changes: 6 additions & 2 deletions statistics/handle/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,10 +879,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 @@ -2284,3 +2284,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 {
Copy link
Member

Choose a reason for hiding this comment

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

The name could be changed since the topn is split out of the histogram.

for _, col := range t.Columns {
if col != nil {
Comment on lines +218 to +220
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to check Table.Pseudo here.
Probably Column.IsInvalid() also.

Copy link
Member

Choose a reason for hiding this comment

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

Why do we only try to get row count from columns? I think indexes are also useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

1, autoAnalyzeTable has already checked Table.Pseudo;
2, We should NOT check Column.IsInvalid(), since our purpose here is to get the row count of last analyze, even if Column.IsInvalid() is true, the row count is what we want;
3, We should NOT get row count from indexes, normally, index count should be same as column count, but if analyze index happens, index count is not what we want;

Copy link
Member

@time-and-fate time-and-fate May 10, 2021

Choose a reason for hiding this comment

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

OK. 👌

There is one special case. When the stats for a column is not loaded and there are NULLs in this column, the (*Column).TotalRowCount() will equal null count, which is incorrect. We need to check against this case.

return col.TotalRowCount()
}
}
return -1
}

type tableColumnID struct {
TableID int64
ColumnID int64
Expand Down