When using MySQL (actually, Aurora), will inserting into a table with a foreign key constraint be slower than the same table without the constraint?
Mysql – Is inserting into a table with a foreign key constraint slower than inserting without constraint
auroraforeign keyinsertMySQL
Related Question
- MySQL – Cannot Add Foreign Key Constraint While Creating Table
- MySQL Foreign Key – Preventing Deletion of Empty Table
- Postgresql – Insertion of data violates the foreign key constraint
- MySQL Foreign Key – Adding a Foreign Key Constraint Without Losing Data
- SSIS Foreign Key Constraint – How to Resolve Failed Foreign Key Constraint
- MariaDB – ERROR 1452 (23000): Foreign Key Constraint Fails
- MySQL – FK Constraint Fails While Inserting into Child Tables
- Mysql – “Cannot drop index ‘unique_token_data’: needed in a foreign key constraint” in MySQL without a foreign key
Best Answer
INSERT
(etc) must check that there are noFOREIGN KEY
violations. Hence it is slower. Think of it as checking to see if an entry is in an index.The actual cost depends heavily on whether the block of the other index is currently cached, which depends on size of entire dataset, workingset size, speed of disk, etc. I have not encountered a case where FKs made the difference between the system being 'viable' versus 'too slow'.