是一个

select *  from myView

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

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

?

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


当前回答

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

其他回答

我的理解是,在过去,视图会更快,因为SQL Server可以存储执行计划,然后直接使用它,而不是试图在飞行中找出一个。我认为现在的性能增益可能没有以前那么大,但我不得不猜测使用视图会有一些边际改进。

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语句更有可能被“重用”。

如果创建物化视图(带有模式绑定),可能会更快。非物化视图的执行就像常规查询一样。

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

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

布伦特·欧扎尔报道