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

stats: fix auto analyze trigger condition #7550

Merged
merged 2 commits into from
Aug 30, 2018
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: 4 additions & 4 deletions statistics/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,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 @@ -607,7 +607,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 @@ -405,6 +405,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) TestUpdateErrorRate(c *C) {
defer cleanEnv(c, s.store, s.do)
h := s.do.StatsHandle()
Expand Down