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


当前回答

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

其他回答

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

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

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

这并没有确切地回答你的问题,但我认为值得一提的是物化视图。我的经验主要是使用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.

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

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