diff --git a/executor/simple_test.go b/executor/simple_test.go index 82accbc3ddd6f..70c5a568628b9 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -578,6 +578,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 5f1d9b907c3a9..ba04e96dcdcf4 100644 --- a/statistics/handle/gc.go +++ b/statistics/handle/gc.go @@ -149,6 +149,7 @@ func (h *Handle) DeleteTableStatsFromKV(physicalID int64) (err error) { return errors.Trace(err) } startTS := txn.StartTS() +<<<<<<< HEAD // We only update the version so that other tidb will know that this table is deleted. sql := fmt.Sprintf("update mysql.stats_meta set version = %d where table_id = %d ", startTS, physicalID) _, err = exec.Execute(context.Background(), sql) @@ -161,4 +162,14 @@ func (h *Handle) DeleteTableStatsFromKV(physicalID int64) (err error) { } _, err = exec.Execute(context.Background(), fmt.Sprintf("delete from mysql.stats_buckets where table_id = %d", physicalID)) return +======= + 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) +>>>>>>> 8fe2a0b... statistics: drop stats should delete topn (#18160) }