Lets say I have five tables named table1, table2 ... table5. I have already made a view/query named query1 by joining table1, 2 and 3.
Now, I want to create a view which requires joining all the five tables. My question is, is it good to use query1 and just join table4 and table5 into it, or is it better to join the five tables one by one?
Im afraid that if I join two views, or a view and table/s, then modifying that same view requires me to reconstruct the whole query becauses of being dependent to that view.
If you do not want your queries to be dependent on a view, then you must ask yourself, why you are creating that view in the first place.
If your 5-table query joins table1, table2 and table3 in a way that merely happens to be identical to how the same tables are joined in the query1 view but in actual fact has a completely different meaning, then, in my opinion, it would be perfectly fine not to use query1 in your second view/query.
But if your second view/query is going to reproduce the same business rules that are already implemented in query1, then I can see absolutely no point in duplicating the code. That's partly the point of a view: by creating it, you can avoid repeating the same code if it needs to be used many times. When you alter query1, it would make sense for other dependent queries and/or views to alter their output accordingly.
Of course, sometimes changes in a view require changes in other queries and/or views that use it. If that's what you are concerned most about at the moment, then I suggest you invest your time into planning your views properly to reduce the possibility of such changes after your database has been in use for a long time.
So, basically, your choice is either to duplicate logic in every view to avoid their being dependent upon one another, or use your views actively as necessary but risk coming across a situation where you have to make a chain of changes merely because one particular view needs to be changed. But, again, as I said earlier, you can minimise that risk if you properly design your views beforehand.
Do as you please.
The query optimizer will analyse the code and build the same execution plan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With