From 8be4dd432a7da8e9f7d1750d2259d999d8dea0b5 Mon Sep 17 00:00:00 2001 From: "Zhuomin(Charming) Liu" Date: Wed, 26 Aug 2020 14:15:51 +0800 Subject: [PATCH] cherry pick #18160 to release-4.0 Signed-off-by: ti-srebot --- executor/simple_test.go | 36 ++++++++++++++++++++++++++++++++++++ statistics/handle/gc.go | 4 +++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/executor/simple_test.go b/executor/simple_test.go index b01f5966e89c4..cddfe4940cbf2 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -594,6 +594,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) diff --git a/statistics/handle/gc.go b/statistics/handle/gc.go index 40824a0d74294..3264485e6b703 100644 --- a/statistics/handle/gc.go +++ b/statistics/handle/gc.go @@ -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) }