diff --git a/bindinfo/bind_test.go b/bindinfo/bind_test.go index 1e2281ee20438..8be8eb4ce5b9d 100644 --- a/bindinfo/bind_test.go +++ b/bindinfo/bind_test.go @@ -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) } } @@ -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", )) } @@ -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) { @@ -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", )) } diff --git a/bindinfo/handle.go b/bindinfo/handle.go index 21abe241151f4..1301f6e2556ac 100644 --- a/bindinfo/handle.go +++ b/bindinfo/handle.go @@ -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 } @@ -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 diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 14e1f6e99b390..51629df4ada75 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -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()`)