From b5d078b41b006b524e06363caec462e6d39bf73d Mon Sep 17 00:00:00 2001 From: Han Fei Date: Tue, 6 Feb 2018 18:23:48 +0800 Subject: [PATCH] stats: set a min count for auto analyze (#5796) --- statistics/update.go | 5 ++++- statistics/update_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/statistics/update.go b/statistics/update.go index b5775c3790..f55c48d477 100644 --- a/statistics/update.go +++ b/statistics/update.go @@ -236,8 +236,11 @@ const ( StatsPrompt = "stats" ) +// AutoAnalyzeMinCnt means if the count of table is less than this value, we needn't do auto analyze. +var AutoAnalyzeMinCnt int64 = 1000 + func needAnalyzeTable(tbl *Table, limit time.Duration) bool { - if tbl.ModifyCount == 0 { + if tbl.ModifyCount == 0 || tbl.Count < AutoAnalyzeMinCnt { return false } t := time.Unix(0, oracle.ExtractPhysical(tbl.Version)*int64(time.Millisecond)) diff --git a/statistics/update_test.go b/statistics/update_test.go index f00ea08a9d..cc8d73968a 100644 --- a/statistics/update_test.go +++ b/statistics/update_test.go @@ -21,6 +21,7 @@ import ( "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/model" + "github.com/pingcap/tidb/statistics" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/testkit" "github.com/pingcap/tidb/util/testleak" @@ -269,6 +270,11 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) { testKit.MustExec("use test") testKit.MustExec("create table t (a int)") + statistics.AutoAnalyzeMinCnt = 0 + defer func() { + statistics.AutoAnalyzeMinCnt = 1000 + }() + do := s.do is := do.InfoSchema() tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))