Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alter table query taking too long for adding constraint

Tags:

sql

mysql

I want to alter a table called person and want to add foreign key to it using office table

the query I am using is

ALTER TABLE person
ADD CONSTRAINT person_Office_FK
FOREIGN KEY ( Office_id )
REFERENCES Office ( Office_id ) ;

Table office has around 500,000 rows and table person has around 5 million

This query is taking forever i am not sure what is happening.

like image 405
mb1987 Avatar asked Mar 19 '23 19:03

mb1987


1 Answers

If Office_id is the primary key of Office, make sure it has a (primary key) index. This will definitely speed up the adding of the constraint.

Also, according to How to temporarily disable a foreign key constraint in MySQL?, you can use

SET FOREIGN_KEY_CHECKS=0;

To disable ALL foreign key constraint checks, possibly this works too when adding them.

like image 152
Patrick Hofman Avatar answered Mar 21 '23 09:03

Patrick Hofman