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

statistics: skip reading mysql.stats_histograms if cached stats is up-to-date (#24175) #24352

Merged
merged 4 commits into from
May 10, 2021
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
4 changes: 4 additions & 0 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ func (h *Handle) Update(is infoschema.InfoSchema) error {
continue
}
tableInfo := table.Meta()
if oldTbl, ok := oldCache.tables[physicalID]; ok && oldTbl.Version >= version && tableInfo.UpdateTS == oldTbl.TblInfoUpdateTS {
continue
}
tbl, err := h.tableStatsFromStorage(tableInfo, physicalID, false, 0)
// Error is not nil may mean that there are some ddl changes on this table, we will not update it.
if err != nil {
Expand All @@ -228,6 +231,7 @@ func (h *Handle) Update(is infoschema.InfoSchema) error {
tbl.Count = count
tbl.ModifyCount = modifyCount
tbl.Name = getFullTableName(is, tableInfo)
tbl.TblInfoUpdateTS = tableInfo.UpdateTS
tables = append(tables, tbl)
}
h.updateStatsCache(oldCache.update(tables, deletedTableIDs, lastVersion))
Expand Down
21 changes: 21 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,24 @@ func (s *testStatsSuite) TestCorrelation(c *C) {
c.Assert(len(result.Rows()), Equals, 1)
c.Assert(result.Rows()[0][9], Equals, "0")
}

func (s *testStatsSuite) TestStatsCacheUpdateSkip(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
do := s.do
h := do.StatsHandle()
testKit.MustExec("use test")
testKit.MustExec("create table t (c1 int, c2 int)")
testKit.MustExec("insert into t values(1, 2)")
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
testKit.MustExec("analyze table t")
is := do.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tableInfo := tbl.Meta()
statsTbl1 := h.GetTableStats(tableInfo)
c.Assert(statsTbl1.Pseudo, IsFalse)
h.Update(is)
statsTbl2 := h.GetTableStats(tableInfo)
c.Assert(statsTbl1, Equals, statsTbl2)
}
13 changes: 10 additions & 3 deletions statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ type Table struct {
HistColl
Version uint64
Name string
// TblInfoUpdateTS is the UpdateTS of the TableInfo used when filling this struct.
// It is the schema version of the corresponding table. It is used to skip redundant
// loading of stats, i.e, if the cached stats is already update-to-date with mysql.stats_xxx tables,
// and the schema of the table does not change, we don't need to load the stats for this
// table again.
TblInfoUpdateTS uint64
}

// HistColl is a collection of histogram. It collects enough information for plan to calculate the selectivity.
Expand Down Expand Up @@ -99,9 +105,10 @@ func (t *Table) Copy() *Table {
newHistColl.Indices[id] = idx
}
nt := &Table{
HistColl: newHistColl,
Version: t.Version,
Name: t.Name,
HistColl: newHistColl,
Version: t.Version,
Name: t.Name,
TblInfoUpdateTS: t.TblInfoUpdateTS,
}
return nt
}
Expand Down