什么时候以及为什么有些人决定他们需要在他们的数据库中创建一个视图?为什么不运行一个普通的存储过程或选择?


当我想要查看一个表的快照和/或视图(以只读方式)时

视图是查询的封装。转换为视图的查询往往比较复杂,因此将它们保存为视图以供重用是有好处的。

它可以作为ORM和表之间的“中间人”。

例子:

我们有一个Person表,我们需要改变它的结构,以便将SomeColumn列移动到另一个表,并与之有一对多的关系。

然而,系统的大多数情况下,就Person而言,仍然将SomeColumn作为一个单独的东西使用,而不是很多东西。我们使用一个视图将所有的SomeColumns聚集在一起,并将其放在视图中,效果很好。

这是可行的,因为数据层发生了变化,但业务需求没有根本变化,因此业务对象不需要更改。如果业务对象必须改变,我不认为这是一个可行的解决方案,但视图绝对是一个很好的中间点。

在其他方面,它可以用于安全。如果您有一个“customer”表,您可能希望让所有销售人员访问名称、地址、邮政编码等字段,但不允许credit_card_number。您可以创建一个只包含他们需要访问的列的视图,然后授予他们对视图的访问权。

我通常创建视图来反规范化和/或聚合经常用于报告目的的数据。

EDIT

By way of elaboration, if I were to have a database in which some of the entities were person, company, role, owner type, order, order detail, address and phone, where the person table stored both employees and contacts and the address and phone tables stored phone numbers for both persons and companies, and the development team were tasked with generating reports (or making reporting data accessible to non-developers) such as sales by employee, or sales by customer, or sales by region, sales by month, customers by state, etc I would create a set of views that de-normalized the relationships between the database entities so that a more integrated view (no pun intended) of the real world entities was available. Some of the benefits could include:

减少编写查询时的冗余 建立关联实体的标准 提供机会 评估和最大化绩效 用于复杂的计算和连接 (例如在Schemabound视图上建立索引 在该软件中) 让数据更容易获取 对于团队成员和非开发人员来说是直观的。

几个原因: 如果您有复杂的连接,有时最好有一个视图,这样任何访问都会有正确的连接,开发人员不必记住他们可能需要的所有表。通常情况下,这可能用于财务应用程序,其中所有财务报告都基于同一组数据,这是非常重要的。

如果你有用户,你想限制他们可以看到的记录,你可以使用一个视图,只给他们访问视图,而不是底层表,然后查询视图

Crystal报表似乎更喜欢使用视图来存储过程,因此编写大量报表的人倾向于使用大量视图

视图在重构数据库时也非常有用。您通常可以通过创建一个视图来隐藏更改,以便旧代码看不到它。阅读重构数据库的相关内容,了解它是如何工作的,因为这是一种非常强大的重构方式。

这样做的原因不止一个。有时可以使通用连接查询变得简单,因为可以只查询表名而不是执行所有的连接。

另一个原因是将数据限制为不同的用户。例如:

表1:列- USER_ID;用户名;SSN

管理用户可以在实际表上有私权,但你不想访问的用户,比如SSN,你创建一个视图

CREATE VIEW USERNAMES AS SELECT user_id, username FROM Table1;

然后给他们访问视图而不是表的私权。

当我只运行查询时,我喜欢在存储过程上使用视图。视图还可以简化安全性,可用于简化对多个表的插入/更新,并可用于快照/物化数据(运行长时间运行的查询,并保持结果缓存)。

我使用物化视图来执行不需要实时保持准确的查询。

以下是两个常见的原因:

你可以用它来保证安全。在主表上不授予权限,创建限制列或行访问的视图,并向用户授予查看视图的权限。

你可以为了方便使用它。将视图中经常使用的一些表连接在一起。这可以使查询保持一致并更容易。

视图提供了几个好处。

1. 视图可以隐藏复杂性

如果您有一个需要连接几个表的查询,或者有复杂的逻辑或计算,您可以将所有这些逻辑编码到一个视图中,然后像处理表一样从视图中进行选择。

2. 视图可以用作一种安全机制

视图可以从一个(或多个)表中选择某些列和/或行,并在视图上而不是在底层表上设置权限。这允许只显示用户需要查看的数据。

3.视图可以简化对遗留代码的支持

如果您需要重构一个会破坏大量代码的表,您可以用同名的视图替换这个表。视图提供了与原始表完全相同的模式,而实际的模式已经更改。这可以防止引用表的遗留代码中断,允许您在空闲时更改遗留代码。

这些只是展示视图如何有用的众多示例中的一些。

在对遗留数据库进行报告时,视图可能是天赐之物。特别是,您可以使用有意义的表名,而不是神秘的字母名(其中两个字母是常见的前缀!),或者充满缩写的列名,我确信这在当时是有意义的。

视图还将非常复杂的配置和表分解为易于查询的可管理块。在我们的数据库中,整个表管理系统被分解为来自一个大表的视图。

这并没有确切地回答你的问题,但我认为值得一提的是物化视图。我的经验主要是使用Oracle,但是SQL-Server应该是相当相似的。

We used something similar in our architecture to address XML performance problems. Our systems are designed with a lot of data stored as XML on a row and applications might need to query particular values within it. Handling lots of XMLTypes and running XPaths across large number of rows has a large impact on performance so we use a form of materialized views to extract the desired XML nodes out into a relational table anytime the base table changes. This effectively provides a physical snapshot of the query at a point in time as opposed to standard views which would run their query on demand.

在我看来,存储过程更多的是一种可以针对数据调用的方法,而视图提供了一种机制来创建基础数据的合成版本,可以根据它创建查询或存储过程。当简化或聚合有意义时,我将创建一个视图。当我想提供一个非常特定的服务时,我将编写一个存储过程。

视图相对于存储过程的一个主要优点是,您可以像使用表一样使用视图。也就是说,可以在查询的FROM子句中直接引用视图。例如,SELECT * FROM dbo.name_of_view。

在几乎所有其他方面,存储过程都更强大。你可以传入参数,包括输出参数,让你一次有效地返回几个值,你可以做SELECT, INSERT, UPDATE和DELETE操作,等等。

如果你想要一个View能够从from子句中进行查询,但你也想要能够传入参数,也有一种方法可以做到这一点。它叫做表值函数。

这里有一篇关于这个主题的非常有用的文章:

http://databases.aspfaq.com/database/should-i-use-a-view-a-stored-procedure-or-a-user-defined-function.html

编辑:顺便说一下,这就提出了一个问题,视图比表值函数有什么优势?对此,我没有一个很好的答案,但我要指出,创建视图的T-SQL语法比创建表值函数要简单,数据库用户可能更熟悉视图。

关于视图的一件奇怪的事情是,它们被Microsoft Access视为表:当您使用ODBC将Microsoft Access前端附加到SQL数据库时,您将在可用表列表中看到表和视图。因此,如果你在MS Access中准备复杂的报告,你可以让SQL服务器来做连接和查询,这大大简化了你的工作。在Excel中准备查询也是如此。

可以把它看作重构数据库模式。

一般来说,我使用视图是为了让生活更简单,从存储在多个表中的实体中获得扩展细节(消除代码中的大量连接以增强可读性),有时还可以在多个数据库中共享数据,甚至可以使插入更容易阅读。

我的生产数据库中只有10个左右的视图。我用了几个我一直在用的列。我使用的一组来自7个表,一些具有外部连接,而不是不断重写,我只需要在一个选择中调用该视图,并使一个或2个连接。对我来说,这只是节省时间。

我正在创建xxx,映射主表(如Products表)和引用表(如ProductType或ProductDescriptionByLanguage)之间的所有关系。这将创建一个视图,允许我检索产品及其从外键转换到描述的所有详细信息。 然后我可以使用ORM创建对象,轻松地构建网格、组合框等。

我认为第一个。隐藏查询的复杂性。它非常适合于视图。当我们规范化数据库表时如何增加。现在,当表数量增加时,获取数据是非常困难的。所以最好的处理方法是遵循视图。如果我错了,请纠正我。

下面是如何使用视图以及权限来限制用户可以在表中更新的列。

/* This creates the view, limiting user to only 2 columns from MyTestTable */
CREATE VIEW dbo.myTESTview 
WITH SCHEMABINDING AS
SELECT ID, Quantity FROM dbo.MyTestTable;

/* This uses the view to execute an update on the table MyTestTable */
UPDATE dbo.myTESTview
SET Quantity = 7
WHERE ID = 1

我们创建view来限制或严格访问表中的所有行/列。如果所有者希望只有特定或有限的行/列需要共享,那么他将使用这些列创建一个视图。

关注特定的数据 视图允许用户专注于他们感兴趣的特定数据和他们负责的特定任务。不必要的数据可以从视图中删除。这还提高了数据的安全性,因为用户只能看到视图中定义的数据,而不能看到底层表中的数据。有关将视图用于安全目的的更多信息,请参见将视图用作安全机制。

To Simplify Data Manipulation Views can simplify how users manipulate data. You can define frequently used joins, projections, UNION queries, and SELECT queries as views so that users do not have to specify all the conditions and qualifications each time an additional operation is performed on that data. For example, a complex query that is used for reporting purposes and performs subqueries, outer joins, and aggregation to retrieve data from a group of tables can be created as a view. The view simplifies access to the data because the underlying query does not have to be written or submitted each time the report is generated; the view is queried instead. For more information about manipulating data.

还可以创建逻辑上作为参数化视图操作的内联用户定义函数,或者在where子句搜索条件中具有参数的视图。有关更多信息,请参见内联用户定义函数。

自定义数据 视图允许不同的用户以不同的方式查看数据,即使他们同时使用相同的数据。当具有许多不同兴趣和技能水平的用户共享同一个数据库时,这是特别有利的。例如,可以创建一个视图,该视图仅检索与客户经理打交道的客户的数据。该视图可以根据使用该视图的帐户经理的登录ID确定检索哪些数据。

To Export and Import Data Views can be used to export data to other applications. For example, you may want to use the stores and sales tables in the pubs database to analyze sales data using Microsoft® Excel. To do this, you can create a view based on the stores and sales tables. You can then use the bcp utility to export the data defined by the view. Data can also be imported into certain views from data files using the bcp utility or BULK INSERT statement providing that rows can be inserted into the view using the INSERT statement. For more information about the restrictions for copying data into views, see INSERT. For more information about using the bcp utility and BULK INSERT statement to copy data to and from a view, see Copying To or From a View.

To Combine Partitioned Data The Transact-SQL UNION set operator can be used within a view to combine the results of two or more queries from separate tables into a single result set. This appears to the user as a single table called a partitioned view. For example, if one table contains sales data for Washington, and another table contains sales data for California, a view could be created from the UNION of those tables. The view represents the sales data for both regions. To use partitioned views, you create several identical tables, specifying a constraint to determine the range of data that can be added to each table. The view is then created using these base tables. When the view is queried, SQL Server automatically determines which tables are affected by the query and references only those tables. For example, if a query specifies that only sales data for the state of Washington is required, SQL Server reads only the table containing the Washington sales data; no other tables are accessed.

Partitioned views can be based on data from multiple heterogeneous sources, such as remote servers, not just tables in the same database. For example, to combine data from different remote servers each of which stores data for a different region of your organization, you can create distributed queries that retrieve data from each data source, and then create a view based on those distributed queries. Any queries read only data from the tables on the remote servers that contains the data requested by the query; the other servers referenced by the distributed queries in the view are not accessed.

When you partition data across multiple tables or multiple servers, queries accessing only a fraction of the data can run faster because there is less data to scan. If the tables are located on different servers, or on a computer with multiple processors, each table involved in the query can also be scanned in parallel, thereby improving query performance. Additionally, maintenance tasks, such as rebuilding indexes or backing up a table, can execute more quickly. By using a partitioned view, the data still appears as a single table and can be queried as such without having to reference the correct underlying table manually.

如果满足以下条件之一,分区视图是可更新的: 在视图上定义了一个INSTEAD OF触发器,其逻辑支持INSERT、UPDATE和DELETE语句。

视图和INSERT、UPDATE和DELETE语句都遵循为可更新分区视图定义的规则。有关更多信息,请参见创建分区视图。

https://technet.microsoft.com/en-us/library/aa214282 (v = sql.80) . aspx # sql:加入

为了安全性:仅允许每个用户通过包含用户或用户组有权查看的特定数据的一小组视图访问数据库,限制用户对其他数据的访问。

查询和结构的简单性:视图可以从多个表中提取数据并呈现单个表,简化信息并将多表查询转换为视图的单表查询,它为用户提供数据库结构的特定视图,将数据库呈现为特定用户或用户组的一组虚拟表。

为了创建一致的数据库结构:即使底层源表发生了更改,视图也会显示一致的、未更改的数据库结构映像。