是一个

select *  from myView

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

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

?

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


当前回答

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

其他回答

一般来说,没有。视图主要用于方便和安全,(它们本身)不会带来任何速度上的好处。

也就是说,SQL Server 2000及以上版本确实有一个称为索引视图的特性,可以极大地提高性能,但有一些注意事项:

不是每个视图都可以被做成索引视图;它们必须遵循一组特定的指导方针,这(在其他限制中)意味着您不能包含常见的查询元素,如COUNT、MIN、MAX或TOP。 索引视图使用数据库中的物理空间,就像表上的索引一样。

本文描述了索引视图的其他优点和局限性:

You Can… The view definition can reference one or more tables in the same database. Once the unique clustered index is created, additional nonclustered indexes can be created against the view. You can update the data in the underlying tables – including inserts, updates, deletes, and even truncates. You Can’t… The view definition can’t reference other views, or tables in other databases. It can’t contain COUNT, MIN, MAX, TOP, outer joins, or a few other keywords or elements. You can’t modify the underlying tables and columns. The view is created with the WITH SCHEMABINDING option. You can’t always predict what the query optimizer will do. If you’re using Enterprise Edition, it will automatically consider the unique clustered index as an option for a query – but if it finds a “better” index, that will be used. You could force the optimizer to use the index through the WITH NOEXPAND hint – but be cautious when using any hint.

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

这要视情况而定。索引视图比普通视图或查询快,但不能在镜像数据库环境(MS SQL)中使用索引视图。

任何类型的循环中的视图都会导致严重的减速,因为每次在循环中调用视图时都会重新填充视图。与查询相同。在这种情况下,使用#或@来保存要循环的数据的临时表比视图或查询更快。

所以这要视情况而定。

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

视图的目的是一遍又一遍地使用查询。为此,SQL Server、Oracle等通常会提供视图的“缓存”或“编译”版本,从而提高其性能。一般来说,这应该比“简单”查询执行得更好,但如果查询确实非常简单,好处可能可以忽略不计。

现在,如果您正在执行一个复杂的查询,请创建视图。