sql-server - SQL Server 中 View 的成本

标签 sql-server database t-sql view

创建 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.

Read more about materialized view

关于sql-server - SQL Server 中 View 的成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48565943/

相关文章:

python - Django Model() 与 Model.objects.create()

像 PEPSI 这样的 SQL SERVER PIVOT 按年划分,然后按月份划分等。(请参阅附图以获得清晰的想法)

sql - 返回逗号分隔值 SQL

来自不同数据库的 Mysqldump 表?

sql - 需要帮助为我的问题提出 sql 查询

sql - 根据列值重置行号T-SQL

sql - 哪个更快/更好 : UPDATE WHERE IN or MERGE?

c# - SQL CLR中的存储过程

sql-server - 组内 STRING_AGG 和 Visual Studio 的问题

sql-server - 主键自动成为索引吗?