创建 View 时(假设在 SQL Server (2016) 上,但我猜这不是特定于 DBMS 的):
View 的数据是否存储在其他地方,因此需要额外的内存(作为数据的重复)?
那么当查询的表发生变化时,引擎如何更新 View 的数据呢?是否有额外的计算时间成本?
否则,我们可以说 View 只是“保存 SELECT 的一种方式”吗?
最佳答案
View
只是您设置用于查看数据的角度。 查看
它本身,默认情况下不包含数据,如果您查看 View 的执行计划,您会注意到SQL Server将在 View 内执行查询。
但这并不是故事的全部。如果您具体化了一个 View (在View
的列上创建索引),那么View
也将包含数据。 MSDN 对物化 View
的描述如下:
The Materialized View pattern describes generating prepopulated views of data in environments where the source data isn't in a suitable format for querying, where generating a suitable query is difficult, or where query performance is poor due to the nature of the data or the data store.
关于物化 View
的问题是,View
数据的更新频率是多少? MSDN 说:
When the source data for the view changes, the view must be updated to include the new information. You can schedule this to happen automatically, or when the system detects a change to the original data. In some cases it might be necessary to regenerate the view manually.
关于sql-server - SQL Server 中 View 的成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48565943/