Skip to content

Commit

Permalink
planner: logically delete the bindinfo when create the new binding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Reminiscent authored Aug 6, 2021
1 parent fff85d0 commit 300f159
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
16 changes: 10 additions & 6 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func (s *testSuite) TestGlobalBinding(c *C) {
c.Check(err, IsNil)
c.Check(chk.NumRows(), Equals, 0)

_, err = tk.Exec("delete from mysql.bind_info")
_, err = tk.Exec("delete from mysql.bind_info where source != 'builtin'")
c.Assert(err, IsNil)
}
}
Expand Down Expand Up @@ -1102,6 +1102,7 @@ func (s *testSuite) TestBaselineDBLowerCase(c *C) {
// default_db should have lower case.
c.Assert(rows[0][2], Equals, "spm")
tk.MustQuery("select original_sql, default_db, status from mysql.bind_info where original_sql = 'select * from `spm` . `t`'").Check(testkit.Rows(
"select * from `spm` . `t` SPM deleted",
"select * from `spm` . `t` spm using",
))
}
Expand Down Expand Up @@ -2015,13 +2016,15 @@ func (s *testSuite) TestReCreateBind(c *C) {
c.Assert(rows[0][3], Equals, "using")

tk.MustExec("create global binding for select * from t using select * from t")
tk.MustQuery("select original_sql, status from mysql.bind_info").Check(testkit.Rows(
"select * from `test` . `t` using",
))
rows = tk.MustQuery("show global bindings").Rows()
c.Assert(len(rows), Equals, 1)
c.Assert(rows[0][0], Equals, "select * from `test` . `t`")
c.Assert(rows[0][3], Equals, "using")

rows = tk.MustQuery("select original_sql, status from mysql.bind_info where source != 'builtin';").Rows()
c.Assert(len(rows), Equals, 2)
c.Assert(rows[0][1], Equals, "deleted")
c.Assert(rows[1][1], Equals, "using")
}

func (s *testSuite) TestExplainShowBindSQL(c *C) {
Expand Down Expand Up @@ -2096,8 +2099,9 @@ func (s *testSuite) TestConcurrentCapture(c *C) {
tk.MustExec("select * from t")
tk.MustExec("select * from t")
tk.MustExec("admin capture bindings")
tk.MustQuery("select original_sql, source from mysql.bind_info where source != 'builtin'").Check(testkit.Rows(
"select * from `test` . `t` capture",
tk.MustQuery("select original_sql, source, status from mysql.bind_info where source != 'builtin'").Check(testkit.Rows(
"select * from `test` . `t` manual deleted",
"select * from `test` . `t` capture using",
))
}

Expand Down
12 changes: 7 additions & 5 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (h *BindHandle) Update(fullLoad bool) (err error) {

exec := h.sctx.Context.(sqlexec.RestrictedSQLExecutor)
stmt, err := exec.ParseWithParams(context.TODO(), `SELECT original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source
FROM mysql.bind_info WHERE update_time > %? ORDER BY update_time`, updateTime)
FROM mysql.bind_info WHERE update_time > %? ORDER BY update_time, create_time`, updateTime)
if err != nil {
return err
}
Expand Down Expand Up @@ -218,14 +218,16 @@ func (h *BindHandle) CreateBindRecord(sctx sessionctx.Context, record *BindRecor
if err = h.lockBindInfoTable(); err != nil {
return err
}
// Binding recreation should physically delete previous bindings.
_, err = exec.ExecuteInternal(context.TODO(), `DELETE FROM mysql.bind_info WHERE original_sql = %?`, record.OriginalSQL)

now := types.NewTime(types.FromGoTime(time.Now()), mysql.TypeTimestamp, 3)

updateTs := now.String()
_, err = exec.ExecuteInternal(context.TODO(), `UPDATE mysql.bind_info SET status = %?, update_time = %? WHERE original_sql = %? AND update_time < %?`,
deleted, updateTs, record.OriginalSQL, updateTs)
if err != nil {
return err
}

now := types.NewTime(types.FromGoTime(time.Now()), mysql.TypeTimestamp, 3)

for i := range record.Bindings {
record.Bindings[i].CreateTime = now
record.Bindings[i].UpdateTime = now
Expand Down
2 changes: 1 addition & 1 deletion infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ func (s *testTableSuite) TestStmtSummaryInternalQuery(c *C) {
"where digest_text like \"select `original_sql` , `bind_sql` , `default_db` , status%\""
tk.MustQuery(sql).Check(testkit.Rows(
"select `original_sql` , `bind_sql` , `default_db` , status , `create_time` , `update_time` , charset , " +
"collation , source from `mysql` . `bind_info` where `update_time` > ? order by `update_time`"))
"collation , source from `mysql` . `bind_info` where `update_time` > ? order by `update_time` , `create_time`"))

// Test for issue #21642.
tk.MustQuery(`select tidb_version()`)
Expand Down

0 comments on commit 300f159

Please sign in to comment.