6 ms·
I'm guessing they are validating the constraints in their applications instead of their databases. While this puts more requirements on your setup and developer
by Chilinot 8y ago
I'm guessing they are validating the constraints in their applications instead of their databases. While this puts more requirements on your setup and developers, it offloads a lot of stress from your database.
- gaius 8y agoHow does the app know what is valid in a foreign key column without repeatedly retrieving the primary key column of the other table?
- tomnipotent 8y agoIt repeatedly retrieves the parent row. Look at the URL when replying on HN and change the id by adding a few 0's - the app prevents a comment from being added if the ID isn't found. Of course, this doesn't prevent a developer from circumventing the apps constraint logic.
- gaius 8y agoThis would place a far higher load on the DB (which would validate the constraint via the index if allowed to) and create more network roundtrips. It’s 10x as expensive as just using a FK, and far less reliable!
- tomnipotent 8y ago>> It’s 10x as expensive as just using a FK It's not about load, it's about locks and contention in the database caused by FK constraint enforcement. Extra read queries will barely be noticeable compared to that.
- gaius 8y agoit's about locks and contention in the database caused by FK constraint enforcement What locks do you mean?
- cup-of-tea 8y agoYeah, but from a data integrity point of view that is horrifying. I guess they must have some very comprehensive testing framework to validate the code. Also, pardon my further ignorance, but if you're not going to use foreign key constraints, what is the point of using a relational db? Why not just a fast key-value store for each index?
- fipar 8y agoYou may me able to architect your application to do well-enough without Foreign Keys yet still require features that key-value stores do not provide, like SQL, triggers, etc. In the specfic case of MySQL, while still horrifying (I agree with you! but it is one of the things you some times have to do at scale), you can create the Foreign Key constraints but then disable their verification and periodically look for violations, as described here: https://www.percona.com/blog/2011/11/18/eventual-consistency-in-mysql/ https://www.percona.com/blog/2011/11/18/eventual-consistency...
- zzzeek 8y agoThe application still has foreign key references and JOIN, which you don't get in a k/v store, you just don't have the actual CONSTRAINT. MySQL worked this way for literally decades with the MyISAM storage engine that does not have referential integrity constraints, it just allows the FOREIGN KEY syntax to pass silently.