是一个

select *  from myView

比查询本身更快地创建视图(为了拥有相同的resultSet):

select * from ([query to create same resultSet as myView])

?

我不完全清楚视图是否使用了某种缓存,使其比简单查询更快。


当前回答

我无意中看到了这个帖子,只是想分享Brent Ozar的这篇文章,作为使用可用性组时的考虑。

布伦特·欧扎尔报道

其他回答

是的,视图可以分配一个聚集索引,当它们这样做时,它们将存储临时结果,可以加快结果查询的速度。

微软自己的文档非常清楚地表明,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。因此,我继续坚持这个答案。

没有实际的区别,如果你读BOL,你会发现你的普通旧SQL SELECT * FROM X确实利用了计划缓存等。

存储执行计划应该会有一些微不足道的好处,但可以忽略不计。

In SQL Server at least, Query plans are stored in the plan cache for both views and ordinary SQL queries, based on query/view parameters. For both, they are dropped from the cache when they have been unused for a long enough period and the space is needed for some other newly submitted query. After which, if the same query is issued, it is recompiled and the plan is put back into the cache. So no, there is no difference, given that you are reusing the same SQL query and the same view with the same frequency.

显然,在一般情况下,一个视图,根据它的本质(有人认为它被经常使用,使它成为一个视图)通常比任何任意的SQL语句更有可能被“重用”。

对于SQL Server来说,视图肯定比嵌套查询要好。在不知道为什么它更好的情况下(直到我读到Mark Brittingham的文章),我已经运行了一些测试,在使用视图和嵌套查询时,我经历了几乎惊人的性能提升。在连续运行查询的每个版本数百次之后,查询的视图版本在一半的时间内完成。我得说这对我来说已经足够了。