SQL 在数据库中使用视图有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7450423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:09:32  来源:igfitidea点击:

what are benefits of using view in database?

sqldatabaseview

提问by Oded

Possible Duplicate:
Why is database view used?

可能的重复:
为什么使用数据库视图?

Since I always can use Select statement from original tables instead of creating a view and using select from it, I wonder what are benefits of using view in database?

由于我总是可以使用原始表中的 Select 语句,而不是创建视图并从中选择,我想知道在数据库中使用视图有什么好处?

回答by Oded

It simplifies calls and provides a layer of indirection.

它简化了调用并提供了一个间接层。

So, if you have a complex select with lots of joins, you can implement it in a view and simply call the view without need to consider all these joins. You can then reuse this view.

因此,如果您有一个包含大量连接的复杂选择,您可以在视图中实现它并简单地调用该视图,而无需考虑所有这些连接。然后您可以重用此视图。

Additionally, if you use a view instead of a table in this manner, in the future if you need to migrate a column, you can easily do that and only require changes to the view.

此外,如果您以这种方式使用视图而不是表,将来如果您需要迁移列,您可以轻松地做到这一点,并且只需要更改视图。

回答by Elian Ebbing

Besides the obvious benefits that Oded mentioned, you can sometimes drastically improve speed by using materialized views. From wikipedia:

除了 Oded 提到的明显好处之外,您有时还可以通过使用物化视图来显着提高速度。来自维基百科:

In a database management system following the relational model, a view is a virtual table representing the result of a database query. Whenever an ordinary view's table is queried or updated, the DBMS converts these into queries or updates against the underlying base tables. A materialized view takes a different approach in which the query result is cached as a concrete table that may be updated from the original base tables from time to time.

在遵循关系模型的数据库管理系统中,视图是表示数据库查询结果的虚拟表。每当查询或更新普通视图的表时,DBMS 都会将这些转换为针对底层基表的查询或更新。物化视图采用不同的方法,将查询结果缓存为一个具体的表,该表可能会不时从原始基表中更新。

回答by Mike Sherrill 'Cat Recall'

There are several, but I think the main benefit is that views are the SQL implementation of logical data independence.

有几个,但我认为主要的好处是视图是逻辑数据独立性的 SQL 实现。

Build an updatable view, and applications that use the view are relatively immune to changes in the underlying tables. Change the structure of the underlying tables, update the view definition, and all applications work as if nothing had happened. (On legacy databases, there might be hundreds of applications written in dozens of languages. This is the bigwin.)

构建可更新的视图,使用该视图的应用程序相对不受底层表更改的影响。改变底层表的结构,更新视图定义,所有应用程序就好像什么都没发生过一样工作。(在遗留数据库上,可能有数百个应用程序用几十种语言编写。这是一个巨大的胜利。)

Other benefits (paraphrasing Chris Date)

其他好处(转述 Chris Date)

"Automatic" security for hidden data. Restrict access to views, and you have fine-grained control over who sees what.

隐藏数据的“自动”安全性。限制对视图的访问,您可以精细控制谁可以看到什么。

"DRY" capability for applications. A view can provide a simple, public interface to a complex SELECT statement, so applications can just SELECT column-list FROM my-easy-view.

应用程序的“干燥”功能。视图可以为复杂的 SELECT 语句提供简单的公共接口,因此应用程序可以只SELECT column-list FROM my-easy-view.

Different users can see the same data in different ways.

不同的用户可以以不同的方式查看相同的数据。