Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Views, blessing or curse? [closed]

Tags:

sql

sql-server

I once worked with an architect who banned the use of SQL views. His main reason was that views made it too easy for a thoughtless coder to needlessly involve joined tables which, if that coder tried harder, could be avoided altogether. Implicitly he was encouraging code reuse via copy-and-paste instead of encapsulation in views.

The database had nearly 600 tables and was highly normalised, so most of the useful SQL was necessarily verbose.

Several years later I can see at least one bad outcome from the ban - we have many hundreds of dense, lengthy stored procs that verge on unmaintainable.

In hindsight I would say it was a bad decision, but what are your experiences with SQL views? Have you found them bad for performance? Any other thoughts on when they are or are not appropriate?

like image 361
Ed Guiness Avatar asked Sep 03 '08 21:09

Ed Guiness


People also ask

Are SQL views permanent?

It's stored as permanently as anything in your database is, which is to say you can exlicitly delete it if you want.

Do SQL views affect performance?

SQL Server views are helpful in many ways, for example in encapsulating complex multi-table query logic, allowing us to simplify client code. Views make queries faster to write, but they don't improve the underlying query performance.

Can views in SQL provide access restrictions?

There are two primary reasons to provide users with access to data through views rather than providing them with direct access to database tables: Views provide simple, granular security. Use a view to limit the data that a user is allowed to see in a table.

Does view in SQL provide security?

Views can be used as security mechanisms by letting users access data through the view, without granting the users permissions to directly access the underlying base tables of the view. Views can be used to provide a backward compatible interface to emulate a table that used to exist but whose schema has changed.


1 Answers

There are some very good uses for views; I have used them a lot for tuning and for exposing less normalized sets of information, or for UNION-ing results from multiple selects into a single result set.

Obviously any programming tool can be used incorrectly, but I can't think of any times in my experience where a poorly tuned view has caused any kind of drawbacks from a performance standpoint, and the value they can provide by providing explicitly tuned selects and avoiding duplication of complex SQL code can be significant.

Incidentally, I have never been a fan of architectural "rules" that are based on keeping developers from hurting themselves. These rules often have unintended side-effects -- the last place I worked didn't allow using NULLs in the database, because developers might forget to check for null. This ended up forcing us to work around "1/1/1900" dates and integers defaulted to "0" in all the software built against the databases, and introducing a litany of bugs caused by devs working around places where NULL was the appropriate value.

like image 129
Guy Starbuck Avatar answered Oct 19 '22 18:10

Guy Starbuck