什么时候以及为什么有些人决定他们需要在他们的数据库中创建一个视图?为什么不运行一个普通的存储过程或选择?
当前回答
我通常创建视图来反规范化和/或聚合经常用于报告目的的数据。
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视图上建立索引 在该软件中) 让数据更容易获取 对于团队成员和非开发人员来说是直观的。
其他回答
我通常创建视图来反规范化和/或聚合经常用于报告目的的数据。
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报表似乎更喜欢使用视图来存储过程,因此编写大量报表的人倾向于使用大量视图
视图在重构数据库时也非常有用。您通常可以通过创建一个视图来隐藏更改,以便旧代码看不到它。阅读重构数据库的相关内容,了解它是如何工作的,因为这是一种非常强大的重构方式。
在其他方面,它可以用于安全。如果您有一个“customer”表,您可能希望让所有销售人员访问名称、地址、邮政编码等字段,但不允许credit_card_number。您可以创建一个只包含他们需要访问的列的视图,然后授予他们对视图的访问权。
这并没有确切地回答你的问题,但我认为值得一提的是物化视图。我的经验主要是使用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.
在我看来,存储过程更多的是一种可以针对数据调用的方法,而视图提供了一种机制来创建基础数据的合成版本,可以根据它创建查询或存储过程。当简化或聚合有意义时,我将创建一个视图。当我想提供一个非常特定的服务时,我将编写一个存储过程。
推荐文章
- 如何改变字符集(和排序)在整个数据库?
- mySQL:: insert到表,数据从另一个表?
- nvarchar(max)非文本
- 如何在Postgres中获得两个字段的MIN() ?
- 在SQL Server 2008 R2中重命名数据库时出错
- 将数据复制到另一个表中
- 将表从一个数据库复制到另一个数据库的最简单方法?
- 如何在SQL中选择表的最后一条记录?
- SQL在Oracle中连接多行列值的查询
- 修改列,添加默认约束
- 在单个查询中计算空值和非空值
- 在存储过程中使用“SET XACT_ABORT ON”有什么好处?
- 什么是分片,为什么它很重要?
- SQL to LINQ工具
- 如何从一个查询插入多行使用雄辩/流利