Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I noticed wordpress/rails etc do not use foreign keys constraint or cascade deletes features from database. Instead they handle this in PHP/Ruby/scripting level!

I have read this and this. Most arguments against foreign keys constraint talks about performance, multithreading, locking, scalability, etc.

Assuming arguments against foreign keys are valid, my question are:

  1. If foreign keys are bad, why WordPress/Rails/etc uses a sql-server which supports foreign keys? Will they benefit going away from MySQL to a NoSQL kind of server?
  2. On other hand, can applications coded in a way to make use of foreign keys feature without running into issues?
  3. Is noSQL/redis better if we are using database only for storage and managing "relations" at application/script layer?
share|improve this question
3  
People who claim that foreign keys hurt performance have no idea what they are talking about. In any serious DBMS they can actually improve performance. And what's even more important: what use is a database that is (supposedly) fast but contains invalid/inconsistent data because it is not validated by the database. Claiming that the application can handle that is plain wrong. Any database that contains important or valuable data will be used by more than one application. –  a_horse_with_no_name Jun 12 '13 at 7:16
2  
Foreign Keys is one thing and Cascading effects is another. The second, yes, is avoided by many designers, as a delete of a few rows in a table can lead to millions of deletes in other tables. –  ypercube Jun 12 '13 at 8:16

1 Answer 1

Lets start with your first link. It says clearly:

Working on large databases with referential integrity just does not perform well.

And that is right. Just you likely have no clue that "large database" is terabyte size with billions of rows in a table. A simple select may cascade into hundreds of millions of related elements to be deleted, and then you have a performance problem.

This is a non-issue for regular small databases such as a wordpress log or most CMS - it turns into a problem if you do something like facebook, or handle financial simulation data. I regularly deal with multi billion row tables and deletes that work in stored procedures outside of transactions in batches of x - because the end delete may easily clean up some hundred million rows.

Will they benefit going away from MySQL to a NoSQL kind of server?

Hardly. They are useful when a professional uses them as appropriate.

On other hand, can applications coded in a way to make use of foreign keys feature without running into issues?

Yes.

Is noSQL/redis better if we are using database only for storage and managing "relations" at application/script layer?

I once did a application review for a technology upgrade in a bank that uses no referential integrity (to get the performance up). Loading the data into SQL Server (which was supposed to replace their aging Adabas installation) it failed with integrity constraint violations. Happens 40% of the historical records were invalid because some * had deleted lookup table values not in use any more (such as old customer classification codes, which got replaced for all active customers, just not the old ones). No referential integrity warning ever came up. The result was some fired people and a problem stuck in workaround and a partially useless data warehouse build on top.

So much for managing relations at application / script layer. Errors WILL happen. Data is valuable, applications change.

Most people complaining about SQL level features would be more advised to read a book about them, and try to understand them, instead of complaining. Sadly a lot of the advice on the internet is written by people that refuse to read even documentation. Always be careful with that. Most advice to NoSql is based strongly on ignorance.

share|improve this answer
    
Thanks for detailed answer. For cascade delete, I have assumption in mind. Lets take WordPress example. If you deleted a post, and then all comments for that post in PHP, why not let mysql do that using cascade-delete? Assuming you want to delete same set of data, won't it be faster using cascade delete, rather than running multiple mysql queries from php-code? –  rahul286 Jun 12 '13 at 8:37
1  
Exactly. THat is where cascading delete is good. but otoh imagine you delete one post and it takes hours because ther are 500 million comments on it ;) That is where the perforamnce issue arises. Like all SQL related stuff, it requires planning and thinking when scaling up. You may be better of using a stored procedure to slowly delete things more controlled when you hit large data amounts. –  TomTom Jun 12 '13 at 9:00
1  
Yes, but you enter a delete 1 row sql, and wait for hours - with locks in the database that you did not expect. People then call support and curse on the slow database. It IS developer ignorance, but this is something you have to work around. Not every - even large - database can handle a 500 million row delete without service interruption. And the website doing the delete may block the page for hours until it is committed. Try thinking about such scenarios yourself - that is part of the job of a developer. –  TomTom Jun 12 '13 at 10:40
1  
Pretty exactly yes. Basically - cascades are perfect as long as they do not change the characteristics too much. If 1 row deletion turns into an hour long wait, that is such a case. And let's do not get into issues like database log size management, memory consumption etc. that likely need also be planne d out if a simple delete turns into a multi gigabyte change. –  TomTom Jun 12 '13 at 13:34
1  
The point is that some dude then knows he has to use a stored procedure and it takes time. With a cascading delete that may be something that pops up in a test. In fact it may not b possible to make the delete in one transaction. –  TomTom Jun 13 '13 at 4:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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