sql - 在使用 View 的查询中使用WITH NOLOCK 表提示 - 它是否在 View 内传播?

标签 sql sql-server locking

如果在 SQL Server 中的 View 上使用“WITH NOLOCK”查询提示,即使 View 定义中的原始表未使用 NOLOCK,它是否会将该提示传播到 View 定义本身?需要这样做的原因是,有时支持人员想要执行大量耗时的查询,但又不想使用应用程序本身的 View 对所有查询强制锁定。

最佳答案

是的,NOLOCK 将传播到 View 定义所使用的表(至少在 SQL Server 2005 中)。

参见Table Hints在 MSDN 中:

In SQL Server 2005, all lock hints are propagated to all the tables and views that are referenced in a view. Also, SQL Server performs the corresponding lock consistency checks.

但是,

If a table contains computed columns and the computed columns are computed by expressions or functions accessing columns in other tables, the table hints are not used on those tables. This means the table hints are not propagated. For example, a NOLOCK table hint is specified on a table in the query. This table has computed columns that are computed by a combination of expressions and functions that access columns in another table. The tables referenced by the expressions and functions do not use the NOLOCK table hint when accessed.

如果您使用索引 View ,您可能需要阅读更多内容,因为也有一些特殊情况。

另请参阅View Resolution了解更多信息。

关于sql - 在使用 View 的查询中使用WITH NOLOCK 表提示 - 它是否在 View 内传播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/981671/

相关文章:

获取数据时 Java 与 SQL

sql - postgresql 断言约束

php - 如何为我的应用程序创建索引数据库

sql - 如何获取存储过程参数详细信息?

ios - 如何从 NSOperations 收集数据到数组中?

mysql - 保护表不被插入错误数据

sql-server - 使用@@identity而不是scope_identity的原因

sql-server - 尝试在 SQL Server 中创建或编辑作业步骤时出错 "The system cannot find the file specified."

c# - NHibernate 锁定数据库表以避免插入 "duplicates"

c++ - 使用 C++ lock_guard 时如何收紧范围?