是一个
select * from myView
比查询本身更快地创建视图(为了拥有相同的resultSet):
select * from ([query to create same resultSet as myView])
?
我不完全清楚视图是否使用了某种缓存,使其比简单查询更快。
是一个
select * from myView
比查询本身更快地创建视图(为了拥有相同的resultSet):
select * from ([query to create same resultSet as myView])
?
我不完全清楚视图是否使用了某种缓存,使其比简单查询更快。
当前回答
编辑:我错了,你应该在上面看到马克斯的回答。
我不能从使用SQL Server的经验来说,但对于大多数数据库来说,答案是否定的。在性能方面,使用视图获得的唯一潜在好处是它可能基于查询创建一些访问路径。但是使用视图的主要原因是简化查询或标准化访问表中某些数据的方式。一般来说,您不会获得性能上的好处。不过,我可能错了。
我会举一个稍微复杂一点的例子,自己计时看看。
其他回答
是的,视图可以分配一个聚集索引,当它们这样做时,它们将存储临时结果,可以加快结果查询的速度。
微软自己的文档非常清楚地表明,Views可以提高性能。
首先,人们创建的大多数视图都是简单视图,不使用此特性,因此与直接查询基表没有区别。简单视图是在适当的地方进行扩展的,因此不会直接促进性能的提高——这是事实。然而,索引视图可以显著提高性能。
让我直接看文档:
在视图上创建了唯一的聚集索引之后,视图的结果集将立即物化,并持久化在数据库的物理存储中,从而节省了在执行时执行这一昂贵操作的开销。
其次,即使没有被另一个查询直接引用,这些索引视图也可以工作,因为优化器将在适当的时候使用它们来代替表引用。
同样,文档:
The indexed view can be used in a query execution in two ways. The query can reference the indexed view directly, or, more importantly, the query optimizer can select the view if it determines that the view can be substituted for some or all of the query in the lowest-cost query plan. In the second case, the indexed view is used instead of the underlying tables and their ordinary indexes. The view does not need to be referenced in the query for the query optimizer to use it during query execution. This allows existing applications to benefit from the newly created indexed views without changing those applications.
该文档以及演示性能改进的图表可以在这里找到。
更新2:这个答案受到了批评,因为提供性能优势的是“索引”,而不是“视图”。然而,这很容易被反驳。
Let us say that we are a software company in a small country; I'll use Lithuania as an example. We sell software worldwide and keep our records in a SQL Server database. We're very successful and so, in a few years, we have 1,000,000+ records. However, we often need to report sales for tax purposes and we find that we've only sold 100 copies of our software in our home country. By creating an indexed view of just the Lithuanian records, we get to keep the records we need in an indexed cache as described in the MS documentation. When we run our reports for Lithuanian sales in 2008, our query will search through an index with a depth of just 7 (Log2(100) with some unused leaves). If we were to do the same without the VIEW and just relying on an index into the table, we'd have to traverse an index tree with a search depth of 21!
显然,视图本身会比单独使用索引提供性能优势(3倍)。我试图使用一个现实世界的例子,但您会注意到,立陶宛销售的简单列表会给我们带来更大的优势。
注意,在我的例子中,我只是使用了一个直b树。虽然我相当确定SQL Server使用了b-树的一些变体,但我不知道细节。尽管如此,这一点还是成立的。
更新3:出现了关于索引视图是否只使用底层表上的索引的问题。换句话说:“索引视图与标准索引是等价的,它没有为视图提供任何新的或独特的东西。”当然,如果这是真的,那么上面的分析就是不正确的!让我引用微软文档中的一段话来说明为什么我认为这种批评是不正确的:
使用索引来提高查询性能并不是一个新概念;然而,索引视图提供了使用标准索引无法实现的额外性能优势。
加上上面关于数据在物理存储中的持久性的引用,以及文档中关于如何在视图上创建索引的其他信息,我认为可以肯定地说,索引视图不仅仅是恰好使用在主表上定义的索引的缓存SQL Select。因此,我继续坚持这个答案。
从视图或表中选择没有太大意义。
当然,如果视图没有不必要的连接、字段等。您可以检查用于提高View性能的查询、连接和索引的执行计划。
您甚至可以为更快的搜索需求在视图上创建索引。http://technet.microsoft.com/en-us/library/cc917715.aspx
但是如果你搜索'%…SQL引擎将不能从文本列上的索引中获益。如果你能强迫你的用户进行类似“……比那快多了
参考asp论坛上的回答: https://forums.asp.net/t/1697933.aspx?Which+is+faster+when+using+SELECT+query+VIEW+or+Table+
视图的目的是一遍又一遍地使用查询。为此,SQL Server、Oracle等通常会提供视图的“缓存”或“编译”版本,从而提高其性能。一般来说,这应该比“简单”查询执行得更好,但如果查询确实非常简单,好处可能可以忽略不计。
现在,如果您正在执行一个复杂的查询,请创建视图。
我的理解是,在过去,视图会更快,因为SQL Server可以存储执行计划,然后直接使用它,而不是试图在飞行中找出一个。我认为现在的性能增益可能没有以前那么大,但我不得不猜测使用视图会有一些边际改进。
我希望这两个查询的执行是相同的。视图只不过是一个存储的查询定义,视图没有缓存或存储数据。当您运行第一个查询时,优化器将有效地将其转换为第二个查询。