8

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?
2
  • 5
    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.
    – user1822
    Jun 12, 2013 at 7:16
  • 4
    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. Jun 12, 2013 at 8:16

1 Answer 1

14

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.

13
  • 2
    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, 2013 at 9:00
  • 2
    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, 2013 at 10:40
  • 2
    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, 2013 at 13:34
  • 2
    @rahul286 The problem here is not that SQL needs more care to be more performant, it's just so that some problems are not able to be faster - by pure mathematical reasons. SQL is all about searching and ordering structured data. To search a heap you have to touch each item until found. If someone created an ordered list (roughly an index in SQL) for you then you will be much faster but still need time. But that's it. There's nothing you can do to drastically lower this "list-lookup time". Maybe small tweaks, but nothing major. SQL is in this state. It's fast, all limits are "by nature itself".
    – Foo Bar
    Apr 4, 2018 at 16:03
  • 2
    @rahul286 NoSQL style storages are basically what an "index" is in SQL. They can not ensure data integrity by itself - that's why they are faster than SQL. Usually you use NoSQL storages to support relational/SQL storages. Best example is a search engine for a laaarge website. All the real data is in some SQL database. Doing a search on it is slow (especially with LIKE or REGEX). Then you take your data, prepare it for the NoSQL storage and then you are doing the search function on the NoSQL storage instead on the SQL storage. NoSQL "duplicates" the SQL storage in a usefull way to be fast.
    – Foo Bar
    Apr 4, 2018 at 16:10

Your Answer

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

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