Search code examples
databaseoptimizationforeign-keysquery-optimizationdatabase-performance

Do foreign keys affect performance / storage efficiency?


Are foreign keys just for assuring the database consistency, or do they also affect performance (for example in performing joins) and/or storage efficiency (e.g. by using only foreign table index etc.)?

Please do mention if you answer in general or if you have in mind particular database solution (MySQL, PostgresQL, Oracle, ...).


Solution

  • In and of themselves, foreign keys are about data consistency - nothing more.

    Of course there is a performance implication on inserts and updates, as values need to be checked, but how this effects performance is governed by other things.

    In general, you should have indexes on your primary and foreign key columns - this is the best way to ensure joins would be using indexes and would be fast (and having indexes does impact storage - they need to be stored somewhere, after all).

    This is true for pretty much every modern RDBMS.