Nowadays, I’m working on a database, with no ‘Relations, PKs and FKs’, just raw data. I can say that database is just set of papers. When I asked about this, I had this; ‘Hide the Business’.
Also, one of my friends said, this always happens in ‘Large systems’.
In large systems, they are tyring to hide thier business through raw data. Regarding development; relations, constraints, validation, are done in database using triggers and of course user interface.
What do you think regarding this?
Well, this may have point on large databases, when you need fast responce on massive
DML(INSERT / UPDATE / DELETE).The problem is that if you rely on database’s way to ensure integrity, you hardly can optimize it.
There is also thing called
SQL/PLSQL context switchinginOracle: if you create an empty trigger on the table, it will slow downDMLabout 20 times — with the mere fact that the trigger exists.In Oracle, when you write a
ON UPDATEtrigger and update50,000rows in the table, the trigger and the query in it gets called50,000times. Foreign keys perform better, but they may also get laggy (and you can do nothing with the underlying queries)In this case, it’s better to put the results you want to update into a temporary table, issue a
MERGE, check integrity before and after, and apply the business rules. A single query that processes50,000rows works faster than a loop of50,000queries processing single row.Of course, it’s very hard to implement and only pays for itself when you have really large database and need to perform really massive updates on it.
In
Oracle, in any case,FOREING KEYconstraints perform better than tiggers implementing the same functionality.PRIMARY KEYSwill most likely improve performance, as a primary key implies creating theUNIQUE INDEXon the constrained field, and this index may be efficiently used in the queries. AUNIQUE INDEXis also a natural and most efficent way to enforce uniqueness.But of course, as any index, is slows down
INSERTSand thoseUPDATESandDELETESwhoseWHEREcondition is not selective.I. e. if you need to
UPDATEorDELETE1row of2,000,000, then the index is your friend; if you need toUPDATEorDELETE1,500,000rows of2,000,000, the index is your enemy. It’s a matter of tradeoff.You may also see my answer here.