From f0725f877c39e786477052859c0a378365771282 Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Thu, 30 Aug 2018 20:21:24 +0800 Subject: [PATCH] stats: fix auto analyze trigger condition (#7550) (#7556) --- statistics/update.go | 8 ++++---- statistics/update_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/statistics/update.go b/statistics/update.go index 6564a68e784e5..05c49940a4dbd 100644 --- a/statistics/update.go +++ b/statistics/update.go @@ -386,10 +386,10 @@ const ( // AutoAnalyzeMinCnt means if the count of table is less than this value, we needn't do auto analyze. var AutoAnalyzeMinCnt int64 = 1000 -// tableAnalyzed checks if the table is analyzed. -func tableAnalyzed(tbl *Table) bool { +// TableAnalyzed checks if the table is analyzed. +func TableAnalyzed(tbl *Table) bool { for _, col := range tbl.Columns { - if col.Histogram.Len() > 0 { + if col.Count > 0 { return true } } @@ -407,7 +407,7 @@ func tableAnalyzed(tbl *Table) bool { // 2. If the table had been analyzed before, we need to analyze it when // "tbl.ModifyCount/tbl.Count > autoAnalyzeRatio". func needAnalyzeTable(tbl *Table, limit time.Duration, autoAnalyzeRatio float64) bool { - analyzed := tableAnalyzed(tbl) + analyzed := TableAnalyzed(tbl) if !analyzed { t := time.Unix(0, oracle.ExtractPhysical(tbl.Version)*int64(time.Millisecond)) return time.Since(t) >= limit diff --git a/statistics/update_test.go b/statistics/update_test.go index e6777799f233d..49da14e4de8d2 100644 --- a/statistics/update_test.go +++ b/statistics/update_test.go @@ -393,6 +393,40 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) { c.Assert(hg.Len(), Equals, 3) } +func (s *testStatsUpdateSuite) TestTableAnalyzed(c *C) { + defer cleanEnv(c, s.store, s.do) + testKit := testkit.NewTestKit(c, s.store) + testKit.MustExec("use test") + testKit.MustExec("create table t (a int)") + testKit.MustExec("insert into t values (1)") + + is := s.do.InfoSchema() + tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) + c.Assert(err, IsNil) + tableInfo := tbl.Meta() + h := s.do.StatsHandle() + + h.Update(is) + statsTbl := h.GetTableStats(tableInfo) + c.Assert(statistics.TableAnalyzed(statsTbl), IsFalse) + + testKit.MustExec("analyze table t") + h.Update(is) + statsTbl = h.GetTableStats(tableInfo) + c.Assert(statistics.TableAnalyzed(statsTbl), IsTrue) + + h.Clear() + oriLease := h.Lease + // set it to non-zero so we will use load by need strategy + h.Lease = 1 + defer func() { + h.Lease = oriLease + }() + h.Update(is) + statsTbl = h.GetTableStats(tableInfo) + c.Assert(statistics.TableAnalyzed(statsTbl), IsTrue) +} + func (s *testStatsUpdateSuite) TestZeroColSizeUpdate(c *C) { defer cleanEnv(c, s.store, s.do) testKit := testkit.NewTestKit(c, s.store)