If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Foreign keys - what's the overhead?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-26-11, 10:30
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Foreign keys - what's the overhead?

Hi everyone!
Got a question about foreign keys. As for me, I think that they are really convenient thing. But I've read some opinions that they add overhead and it's not recommended to use them on a highload projects. I googled a bit about this topic, but found nothing.

What do you think? Is this true?
Reply With Quote
  #2 (permalink)  
Old 01-26-11, 11:19
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by tedd View Post
H But I've read some opinions that they add overhead and it's not recommended to use them on a highload projects.
you've been reading the wrong stuff

the "overhead" of foreign keys is inconsequential

the benefit of foreign keys is data integrity

you decide

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-27-11, 03:33
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
The overhead is performance of inserts/updates and delete operations on the tables. Every insert needs to cross check with the other tables that the key exists. So for a table with only 1 foreign key the insert will cause a check in the foreign key table before applying the INSERT statement.

For large numbers of inserts, deletes and updates this will become an issue, but, for OLTP type transactions where a single insert it taking place then indeed the overhead will not be significant and would justify including the foreign key constraints.

On warehouse type databases where large volumes are loaded periodically, then using foreign key constraints can be a hindrance due to the performance overhead. A better approach here is to load the data into staging tables (identical setup to the primary table) and load the information in here. None of the tables will have foreign key constraints. However, if you generate an SQL statement that verifies the integrity of the data will a) verify the integrity of the data and b) will allow data cleanup before applying into the primary table. It should be noted that warehouse type databases perform inserts and rarely perform updates or deletes so the data remains static.

Hope this helps?
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #4 (permalink)  
Old 01-27-11, 06:01
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Rudy, Ronan, thanks for answers! Now the benefits and limitations of foreign keys are obvious to me
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On