Skip to content

Commit

Permalink
stats: fix auto analyze trigger condition (#7550) (#7556)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and zz-jason committed Aug 30, 2018
1 parent 9d09f1d commit f0725f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
8 changes: 4 additions & 4 deletions statistics/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions statistics/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f0725f8

Please sign in to comment.