Skip to content

Commit

Permalink
cherry pick #24175 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
eurekaka committed Apr 28, 2021
1 parent 11a9254 commit 432bc38
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
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

0 comments on commit 432bc38

Please sign in to comment.