Skip to content

Commit

Permalink
stats: remove the lower bound of auto analyze ratio (#13995) (#14013)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and sre-bot committed Dec 12, 2019
1 parent 82e6ea0 commit 73da57f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
6 changes: 6 additions & 0 deletions sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
return "", errors.Trace(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]
Expand Down
11 changes: 2 additions & 9 deletions statistics/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,13 +644,9 @@ func NeedAnalyzeTable(tbl *Table, limit time.Duration, autoAnalyzeRatio float64,
return false, ""
}
// Tests if current time is within the time period.
return withinTimePeriod(start, end, now), fmt.Sprintf("too many modifications(%v/%v)", tbl.ModifyCount, tbl.Count)
return withinTimePeriod(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)
Expand All @@ -670,10 +666,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) {
Expand Down
22 changes: 11 additions & 11 deletions statistics/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
testKit.MustExec("create table t (a varchar(20))")

statistics.AutoAnalyzeMinCnt = 0
testKit.MustExec("set global tidb_auto_analyze_ratio = 0.6")
testKit.MustExec("set global tidb_auto_analyze_ratio = 0.2")
defer func() {
statistics.AutoAnalyzeMinCnt = 1000
testKit.MustExec("set global tidb_auto_analyze_ratio = 0.0")
Expand All @@ -341,18 +341,18 @@ func (s *testStatsUpdateSuite) 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)
h.DumpStatsDeltaToKV(statistics.DumpAll)
h.Update(is)
h.HandleAutoAnalyze(is)
h.Update(is)
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
}

Expand All @@ -366,7 +366,7 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
h.HandleAutoAnalyze(is)
h.Update(is)
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')")
Expand All @@ -376,7 +376,7 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
h.HandleAutoAnalyze(is)
h.Update(is)
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')")
Expand All @@ -386,12 +386,12 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
h.HandleAutoAnalyze(is)
h.Update(is)
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(14))
// TotColSize = 26, because the table has not been analyzed, and insert statement will add 3(length of 'eee') to TotColSize.
c.Assert(item.TotColSize, Equals, int64(26))
break
}

Expand All @@ -405,7 +405,7 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
h.HandleAutoAnalyze(is)
h.Update(is)
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)
Expand Down

0 comments on commit 73da57f

Please sign in to comment.