diff --git a/sessionctx/variable/varsutil.go b/sessionctx/variable/varsutil.go index 2da6c0a258a55..627eadda7c273 100644 --- a/sessionctx/variable/varsutil.go +++ b/sessionctx/variable/varsutil.go @@ -537,6 +537,12 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string, return "", err } return v, nil + case TiDBAutoAnalyzeRatio: + v, err := strconv.ParseFloat(value, 64) + if err != nil || v < 0 { + return value, ErrWrongValueForVar.GenWithStackByArgs(name, value) + } + return value, nil case TxnIsolation, TransactionIsolation: upVal := strings.ToUpper(value) _, exists := TxIsolationNames[upVal] diff --git a/statistics/handle/update.go b/statistics/handle/update.go index 73187a49dfe44..9b60683d39b08 100644 --- a/statistics/handle/update.go +++ b/statistics/handle/update.go @@ -636,13 +636,9 @@ func NeedAnalyzeTable(tbl *statistics.Table, limit time.Duration, autoAnalyzeRat return false, "" } // Tests if current time is within the time period. - return timeutil.WithinDayTimePeriod(start, end, now), fmt.Sprintf("too many modifications(%v/%v)", tbl.ModifyCount, tbl.Count) + return timeutil.WithinDayTimePeriod(start, end, now), fmt.Sprintf("too many modifications(%v/%v>%v)", tbl.ModifyCount, tbl.Count, autoAnalyzeRatio) } -const ( - minAutoAnalyzeRatio = 0.3 -) - func (h *Handle) getAutoAnalyzeParameters() map[string]string { sql := fmt.Sprintf("select variable_name, variable_value from mysql.global_variables where variable_name in ('%s', '%s', '%s')", variable.TiDBAutoAnalyzeRatio, variable.TiDBAutoAnalyzeStartTime, variable.TiDBAutoAnalyzeEndTime) @@ -662,10 +658,7 @@ func parseAutoAnalyzeRatio(ratio string) float64 { if err != nil { return variable.DefAutoAnalyzeRatio } - if autoAnalyzeRatio > 0 { - autoAnalyzeRatio = math.Max(autoAnalyzeRatio, minAutoAnalyzeRatio) - } - return autoAnalyzeRatio + return math.Max(autoAnalyzeRatio, 0) } func parseAnalyzePeriod(start, end string) (time.Time, time.Time, error) { diff --git a/statistics/handle/update_test.go b/statistics/handle/update_test.go index 3ea8b93e311b0..8109f932bd801 100644 --- a/statistics/handle/update_test.go +++ b/statistics/handle/update_test.go @@ -375,7 +375,7 @@ func (s *testStatsSuite) TestAutoUpdate(c *C) { testKit.MustExec("create table t (a varchar(20))") handle.AutoAnalyzeMinCnt = 0 - testKit.MustExec("set global tidb_auto_analyze_ratio = 0.6") + testKit.MustExec("set global tidb_auto_analyze_ratio = 0.2") defer func() { handle.AutoAnalyzeMinCnt = 1000 testKit.MustExec("set global tidb_auto_analyze_ratio = 0.0") @@ -393,18 +393,18 @@ func (s *testStatsSuite) TestAutoUpdate(c *C) { stats := h.GetTableStats(tableInfo) c.Assert(stats.Count, Equals, int64(0)) - _, err = testKit.Exec("insert into t values ('ss')") + _, err = testKit.Exec("insert into t values ('ss'), ('ss'), ('ss'), ('ss'), ('ss')") c.Assert(err, IsNil) c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil) c.Assert(h.Update(is), IsNil) h.HandleAutoAnalyze(is) c.Assert(h.Update(is), IsNil) stats = h.GetTableStats(tableInfo) - c.Assert(stats.Count, Equals, int64(1)) + c.Assert(stats.Count, Equals, int64(5)) c.Assert(stats.ModifyCount, Equals, int64(0)) for _, item := range stats.Columns { - // TotColSize = 2(length of 'ss') + 1(size of len byte). - c.Assert(item.TotColSize, Equals, int64(3)) + // TotColSize = 5*(2(length of 'ss') + 1(size of len byte)). + c.Assert(item.TotColSize, Equals, int64(15)) break } @@ -418,7 +418,7 @@ func (s *testStatsSuite) TestAutoUpdate(c *C) { h.HandleAutoAnalyze(is) c.Assert(h.Update(is), IsNil) stats = h.GetTableStats(tableInfo) - c.Assert(stats.Count, Equals, int64(2)) + c.Assert(stats.Count, Equals, int64(6)) c.Assert(stats.ModifyCount, Equals, int64(1)) _, err = testKit.Exec("insert into t values ('fff')") @@ -428,7 +428,7 @@ func (s *testStatsSuite) TestAutoUpdate(c *C) { h.HandleAutoAnalyze(is) c.Assert(h.Update(is), IsNil) stats = h.GetTableStats(tableInfo) - c.Assert(stats.Count, Equals, int64(3)) + c.Assert(stats.Count, Equals, int64(7)) c.Assert(stats.ModifyCount, Equals, int64(0)) _, err = testKit.Exec("insert into t values ('eee')") @@ -438,12 +438,12 @@ func (s *testStatsSuite) TestAutoUpdate(c *C) { h.HandleAutoAnalyze(is) c.Assert(h.Update(is), IsNil) stats = h.GetTableStats(tableInfo) - c.Assert(stats.Count, Equals, int64(4)) + c.Assert(stats.Count, Equals, int64(8)) // Modify count is non-zero means that we do not analyze the table. c.Assert(stats.ModifyCount, Equals, int64(1)) for _, item := range stats.Columns { - // TotColSize = 6, because the table has not been analyzed, and insert statement will add 3(length of 'eee') to TotColSize. - c.Assert(item.TotColSize, Equals, int64(15)) + // TotColSize = 27, because the table has not been analyzed, and insert statement will add 3(length of 'eee') to TotColSize. + c.Assert(item.TotColSize, Equals, int64(27)) break } @@ -457,7 +457,7 @@ func (s *testStatsSuite) TestAutoUpdate(c *C) { h.HandleAutoAnalyze(is) c.Assert(h.Update(is), IsNil) stats = h.GetTableStats(tableInfo) - c.Assert(stats.Count, Equals, int64(4)) + c.Assert(stats.Count, Equals, int64(8)) c.Assert(stats.ModifyCount, Equals, int64(0)) hg, ok := stats.Indices[tableInfo.Indices[0].ID] c.Assert(ok, IsTrue)