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: drop stats should delete topn #18160

Merged
merged 6 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
36 changes: 36 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,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)
}