182

Suppose I have 2 tables, Products and ProductCategories. Both tables have relationship on CategoryId. And this is the query.

SELECT p.ProductId, p.Name, c.CategoryId, c.Name AS Category
FROM Products p
INNER JOIN ProductCategories c ON p.CategoryId = c.CategoryId
WHERE c.CategoryId = 1;

When I create execution plan, table ProductCategories performs cluster index seek, which is as expectation. But for table Products, it performs cluster index scan, which make me doubt. Why FK does not help improve query performance?

So I have to create index on Products.CategoryId. When I create execution plan again, both tables perform index seek. And estimated subtree cost is reduced a lot.

My questions are:

  1. Beside FK helps on relationship constraint, does it have any other usefulness? Does it improve query performance?

  2. Should I create index on all FK columns (liked Products.CategoryId) in all tables?

1
  • We really need to see the specification for both tables to give a full critique. Both tables need some kind of index (primary / secondary / foreign) on CategoryId. And the datatype for that column is critical.
    – Rick James
    Oct 10, 2023 at 15:59

10 Answers 10

218

Foreign Keys are a referential integrity tool, not a performance tool. At least in SQL Server, the creation of an FK does not create an associated index, and you should create indexes on all FK fields to improve look up times.

9
  • 41
    Good models (generally) perform better. Mar 20, 2011 at 3:03
  • 10
    "Foreign Keys are a relational integrity tool" -- please use the word 'relational' with care. Foreign keys are a database concept, a short hand for a referential integrity constraint. They are not part of the relational model. I assume you made a typo.
    – onedaywhen
    Mar 16, 2012 at 8:34
  • 8
    @Kenny Often yes, but sometimes a better model costs more. Case in point: foreign keys cause more processing to happen, not less.
    – Hans
    Dec 6, 2012 at 0:27
  • 8
    foreign keys do improve performance, at least in MySQL. Moreover, you're right, the creation of a FK does not create an index; the creation of a FK requires an index Aug 26, 2014 at 17:31
  • 24
    This answer is pretty much useless because it doesn't answer the question. It's great to know that foreign keys are not intended to have a (positive) effect on performance, but the question was concerning reality, not intentions.
    – John
    Mar 15, 2017 at 16:04
71

Foreign Keys can improve (and hurt) performance

  1. As stated here: Foreign keys boost performance

  2. You should always create indexes on FK columns to reduce lookups. SQL Server does not do this automatically.

Edit

As the link now seems to be dead (kudos to Chris for noticing), following shows the gist of why foreign keys can improve (and hurt) performance.

Can Foreign key improve performance

Foreign key constraint improve performance at the time of reading data but at the same time it slows down the performance at the time of inserting / modifying / deleting data.

In case of reading the query, the optimizer can use foreign key constraints to create more efficient query plans as foreign key constraints are pre declared rules. This usually involves skipping some part of the query plan because for example the optimizer can see that because of a foreign key constraint, it is unnecessary to execute that particular part of the plan.

8
  • 3
    Here's a link that details ways in which they can degrade performance devx.com/getHelpOn/10MinuteSolution/16595/0/page/2
    – cmsjr
    Feb 3, 2009 at 14:31
  • 3
    That makes sense but you'll only run into this with a massive delete statement. Perhaps the conclusion should be that in OLAP environments, non-indexed FK's would improve performance while in OLTP environments, it would degrade performance. Feb 3, 2009 at 14:56
  • 2
    The link in this Answer is dead. This is unfortunate as it's the only argument here for FKs improving performance. Oct 11, 2011 at 18:40
  • 1
    @ChrisMoschini - I didn't notice your comment until now. As you've mentioned, the link is dead but the gist of it is mentioned in the new link (with details) I've posted. Mar 16, 2012 at 7:38
  • 2
    Wayback Machine link for the Win! The article can also be found on SQLMag.com, here. May 13, 2016 at 16:23
19

A foreign key is a DBMS concept for ensuring database integrity.

Any performance implications/improvements will be specific to the database technology being used and are secondary to the purpose of a foreign key.

It is good practice in SQL Server to ensure that all foreign keys have at least a non clustered index on them.

I hope this clears things up for you but please feel free to request more details.

6
  • 11
    @Kenny Evitt if you don't have integrity, your data is useless. I find that sells very easily.
    – HLGEM
    Aug 24, 2011 at 17:14
  • @HLGEM Getting a 404 error once in a while is still quite bearable. Having exceptional throughput in return using cheaper resources and less complex systems, now that sells very easily too. You might be interested in the C.A.P. theorem. Oct 19, 2011 at 14:17
  • 9
    @Daniel Dinnyes, data integrity isn't about getting a 404 error. It's about having usable data. It's about not losing orders and financial data for reports for instance because of the incompetence of the developers. There is NO EXCUSE for not using foreign keys.
    – HLGEM
    Oct 19, 2011 at 14:20
  • 2
    I agree with HLGEM. Letting your code to handle integrity is not always a good idea. Data is often used to take decisions, but if the data is corrupted, then the decision will not be accurate.
    – lepe
    Jan 13, 2012 at 1:29
  • 1
    "Foreign Keys are a relational integrity tool" -- please use the word 'relational' with care. Foreign keys are a database concept, a short hand for a referential integrity constraint. They are not part of the relational model. I assume you made a typo.
    – onedaywhen
    Mar 16, 2012 at 8:35
4

Your best performance bet is to use Indexes on fields you use frequently. If you use SQL Server you can use profiler to profile a specific database and take the file that outputs and use the tuning wizard to recieve recommendations on where to place your indexes. I also like using profiler to flush out long running stored procedures, I have a top ten worst offenders list I publish every week, keeps people honest :D.

4

Adding a foreign key in table will not improve the performance, simply saying if you are inserting a record in a ProductCategories table database will try to find the foreign key column has a value which exist in a products table's primary key value, this look up, operation is overhead on your database every time you add a new entry in ProductCategories table. So by adding a foreign key will not improve your database performance but it will take care about the integrity of your database. Yes it will improve the performance of you db if you are checking integrity using foreign key instead of running many queries for checking the record is exist in database in your program.

3

I do not know much about SQL server, but in case of Oracle, having a foreign key column reduces the performance of data-loading. That is because database needs to check the data integrity for each insert. And yes, as it is already mentioned, having an index on foreign key column is a good practice.

3

For MySQL 5.7, it definitely can speed up queries involving multiple joins amazingly well!

I used 'explain' to understand my query and found that I was joining 4-5 tables - where no keys were used at all. I did nothing but add a foreign key to these tables and the result was a 90% reduction in loadtime. Queries that took >5s now take 500ms or less.

That is an ENORMOUS improvement!

AND, as others have mentioned, you get the added bonus of ensuring relational integrity.

Beyond this, ensuring referential integrity has it's own performance benefits as well. It has the second order effect of ensuring that the tables that have the foreign key are 'up to date' with the foreign table. Say you have a users table and a comments table, and you're doing some statistics on the comments table. Probably if you hard delete the user, you don't want their comments anymore, either.

1
  • 1
    Did the tables have the required indexes to generate the foreign keys before you added them?
    – George
    Mar 26, 2020 at 13:48
3

You can use it to help make a query more efficient. It does allow you to restructure queries in SQL Server to use an outer join instead of an inner one which removes sql servers necesity of having to check if there is a null in the column. You don't need to put that qualifier in because the foreign key relationship already inforces that for you.

So this:

select p.ProductId, p.Name, c.CategoryId, c.Name AS Category 
from Products p
inner join ProductCategories c on
p.CategoryId = c.CategoryId
where c.CategoryId = 1;

Becomes this:

 SELECT p.ProductId, p.Name, c.CategoryId, c.Name AS Category 
 FROM ProductCategories c 
 LEFT OUTER JOIN Products P ON
 c.CategoryId = p.CategoryId 
 WHERE c.CategoryId = 1;

This won't necessarily make a huge performance in small queries, but when tables get large it can be more efficient.

1
  • 6
    Not only are outer joins typically less efficient than inner joins (stackoverflow.com/a/2726683/155892), now your queries are misleading: you're relying on the database to implicitly turn your outer joins into inner joins (restoring the performance) instead of just doing that explicitly
    – Mark Sowul
    May 3, 2012 at 15:23
0

As of SQL Server 2008 foreign keys can influence performance by influencing the way the database engine chooses to optimise the query. Refer to Star Join Heuristics in the following article: https://technet.microsoft.com/en-us/library/2008.04.dwperformance.aspx

0

YES, a FK can speed up SELECT but slow down INSERT/UPDATE/DELETE

SQL Server uses all constraints (FK included) to build better execution plans for SELECTs.

For instance, if you run a query with Column1 = X and X does not fit the constraint, the server won't even touch the table.

P.S. unless the constraint is in "untrusted" state, but that's a whole different story.

P.P.S. However having a foreign key (or other constraints) can slow down INSERT/UPDATE/DELETEs unless you have a non-clustered index on this column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.