Hi,

I’d like to understand the performance impact of adding a FOREIGN KEY
constraint to a new column in a large table. If the column is nullable
and defaults to NULL, does the engine perform any checks or
validations on existing rows? Or is the operation optimized since the
column starts with all values as NULL?

Here’s a simplified example:

ALTER TABLE child_table
ADD COLUMN parent_id INT NULL,
ADD CONSTRAINT fk_child_parent FOREIGN KEY (parent_id) REFERENCES
parent_table (id)
ON UPDATE CASCADE ON DELETE SET NULL;

Both child_table and parent_table are large, and the new column
(parent_id) is empty initially.

Does adding the foreign key have any measurable performance impact in
this case? Additionally, would it make sense to temporarily disable
FOREIGN_KEY_CHECKS for this ALTER TABLE operation and then re-enable
it? Could this save any resources or improve performance?

Thanks for any insights!

Best regards,
Guillermo.
_______________________________________________
discuss mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to