What are the downsides of using SqlServer Views?
I create views frequently to show my data in a denormalized form.
I find it much easier and therefore faster, less error prone, and more self documenting, to query one of these joins rather than to generate complex queries with complicated joins between many tables. Especially when I am analyzing the same data (many same fields, same table joins) from different angles.
But is there a cost to creating and using these views?
Am I slowing down (or speeding up?) query processing?
What are Various Limitations of the Views in SQL Server?
Top 11 Limitations of Views
Source SQL MVP Pinal Dave
http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/
When comes to Views there are advantages and disadvantages.
Advantages:
Disadvantages:
My personal opinion is to not use Views but to instead use stored procedures as they provide the security and encapsulation of Views but also come with improved performance.
One possible downside of using views is that you abstract the complexity of the underlying design which can lead to abuse by junior developers and report creators.
For a particularly large and complex project I designed a set of views which were to be used mostly by report designers to populate crystal reports. I found out weeks later that junior devs had started using these views to fetch aggregates and join these already large views simply because they were there and were easy to consume. (There was a strong element of EAV design in the database.) I found out about this after junior devs started asking why seemingly simple reports were taking many minutes to execute.
A downside to views that I've run into is a dive in performance when incorporating them into distributed queries. This SQLMag article discusses - and whilst I use highly artificial data in the demo, I've run into this problem time and time again in the "real world".
Respect your views, and they'll treat you well.
The following is a SQL hack that allows an order by to be referenced in a view:
But my preference is to use
Row_Number
:Views can be a detriment to performance when the view contains logic, columns, rows, or tables that aren't ultimately used by your final query. I can't tell you how many times I've seen stuff like:
(thus filtering out all rows that were included in the view from the InactiveCustomer table), or
(SQL has to retrieve lots of data that is then discarded at a later step. Its possible those other columns are expensive to retrieve, like through a bookmark lookup), or
(its likely that SQL could have used a more appropriate index if the tables were queried directly), or
(lots of CPU overhead through the join, and unnecessary IO for the table reads that are later discarded), or my favorite:
(Reads 12 tables when it only really needs to read 1).
In most cases, SQL is smart enough to "see through the covers" and come up with an effective query plan anyway. But in other cases (especially very complex ones), it can't. In each of the above situations, the answer was to remove the view and query the underlying tables instead.
At the very least (even if you think SQL would be smart enough to optimize it anyway), eliminating the view can sometimes make your own query debugging and optimization easier (a bit more obvious what needs to be done).