Skip to content

Commit

Permalink
statistics: analyzed table by manual should be updated in lru (#34637)
Browse files Browse the repository at this point in the history
ref #34052
  • Loading branch information
Yisaer authored May 15, 2022
1 parent 333c623 commit bb66294
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
26 changes: 24 additions & 2 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ func (h *Handle) Update(is infoschema.InfoSchema, opts ...TableStatsOpt) error {
if err != nil {
return errors.Trace(err)
}
option := &tableStatsOption{}
for _, opt := range opts {
opt(option)
}
for _, row := range rows {
version := row.GetUint64(0)
physicalID := row.GetInt64(1)
Expand Down Expand Up @@ -293,9 +297,13 @@ func (h *Handle) Update(is infoschema.InfoSchema, opts ...TableStatsOpt) error {
tbl.ModifyCount = modifyCount
tbl.Name = getFullTableName(is, tableInfo)
tbl.TblInfoUpdateTS = tableInfo.UpdateTS
oldCache.Put(physicalID, tbl)
if option.byQuery {
oldCache.PutByQuery(physicalID, tbl)
} else {
oldCache.Put(physicalID, tbl)
}
}
h.updateStatsCache(oldCache.update(nil, nil, lastVersion, opts...))
h.updateStatsCache(oldCache.update(nil, nil, lastVersion))
return nil
}

Expand Down Expand Up @@ -2076,3 +2084,17 @@ func (h *Handle) SetStatsCacheCapacity(c int64) {
sc := v.(statsCache)
sc.SetCapacity(c)
}

// GetStatsCacheFrontTable gets front table in statsCacheInner implementation
// only used for test
func (h *Handle) GetStatsCacheFrontTable() int64 {
if h == nil {
return 0
}
v := h.statsCache.Load()
if v == nil {
return 0
}
sc := v.(statsCache)
return sc.Front()
}
27 changes: 27 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3344,3 +3344,30 @@ func TestAnalyzeIncrementalEvictedIndex(t *testing.T) {
tk.MustExec("analyze incremental table test.t index idx_b")
require.Nil(t, failpoint.Disable("github.com/pingcap/tidb/executor/assertEvictIndex"))
}

func TestAnalyzeTableLRUPut(t *testing.T) {
restore := config.RestoreFunc()
defer restore()
config.UpdateGlobal(func(conf *config.Config) {
conf.Performance.EnableStatsCacheMemQuota = true
})
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("set @@tidb_analyze_version = 1")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b varchar(10), index idx_b (b))")
tk.MustExec("create table t1(a int, b varchar(10), index idx_b (b))")
tk.MustExec("analyze table test.t")
tbl, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.Nil(t, err)
tbl1, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.Nil(t, err)
// assert t1 should be front of lru
tk.MustExec("analyze table test.t1")
require.Equal(t, tbl1.Meta().ID, domain.GetDomain(tk.Session()).StatsHandle().GetStatsCacheFrontTable())
// assert t should be front of lru
tk.MustExec("analyze table test.t")
require.Equal(t, tbl.Meta().ID, domain.GetDomain(tk.Session()).StatsHandle().GetStatsCacheFrontTable())
}
11 changes: 11 additions & 0 deletions statistics/handle/lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ func (s *statsInnerCache) EnableQuota() bool {
return true
}

// Front implements statsCacheInner
func (s *statsInnerCache) Front() int64 {
s.RLock()
defer s.RUnlock()
ele := s.lru.cache.Front()
if ele == nil {
return 0
}
return s.lru.cache.Front().Value.(*lruCacheItem).tblID
}

func (s *statsInnerCache) onEvict(tblID int64) {
element, exist := s.elements[tblID]
if !exist {
Expand Down
7 changes: 7 additions & 0 deletions statistics/handle/statscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type statsCacheInner interface {
Copy() statsCacheInner
SetCapacity(int64)
EnableQuota() bool
// Front returns the front element's owner tableID, only used for test
Front() int64
}

func newStatsCache() statsCache {
Expand Down Expand Up @@ -253,3 +255,8 @@ func (m *mapCache) SetCapacity(int64) {}
func (m *mapCache) EnableQuota() bool {
return false
}

// Front implements statsCacheInner
func (m *mapCache) Front() int64 {
return 0
}

0 comments on commit bb66294

Please sign in to comment.