Friday, February 24, 2012

A question about db relationships

I am starting a new project and somebody told me at work that if I create relationships (foreign keys) between my tables, it makes the database slower, the best way is to "link" the tables using sql instructions like inner/outer join.
Is that true?
Thanks in advance,
venusgirlwhat moron told you that?|||Many developers are leary of Foreign Key relationships becase it "takes away" some of the control they have in the app area. During the development phase of a new project, it can also be a pain to clear out data in a table that has foreign keys enabled.

I am not aware of any (significant) performance penalty that foreign keys impose on a database. They are of tremendous value long term in maintaining relational integrity and for a host of other reasons.

In the end, Brett's assessment is...er...rather more to the point, but spot on.

Regards,

hmscott|||So I see that there's no performance problems in creating my database with foreign keys. :) Thanks guys.
One more question, that could be applied to any database or only to SQL Server?

Thanks,
venusgirl|||There is always some perofrmance cost associated with foreign keys, because the database needs to do a lookup to ensure that the parent row exists. That cost is normally trivial, and compared to the human cost of doing without foreign keys it is always trivial.

Some databases impose a higher "performance price" than others for implementing foreign keys, but I've never seen one where that cost was material. Compared to the hours of maintenance time and the uncertainty of the users when a system runs without foreign key definitions, I see them as cheap.

-PatP|||"Data Sanitation" after the horse has left the barn is a very time consuming thing.

I like the fact that I don't have to believe or rely on developers who say they will "take care of everything"

They always lie

DB2/Oracle/SQL Server and most database management systems take care of RI.

I wonder about MySQL though... I really should play with that...which is what this board is based on I believe|||All current (post 4.0) flavors of MySQL can declare RI (referential integrity). At least one of the flavors of MySQL (InnoDb) can actually enforce RI. Very few of the MySQL implementations that I've seen use a file engine that can actually enforce RI even though they can declare it, which makes me really uncomfortable.

-PatP|||What's the point of declaring RI if it's not enforced?|||What's the point of declaring RI if it's not enforced?It makes lovely documentation?

-PatP|||Really, this thread could have stopped with Brett's first post.

But...MySql can't enforce relationial integrity? I had no idea. In my opinion a database without primary or foreign keys is not a database. It is a datapile.|||But...MySql can't enforce relationial integrity? I had no idea. In my opinion a database without primary or foreign keys is not a database. It is a datapile.Not exactly... MySQL actually supports mutliple (at least three) file formats for holding data. One of those formats (InnoDb) allows you to support DRI.

The "Front End" for MySQL always supports DRI syntax (at least since version 4.0), whether or not the currently selected back end will enforce the DRI or not.

I've never seen an ISP host InnoDB, but that doesn't mean that none of them host it. InnoDb is considerably more complex to manage, and resource intensive than MyISAM. Because of this choice that is commonly made by implementers in the name of speed and ease of maintenance, MySQL gets a bad name.

-PatP

No comments:

Post a Comment