Skip to content

Commit

Permalink
ddl: fix issue of add foreign key too slow in big table (#40112)
Browse files Browse the repository at this point in the history
close #40111
  • Loading branch information
crazycs520 authored Dec 22, 2022
1 parent 5c04d78 commit 1305687
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 18 additions & 1 deletion ddl/fktest/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
Expand Down Expand Up @@ -1563,7 +1564,7 @@ func getLatestSchemaDiff(t *testing.T, tk *testkit.TestKit) *model.SchemaDiff {
return diff
}

func TestTestMultiSchemaAddForeignKey(t *testing.T) {
func TestMultiSchemaAddForeignKey(t *testing.T) {
store, _ := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("set @@foreign_key_checks=1;")
Expand Down Expand Up @@ -1591,3 +1592,19 @@ func TestTestMultiSchemaAddForeignKey(t *testing.T) {
tk.MustExec("create table t2 (a int, b int, index idx0(a,b), index idx1(a), index idx2(b));")
tk.MustExec("alter table t2 drop index idx1, add foreign key (a) references t1(id), add foreign key (b) references t1(id)")
}

func TestAddForeignKeyInBigTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("set @@foreign_key_checks=1;")
tk.MustExec("use test")
tk.MustExec("create table employee (id bigint auto_increment key, pid bigint)")
tk.MustExec("insert into employee (id) values (1),(2),(3),(4),(5),(6),(7),(8)")
for i := 0; i < 14; i++ {
tk.MustExec("insert into employee (pid) select pid from employee")
}
tk.MustExec("update employee set pid=id-1 where id>1")
start := time.Now()
tk.MustExec("alter table employee add foreign key fk_1(pid) references employee(id)")
require.Less(t, time.Since(start), time.Minute)
}
9 changes: 7 additions & 2 deletions ddl/foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,12 @@ func checkForeignKeyConstrain(w *worker, schema, table string, fkInfo *model.FKI
if err != nil {
return errors.Trace(err)
}
defer w.sessPool.put(sctx)
originValue := sctx.GetSessionVars().OptimizerEnableNAAJ
sctx.GetSessionVars().OptimizerEnableNAAJ = true
defer func() {
sctx.GetSessionVars().OptimizerEnableNAAJ = originValue
w.sessPool.put(sctx)
}()

var buf strings.Builder
buf.WriteString("select 1 from %n.%n where ")
Expand Down Expand Up @@ -709,7 +714,7 @@ func checkForeignKeyConstrain(w *worker, schema, table string, fkInfo *model.FKI
}
buf.WriteString(" from %n.%n ) limit 1")
paramsList = append(paramsList, fkInfo.RefSchema.L, fkInfo.RefTable.L)
rows, _, err := sctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(w.ctx, nil, buf.String(), paramsList...)
rows, _, err := sctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(w.ctx, []sqlexec.OptionFuncAlias{sqlexec.ExecOptionUseCurSession}, buf.String(), paramsList...)
if err != nil {
return errors.Trace(err)
}
Expand Down

0 comments on commit 1305687

Please sign in to comment.