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/311429/

相关文章:

sql - 创建触发器防止插入

mysql - 使用条件列表的任何子集查询数据库

sql-server - TSQL程序求最大素数

mysql - 获得非付费成员(member)

数据库事务和锁定

SQL Server : Storing Query Result into Variable

sql - 计数除以 0

c++ - mutex.timed_lock(duration) 和 boost::timed_mutex::scoped_lock 之间的区别 scoped_lock(mutex, duration)

android - devicePolicyManager.lockNow() 不适用于摩托罗拉平板电脑

mysql - 在连接条件下使用函数,是否会因为缺少可用索引而导致全表扫描?