Skip to content

Commit

Permalink
statistics: drop stats should delete topn (#18160)
Browse files Browse the repository at this point in the history
* statistics: drop stats should delete topn

* add rest

* fix test

* fix test

Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
  • Loading branch information
lzmhhh123 and ti-srebot authored Aug 26, 2020
1 parent ef51ea8 commit 8fe2a0b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
36 changes: 36 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,42 @@ func (s *testSuite3) TestDropStats(c *C) {
h.SetLease(0)
}

func (s *testSuite3) TestDropStatsFromKV(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t (c1 varchar(20), c2 varchar(20))")
tk.MustExec(`insert into t values("1","1"),("2","2"),("3","3"),("4","4")`)
tk.MustExec("insert into t select * from t")
tk.MustExec("insert into t select * from t")
tk.MustExec("analyze table t")
tblID := tk.MustQuery(`select tidb_table_id from information_schema.tables where table_name = "t" and table_schema = "test"`).Rows()[0][0].(string)
tk.MustQuery("select modify_count, count from mysql.stats_meta where table_id = " + tblID).Check(
testkit.Rows("0 16"))
tk.MustQuery("select hist_id from mysql.stats_histograms where table_id = " + tblID).Check(
testkit.Rows("1", "2"))
tk.MustQuery("select hist_id, bucket_id from mysql.stats_buckets where table_id = " + tblID).Check(
testkit.Rows("1 0",
"1 1",
"1 2",
"1 3",
"2 0",
"2 1",
"2 2",
"2 3"))
tk.MustQuery("select hist_id from mysql.stats_top_n where table_id = " + tblID).Check(
testkit.Rows("1", "1", "1", "1", "2", "2", "2", "2"))

tk.MustExec("drop stats t")
tk.MustQuery("select modify_count, count from mysql.stats_meta where table_id = " + tblID).Check(
testkit.Rows("0 16"))
tk.MustQuery("select hist_id from mysql.stats_histograms where table_id = " + tblID).Check(
testkit.Rows())
tk.MustQuery("select hist_id, bucket_id from mysql.stats_buckets where table_id = " + tblID).Check(
testkit.Rows())
tk.MustQuery("select hist_id from mysql.stats_top_n where table_id = " + tblID).Check(
testkit.Rows())
}

func (s *testSuite3) TestFlushTables(c *C) {
tk := testkit.NewTestKit(c, s.store)

Expand Down
4 changes: 3 additions & 1 deletion statistics/handle/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@ func (h *Handle) DeleteTableStatsFromKV(physicalID int64) (err error) {
return errors.Trace(err)
}
startTS := txn.StartTS()
sqls := make([]string, 0, 3)
sqls := make([]string, 0, 5)
// We only update the version so that other tidb will know that this table is deleted.
sqls = append(sqls, fmt.Sprintf("update mysql.stats_meta set version = %d where table_id = %d ", startTS, physicalID))
sqls = append(sqls, fmt.Sprintf("delete from mysql.stats_histograms where table_id = %d", physicalID))
sqls = append(sqls, fmt.Sprintf("delete from mysql.stats_buckets where table_id = %d", physicalID))
sqls = append(sqls, fmt.Sprintf("delete from mysql.stats_top_n where table_id = %d", physicalID))
sqls = append(sqls, fmt.Sprintf("delete from mysql.stats_feedback where table_id = %d", physicalID))
return execSQLs(context.Background(), exec, sqls)
}

0 comments on commit 8fe2a0b

Please sign in to comment.