Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: fix issue of add foreign key too slow in big table #40112

Merged
merged 3 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.CreateMockStoreAndDomain(t)
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
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