

Hidden Cost of Foreign Key Constraints in MySQL
source link: https://www.percona.com/blog/hidden-cost-of-foreign-key-constraints-in-mysql/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Do you wonder if MySQL tells you the truth about writes to tables that have foreign key constraints? The situation is complex, and getting visibility on what is really happening can be a problem.
I found this issue intriguing and decided to share and highlight some examples.
Query Plan
Let us take this example table:
We want to know how costly an example UPDATE against this table will be:
This shows that just one table and one row will be considered. This looks right as we’re using a primary key lookup, and one row matches:
In our case, this was not the entire truth as the table has FK relationships:
The related table has a defined ON UPDATE CASCADE action linked to our table:
Therefore, the EXPLAIN plan completely fails to recognize this fact, and the plan tries to convince us that such an update will only change one row in our database.
Another typical method to analyze slow queries is checking the per-session status handlers. In this case, it looks like this:
Handler_update
, as well as the Rows Changed information in the query outcome output, are not taking changes in referencing product_order table into account either. Without additional checks, we don’t even know that additional work has been done!
Monitoring
Let’s see how foreign constraints can impact monitoring database activities.
We already know monitoring Handler_update
won’t work as expected. Let’s check the InnoDB engine-related counter (on an idle MySQL instance as this is a global only counter):
Here is our UPDATE and how many data rows it really changes:
This confirms that InnoDB exposes the real number of rows updated correctly here.
Child table locks are also exposed for active transactions with SHOW ENGINE INNODB STATUS
(after enabling innodb_status_output_locks
).
What about Performance Schema, another popular method to monitor the database?
Unfortunately, Performance Schema completely missed what happened due to the Foreign Key constraint! I think it is a serious issue and so I reported it here: https://bugs.mysql.com/bug.php?id=106012
Summary
I hope I was able to draw your attention to the need to exercise caution when investigating DML queries and system load when Foreign Key constraints are used! Maybe you were surprised that a simple single row update or delete required so much time? It could be that under the hood MySQL changed thousands of rows and hid this fact from you!
Recommend
-
8
Showing full MySQL foreign key errors By continuing your visit to this site, you accept the use of cookies. Read more. Scout APM helps...
-
18
T-SQL Join on the foreign key that has zero advertisements I need to link various tables that each have a common key (a serial number in this...
-
11
I do not see a foreign key index I created advertisements I've created a table with a unique index as well as a foreign index. When I add the uniqu...
-
7
Copy link Contributor benedikt c...
-
1
Showing full MySQL foreign key errors Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start y...
-
5
Supply Constraints Cost Apple $6 Billion in Q4 2021 ...
-
6
Code is available on GitHub What are foreign keys A foreign key is a property in a table that references items in another table. That’s why they ar...
-
6
Rails 7 adds support for deferrable foreign key constraints in PostgreSQL Mar 30, 2022 , by Murtaza Bagwala 2 minute read...
-
5
@rozhnevSlava RozhnevFull stack dev and DBA Maintainer of https://sqlize.onlin...Receive Stories from @...
-
5
Performance Benefits of NOT NULL Constraints on Foreign Key Reference Columns
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK