There is a table named company_competitors
in our database. there is a job which truncates and loads this table daily.
The table has two columns company_id
and competitor_id
both references another table companies
.
CREATE TABLE `company_competitors` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`company_id` int(11) DEFAULT NULL,
`competitor_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_rails_company_id_c1ac450a` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`),
CONSTRAINT `fk_rails_competitor_id_772a45c6` FOREIGN KEY (`competitor_id`) REFERENCES `companies` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=268477 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
The steps included in truncate and load are:
- create a new table
company_competitors_new
- add foreign key constraints on both the columns (
company_id
,competitor_id
) one by one. - load the data into the new table.
- swap the tables
company_competitors
andcompany_competitors_new
.
Queries:
"CREATE TABLE company_competitors_new LIKE company_competitors;
alter table company_competitors_new ADD CONSTRAINT fk_rails_company_id_53f8f57a foreign key (company_id) references companies(id);'
The companies table might be accessed by some else while adding foreign key. So, When the second query is running the database gets stuck in a deadlock and no queries on companies table get executed.
show full processlist
shows all queries saying Waiting to acquire Metadata lock
and I have to kill all select queries so that adding of foreign key gets finished.
I need help in understanding the reason for deadlock here and how can I deal with it. I would also like to hear if there is a better way to truncate and load with 0 downtime.