The Case For Views
I recently had to "defend" my use of views.
To me, they seem natural. Using them is almost always a good thing to do. I've met
those that don't really care for them...I just never understood why. Then again, those same people are still not
convinced of PL/SQL APIs. Maybe there is something to that mindset...
Being forced to articulate one's views is a good thing, it's part of why I blog. I won't lie though, it gets frustrating to have do this, seemingly, all the time.
I'm going to do it here, again.
Complex JoinsNo, I'm not afraid of joins. I am afraid of others who are afraid of joins though. More specifically, I'm afraid of those who aren't proficient at writing SQL. Let me do it, once, and let everyone else have access to the view. Besides, I'm the subject matter expert (SME) on the given set of tables, so it follows that I should create the interface to those tables.
Yes, I said interface. It's exactly what a view is and interface to the underlying data.
EncapsulationWrite it once and let it propogate everywhere.
When I had to "defend" my use of views, I mistakenly used the example of adding columns. Oops. That would (possibly) require changes throughout the system. I meant to say remove columns, in which case you could keep the placeholder in the view using NULL without having to change all of the code. This
does not mean that proper analysis does not need to be performed, it does, but you could possibly get away with not having to change everything that references the view.
My second example was a derived value. This makes more sense to some people thankfully. I've seen the same calculation done on a specific field done 10s, even 100s of times throughout the code. Why not do it one time? Perfect use for views.
SecurityFollowing the least privileges necessary to perform a given action, views allow you to give access to the data without direct access to the tables. Views can also be used to hide or mask data that certain individuals should not have access to. In conjunction with VPD or Application Contexts, it's a powerful way to prevent unauthorized access.
MaintenanceMaintenance has been alluded to above, but not explicitly stated.
For derived values: If you have a derived or calculated value and that calculation is performed all over the place, what happens when it changes? You have to update it everywhere. If you had used a view, change it once and it propogates everywhere. What was once a project is now a "simple" code change. This affects IT in how they choose and assign resources as well as the Business.
For complex joins: What if one table is no longer used or needed? What if that table is littered throughout the code base? You have a project on your hands.
If that table were part of a view, you could "simply" remove it, keep the columns in the view and you're done. There might be places where code needs to be adjusted, but overall, you have a much smaller impact. That's a good thing.
OtherI tried putting the following statement in a category up above, but couldn't make it fit.
Records in a table typically constitute data. Tables, joined together, in a view, tend to turn that data into information.
Labels: design, development, rant, views