Mysql – Is inserting into a table with a foreign key constraint slower than inserting without constraint

auroraforeign keyinsertMySQL

When using MySQL (actually, Aurora), will inserting into a table with a foreign key constraint be slower than the same table without the constraint?

Best Answer

INSERT (etc) must check that there are no FOREIGN 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'.