Today I was working with application which uses Innodb and
foreign keys and got into locking problems possibly due to
foreign keys, so I did a little investigation on that matter.
Interesting enough it looks like most people do not think about
foreign keys overhead in terms of locking. The overhead about
checking referenced table is usually considered but not locking
which also might be important.
So lets talk how locks seems to work with foreign keys in Innodb.
I've only done a quick check so could be missing some details.
All Innodb Foreign Key related operations happen on data
modification. So for example if you do SELECT FOR UPDATE on CHILD
table it will not lock associated rows in PARENT table and so you
can run into lock waits if you do updates to CHILD table which
change parents because this is when row in PARENT table will be
locked to perform update.
The checks are performed "originating" from the table …
[Read more]