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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/311429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:20:03  来源:igfitidea点击:

Using WITH NOLOCK Table Hint in Query Using View - Does it Propagate Within the View?

sqlsql-serverlocking

提问by Turnkey

If a "WITH NOLOCK" query hint is used on a View in SQL Server, does it propagate that hint to the view definition itself, even if NOLOCK is NOT used for the raw tables in the View definition? The reason to need this is that sometimes the support staff wants to do huge time-consuming queries but would rather not force this lock on all queries using the view within the application itself.

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

回答by Rory

Yes, NOLOCK will propagate to the tables used by the view definition (at least in SQL Server 2005).

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

See Table Hintsin MSDN:

请参阅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.

在 SQL Server 2005 中,所有锁定提示都会传播到视图中引用的所有表和视图。此外,SQL Server 执行相应的锁一致性检查。

However,

然而,

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.

如果表包含计算列并且计算列是由访问其他表中的列的表达式或函数计算的,则不会在这些表上使用表提示。这意味着不会传播表提示。例如,在查询中的表上指定了 NOLOCK 表提示。此表具有计算列,这些列由访问另一个表中的列的表达式和函数的组合计算得出。表达式和函数引用的表在访问时不使用 NOLOCK 表提示。

If you're using indexed views you might want to read a bit more as there are some special cases there too.

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

Also see View Resolutionfor more info.

另请参阅查看分辨率了解更多信息。

回答by Jim V.

Just to supplement Rory's excellent answer.

只是为了补充罗里的出色回答。

He writes "Yes, NOLOCK will propagate to the tables used by the view definition (at least in SQL Server 2005)."

他写道“是的,NOLOCK 将传播到视图定义使用的表(至少在 SQL Server 2005 中)。”

In fact this will work in SQL 2000 as well. From BOL:

事实上,这也适用于 SQL 2000。 来自 BOL:

Because select_statement uses the SELECT statement, it is valid to use and hints as specified in the FROM clause. For more information, see FROMand SELECT.

因为 select_statement 使用 SELECT 语句,所以使用和提示在 FROM 子句中指定是有效的。有关更多信息,请参阅FROMSELECT