📜  SQL 中视图和物化视图的区别

📅  最后修改于: 2021-09-10 02:22:09             🧑  作者: Mango

意见:
视图是充当实际关系的虚拟关系。它不是数据库系统逻辑关系模型的一部分。视图的元组不存储在数据库系统中,每次访问视图时都会生成视图的元组。视图的查询表达式存储在数据库系统中。

如果我们可以使用实际关系,则可以在任何地方使用视图。视图可用于根据特定用户的需要创建自定义虚拟关系。我们可以在数据库系统中创建任意数量的视图。

物化视图:
当视图表达式的结果存储在数据库系统中时,它们被称为物化视图。 SQL 没有提供任何定义实体化视图的标准方法,但是一些数据库管理系统提供了自定义扩展来使用实体化视图。保持物化视图更新的过程称为视图维护。

数据库系统使用以下三种方式之一来保持物化视图更新:

  • 一旦定义实体化视图的关系更新,立即更新实体化视图。
  • 每次访问视图时更新物化视图。
  • 定期更新物化视图。

当视图被频繁访问时,物化视图很有用,因为它节省了计算时间,因为结果事先存储在数据库中。在定义视图的关系非常大而视图的结果关系非常小的情况下,物化视图也很有帮助。物化视图具有与之相关的存储成本和更新开销。

视图和物化视图的区别:

Views Materialized Views
Query expression are stored in the databases system, and not the resulting tuples of the query expression. Resulting tuples of the query expression are stored in the databases system.
Views needs not to be updated every time the relation on which view is defined is updated, as the tuples of the views are computed every time when the view is accessed. Materialized views are updated as the tuples are stored in the database system. It can be updated in one of three ways depending on the databases system as mentioned above.
It does not have any storage cost associated with it. It does have a storage cost associated with it.
It does not have any updation cost associated with it. It does have updation cost associated with it.
There is an SQL standard of defining a view. There is no SQL standard for defining a materialized view, and the functionality is provided by some databases systems as an extension.
Views are useful when the view is accessed infrequently. Materialized views are efficient when the view is accessed frequently as it saves the computation time by storing the results before hand.