Skip to content

Commit 34a8ca3

Browse files
authored
executor: use the kv.PresumeKeyNotExists option in updateRecord() (#11720)
This change can avoid unnecessary network roundtrip when creating a primary key or unique index
1 parent 236f6dc commit 34a8ca3

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

executor/write.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/pingcap/parser/ast"
2222
"github.com/pingcap/parser/mysql"
2323
"github.com/pingcap/tidb/expression"
24+
"github.com/pingcap/tidb/kv"
2425
"github.com/pingcap/tidb/sessionctx"
2526
"github.com/pingcap/tidb/table"
2627
"github.com/pingcap/tidb/table/tables"
@@ -156,7 +157,22 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h int64, oldData
156157
if sc.DupKeyAsWarning {
157158
newHandle, err = t.AddRecord(sctx, newData, table.IsUpdate, table.SkipHandleCheck, table.WithCtx(ctx))
158159
} else {
160+
txn, err1 := sctx.Txn(true)
161+
if err1 != nil {
162+
return false, false, 0, err1
163+
}
164+
// If there are primary keys or unique indices, we have to check TiKV to ensure their uniqueness.
165+
// The PresumeKeyNotExists option could delay the check to improve performance.
166+
sessVars := sctx.GetSessionVars()
167+
if !sessVars.ConstraintCheckInPlace {
168+
// The purpose of adding the Autocommit and InTxn conditions here is for compatibility (older version TiDB behaviour).
169+
// Remove the check should not affect correctness.
170+
if sessVars.IsAutocommit() && !sessVars.InTxn() {
171+
txn.SetOption(kv.PresumeKeyNotExists, nil)
172+
}
173+
}
159174
newHandle, err = t.AddRecord(sctx, newData, table.IsUpdate, table.WithCtx(ctx))
175+
txn.DelOption(kv.PresumeKeyNotExists)
160176
}
161177

162178
if err != nil {

executor/write_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,32 @@ func (s *testSuite4) TestDeferConstraintCheckForInsert(c *C) {
24582458
tk.MustExec(`update t set i = 2 where i = 1;`)
24592459
tk.MustExec(`commit;`)
24602460
tk.MustQuery(`select * from t;`).Check(testkit.Rows("2"))
2461+
2462+
tk.MustExec(`set tidb_constraint_check_in_place = 0;`)
2463+
tk.MustExec("replace into t values (1),(2)")
2464+
tk.MustExec("begin")
2465+
_, err = tk.Exec("update t set i = 2 where i = 1")
2466+
c.Assert(err, NotNil)
2467+
_, err = tk.Exec("insert into t values (1) on duplicate key update i = i + 1")
2468+
c.Assert(err, NotNil)
2469+
tk.MustExec("rollback")
2470+
2471+
tk.MustExec(`drop table t; create table t (id int primary key, v int unique);`)
2472+
tk.MustExec(`insert into t values (1, 1)`)
2473+
tk.MustExec(`set tidb_constraint_check_in_place = 1;`)
2474+
tk.MustExec(`set @@autocommit = 0;`)
2475+
2476+
_, err = tk.Exec("insert into t values (3, 1)")
2477+
c.Assert(err, NotNil)
2478+
_, err = tk.Exec("insert into t values (1, 3)")
2479+
c.Assert(err, NotNil)
2480+
tk.MustExec("commit")
2481+
2482+
tk.MustExec(`set tidb_constraint_check_in_place = 0;`)
2483+
tk.MustExec("insert into t values (3, 1)")
2484+
tk.MustExec("insert into t values (1, 3)")
2485+
_, err = tk.Exec("commit")
2486+
c.Assert(err, NotNil)
24612487
}
24622488

24632489
func (s *testSuite4) TestDefEnumInsert(c *C) {

tools/check/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc h1:cJlkeAx
1818
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc/go.mod h1:cuNKsD1zp2v6XfE/orVX2QE1LC+i254ceGcVeDT3pTU=
1919
github.com/kisielk/errcheck v1.2.0 h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E=
2020
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
21+
github.com/kisielk/gotool v0.0.0-20161130080628-0de1eaf82fa3 h1:s/sV9geKJwXXzcrFiQdiiIFgfesbREplXWR9ZFgnGSQ=
2122
github.com/kisielk/gotool v0.0.0-20161130080628-0de1eaf82fa3/go.mod h1:jxZFDH7ILpTPQTk+E2s+z4CUas9lVNjIuKR4c5/zKgM=
2223
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
2324
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -33,6 +34,7 @@ github.com/mgechev/dots v0.0.0-20180605013149-8e09d8ea2757/go.mod h1:KQ7+USdGKfp
3334
github.com/mgechev/revive v0.0.0-20181210140514-b4cc152955fb h1:bLiKpCHe+ngBsF1o7DjZTmoffHEy2gdQ/+9NunuJ4ZY=
3435
github.com/mgechev/revive v0.0.0-20181210140514-b4cc152955fb/go.mod h1:pVHj2KvxEhotJ6Lmr7zb3YgNMX1QKt8cyp6fdPHOrzU=
3536
github.com/mozilla/tls-observatory v0.0.0-20180409132520-8791a200eb40/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
37+
github.com/nbutton23/zxcvbn-go v0.0.0-20160627004424-a22cb81b2ecd h1:hEzcdYzgmGA1zDrSYdh+OE4H43RrglXdZQ5ip/+93GU=
3638
github.com/nbutton23/zxcvbn-go v0.0.0-20160627004424-a22cb81b2ecd/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
3739
github.com/nicksnyder/go-i18n v1.10.0 h1:5AzlPKvXBH4qBzmZ09Ua9Gipyruv6uApMcrNZdo96+Q=
3840
github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2xBGjv0j365Q=
@@ -44,7 +46,9 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181
4446
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
4547
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4648
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
49+
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735 h1:7YvPJVmEeFHR1Tj9sZEYsmarJEQfMVYpd/Vyy/A8dqE=
4750
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
51+
github.com/securego/gosec v0.0.0-20181211171558-12400f9a1ca7 h1:Ca7U7/rZ+caxjW2na7wbmgmaPsoSCIlpc6sm0aWtFg0=
4852
github.com/securego/gosec v0.0.0-20181211171558-12400f9a1ca7/go.mod h1:m3KbCTwh9vLhm6AKBjE+ALesKilKcQHezI1uVOti0Ks=
4953
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
5054
golang.org/x/net v0.0.0-20170915142106-8351a756f30f/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

0 commit comments

Comments
 (0)