使用 SqlServer 视图有哪些缺点?
使用 SqlServer 视图有哪些缺点?
我经常创建视图以非规范化形式显示数据。
我发现查询这些联接之一比使用许多表之间的复杂联接生成复杂查询要容易得多,因此速度更快、更不容易出错并且更具自记录性。特别是当我从不同角度分析相同的数据(许多相同的字段,相同的表连接)时。
但是创建和使用这些视图有成本吗?
我是否减慢(或加快?)查询处理速度?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
视图有优点也有缺点。
优点:
缺点:
我个人的意见是不要使用视图,而是使用存储过程,因为它们提供了视图的安全性和封装性,而且还提高了性能。
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.
使用视图的一个可能的缺点是您抽象了底层设计的复杂性,这可能导致初级开发人员和报告创建者滥用。
对于一个特别大且复杂的项目,我设计了一组视图,主要由报表设计者用来填充水晶报表。几周后我发现初级开发人员已经开始使用这些视图来获取聚合并加入这些已经很大的视图,只是因为它们在那里并且易于使用。 (数据库中有很强的 EAV 设计元素。)在初级开发人员开始询问为什么看似简单的报告需要花费很多分钟来执行后,我发现了这一点。
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.
视图的效率在很大程度上取决于基础表。视图实际上只是一种有组织的、一致的方式来查看查询结果。如果用于形成视图的查询良好,并且在基础表上使用正确的索引,则视图不应对性能产生负面影响。
在 SQL Server 中,您还可以创建物化视图或索引视图(自 SQL Server 2000 起),这会增加速度有点。
The efficiency of a view depends in large part on the underlying tables. The view really is a just an organized an consistent way to look at query results. If the query used to form the view is good, and uses proper indexes on the underlying tables, then the view shouldn't negatively impact performance.
In SQL Server you can also create materialized or indexed views (since SQL Server 2000), which increase speed somewhat.
我也经常使用视图。然而,需要注意的一件事是,如果基础表频繁更改(尤其是在开发期间),则使用大量视图可能难以维护。
编辑:话虽如此,我发现能够简化和重用复杂查询的便利和优势超过了维护问题,特别是在负责任地使用视图的情况下。
I use views regularly as well. One thing to note, however, is that using lots of views could be hard to maintain if your underlying tables change frequently (especially during development).
EDIT: Having said that, I find the convenience and advantage of being able to simplify and re-use complex queries outweighs the maintenance issue, especially if the views are used responsibly.
当视图包含最终查询最终未使用的逻辑、列、行或表时,视图可能会降低性能。我无法告诉你有多少次看到这样的东西:(
从而从 InactiveCustomer 表中过滤掉视图中包含的所有行),或者
(SQL 必须检索大量数据,然后在稍后丢弃这些数据)步骤。其他列的检索成本可能很高,例如通过书签查找),或者
(如果直接查询表,SQL 可能会使用更合适的索引),
或者
(通过连接产生大量 CPU 开销,以及随后丢弃的表读取的不必要 IO),或者我最喜欢的:(
当它只需要读取 1 个表时,读取 12 个表)。
在大多数情况下,SQL 足够聪明,可以“透视表象”并提出有效的查询计划。但在其他情况下(尤其是非常复杂的情况),它不能。在上述每种情况下,答案是删除视图并查询基础表。
至少(即使您认为 SQL 足够聪明,可以优化它),消除视图有时可以使您自己的查询调试和优化更容易(更明显需要做什么)。
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).
我遇到的视图的一个缺点是将它们合并到分布式查询中时,性能会大幅下降。这篇 SQLMag 文章讨论了 - 同时我在演示中使用了高度人工的数据,我在“现实世界”中一次又一次遇到这个问题。
尊重你的观点,他们就会善待你。
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.
SQL Server 中视图的各种限制有哪些?
视图的 11 个主要限制
源 SQL MVP Pinal Dave
http://blog.sqlauthority.com/2010/10/03/sql -server-the-limitations-of-the-views-11-and-more/
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 I started I always though views added performance overhead, however experience paints a different story (the view mechanism itself has negligible overhead).
It all depends on what the underlying query is. Check out indexed views here or here , ultimately you should test the performance both ways to obtain a clear performance profile
我最大的“抱怨”是 ORDER BY 在视图中不起作用。虽然这是有道理的,但如果没有预料到的话,这种情况可能会跳起来咬人。因此,在某些无法稍后指定 ORDER BY 的情况下,我不得不放弃使用视图来使用 SPROCS(这本身就有足够多的问题)。 (我希望有一个具有“最终视图”的构造——例如可能包括排序依据——语义)。
http: //blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/ (限制 #1 是关于 ORDER BY :-)
My biggest 'gripe' is that ORDER BY does not work in a view. While it makes sense, it is a case which can jump up and bite if not expected. Because of this I have had to switch away from using views to SPROCS (which have more than enough problems of their own) in a few cases where I could not specify an ORDER BY later. (I wish there was a construct with "FINAL VIEW" -- e.g. possibly include order by -- semantics).
http://blog.sqlauthority.com/2010/10/03/sql-server-the-limitations-of-the-views-eleven-and-more/ (Limitation #1 is about ORDER BY :-)
以下是一个 SQL hack,它允许在视图中引用 order by:
但我更喜欢使用
Row_Number
: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
: