SQL 服务器数据库锁 - select 无锁
SQL Server DB Locks - select with nolock
我们有几个调用其他视图的视图。如果父视图使用 NOLOCK 从子视图中进行选择,但子视图 缺少 NOLOCK,这会导致锁定吗?
IE:
VIEW1:
select * from view2 WITH (NOLOCK)
VIEW2:
select * from hugetable
从视图 1 中选择是否会有效地忽略 NOLOCK
,因为它在视图 2 的定义中缺失?
All lock hints are propagated to all the tables and views that are
accessed by the query plan, including tables and views referenced in a
view.
这很容易测试。
设置
CREATE DATABASE Testing
GO
ALTER DATABASE Testing SET READ_COMMITTED_SNAPSHOT OFF
GO
USE Testing
GO
CREATE TABLE dbo.Demo(X int);
INSERT INTO dbo.Demo VALUES (1), (2), (3);
go
CREATE VIEW dbo.[Inner] AS
SELECT *
FROM dbo.Demo
GO
CREATE VIEW dbo.[Outer] AS
SELECT *
FROM dbo.[Inner]
WITH (NOLOCK)
连接 1(保持事务处于打开状态并获取锁和未提交的行)
BEGIN TRAN
INSERT INTO dbo.Demo VALUES (4);
连接 2
SELECT *
FROM dbo.[Outer]
Returns
X
-----------
1
2
3
4
显示 NOLOCK
提示已向下传播,它从未提交的事务中读取值 4
。 (从 dbo.[Inner]
中选择按预期被阻止)
我们有几个调用其他视图的视图。如果父视图使用 NOLOCK 从子视图中进行选择,但子视图 缺少 NOLOCK,这会导致锁定吗?
IE:
VIEW1:
select * from view2 WITH (NOLOCK)
VIEW2:
select * from hugetable
从视图 1 中选择是否会有效地忽略 NOLOCK
,因为它在视图 2 的定义中缺失?
All lock hints are propagated to all the tables and views that are accessed by the query plan, including tables and views referenced in a view.
这很容易测试。
设置
CREATE DATABASE Testing
GO
ALTER DATABASE Testing SET READ_COMMITTED_SNAPSHOT OFF
GO
USE Testing
GO
CREATE TABLE dbo.Demo(X int);
INSERT INTO dbo.Demo VALUES (1), (2), (3);
go
CREATE VIEW dbo.[Inner] AS
SELECT *
FROM dbo.Demo
GO
CREATE VIEW dbo.[Outer] AS
SELECT *
FROM dbo.[Inner]
WITH (NOLOCK)
连接 1(保持事务处于打开状态并获取锁和未提交的行)
BEGIN TRAN
INSERT INTO dbo.Demo VALUES (4);
连接 2
SELECT *
FROM dbo.[Outer]
Returns
X
-----------
1
2
3
4
显示 NOLOCK
提示已向下传播,它从未提交的事务中读取值 4
。 (从 dbo.[Inner]
中选择按预期被阻止)