Skip to content

Commit

Permalink
statistics: add GC for mysql.column_stats_usage (#29183)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyifangreeneyes authored Oct 29, 2021
1 parent 6a62f9d commit c0024d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions statistics/handle/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ func (h *Handle) deleteHistStatsFromKV(physicalID int64, histID int64, isIndex i
if _, err := exec.ExecuteInternal(ctx, "delete from mysql.stats_fm_sketch where table_id = %? and hist_id = %? and is_index = %?", physicalID, histID, isIndex); err != nil {
return err
}
if isIndex == 0 {
// delete the record in mysql.column_stats_usage
if _, err = exec.ExecuteInternal(ctx, "delete from mysql.column_stats_usage where table_id = %? and column_id = %?", physicalID, histID); err != nil {
return err
}
}
return nil
}

Expand Down Expand Up @@ -221,6 +227,9 @@ func (h *Handle) DeleteTableStatsFromKV(statsIDs []int64) (err error) {
if _, err = exec.ExecuteInternal(ctx, "delete from mysql.stats_fm_sketch where table_id = %?", statsID); err != nil {
return err
}
if _, err = exec.ExecuteInternal(ctx, "delete from mysql.column_stats_usage where table_id = %?", statsID); err != nil {
return err
}
}
return nil
}
Expand Down
20 changes: 20 additions & 0 deletions statistics/handle/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,23 @@ func (s *testStatsSuite) TestGCExtendedStats(c *C) {
c.Assert(h.GCStats(s.do.InfoSchema(), ddlLease), IsNil)
testKit.MustQuery("select name, type, column_ids, stats, status from mysql.stats_extended").Sort().Check(testkit.Rows())
}

func (s *testStatsSuite) TestGCColumnStatsUsage(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("create table t(a int, b int, c int)")
testKit.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3)")
testKit.MustExec("analyze table t")
testKit.MustQuery("select count(*) from mysql.column_stats_usage").Check(testkit.Rows("3"))
testKit.MustExec("alter table t drop column a")
testKit.MustQuery("select count(*) from mysql.column_stats_usage").Check(testkit.Rows("3"))
h := s.do.StatsHandle()
ddlLease := time.Duration(0)
c.Assert(h.GCStats(s.do.InfoSchema(), ddlLease), IsNil)
testKit.MustQuery("select count(*) from mysql.column_stats_usage").Check(testkit.Rows("2"))
testKit.MustExec("drop table t")
testKit.MustQuery("select count(*) from mysql.column_stats_usage").Check(testkit.Rows("2"))
c.Assert(h.GCStats(s.do.InfoSchema(), ddlLease), IsNil)
testKit.MustQuery("select count(*) from mysql.column_stats_usage").Check(testkit.Rows("0"))
}

0 comments on commit c0024d7

Please sign in to comment.