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


当前回答

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

其他回答

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

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

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

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

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

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

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