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: do not wait for data unchanged when auto analyze #7022

Merged
merged 7 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 22 additions & 13 deletions statistics/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,28 +533,37 @@ const (
// AutoAnalyzeMinCnt means if the count of table is less than this value, we needn't do auto analyze.
var AutoAnalyzeMinCnt int64 = 1000

func tableAnalyzed(tbl *Table) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need comments.

for _, col := range tbl.Columns {
if col.Count > 0 {
return true
}
}
for _, idx := range tbl.Indices {
if idx.Len() > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the idx.Len()? The length of histogram?

return true
}
}
return false
}

func needAnalyzeTable(tbl *Table, limit time.Duration, autoAnalyzeRatio float64) bool {
if tbl.ModifyCount == 0 || tbl.Count < AutoAnalyzeMinCnt {
return false
}
analyzed := tableAnalyzed(tbl)
t := time.Unix(0, oracle.ExtractPhysical(tbl.Version)*int64(time.Millisecond))
if time.Since(t) < limit {
return false
}
if autoAnalyzeRatio > 0 && float64(tbl.ModifyCount)/float64(tbl.Count) > autoAnalyzeRatio {
if !analyzed && time.Since(t) >= limit {
return true
}
for _, col := range tbl.Columns {
if col.Count > 0 {
return false
}
// Auto analyze is closed.
if autoAnalyzeRatio == 0 {
return false
}
for _, idx := range tbl.Indices {
if idx.Len() > 0 {
return false
}
if analyzed && float64(tbl.ModifyCount)/float64(tbl.Count) > autoAnalyzeRatio {
return true
}
return true
return false
}

const minAutoAnalyzeRatio = 0.3
Expand Down
7 changes: 3 additions & 4 deletions statistics/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
break
}

// Test that even if the table is recently modified, we can still analyze the table.
h.Lease = time.Second
defer func() { h.Lease = 0 }()
_, err = testKit.Exec("insert into t values ('fff')")
c.Assert(err, IsNil)
c.Assert(h.DumpStatsDeltaToKV(statistics.DumpAll), IsNil)
Expand All @@ -363,11 +366,7 @@ func (s *testStatsUpdateSuite) TestAutoUpdate(c *C) {
_, err = testKit.Exec("insert into t values ('eee')")
c.Assert(err, IsNil)
h.DumpStatsDeltaToKV(statistics.DumpAll)
h.Clear()
// We set `Lease` here so that `Update` will use load by need strategy.
h.Lease = time.Second
h.Update(is)
h.Lease = 0
err = h.HandleAutoAnalyze(is)
c.Assert(err, IsNil)
h.Update(is)
Expand Down