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


当前回答

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

其他回答

视图提供了几个好处。

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.

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

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视图上建立索引 在该软件中) 让数据更容易获取 对于团队成员和非开发人员来说是直观的。

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