Skip to content

Commit

Permalink
cherry pick pingcap#18160 to release-3.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
lzmhhh123 authored and ti-srebot committed Aug 26, 2020
1 parent dae7cba commit 484f571
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
36 changes: 36 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions statistics/handle/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 484f571

Please sign in to comment.