了解 SQL Server 中的锁定行为
我尝试重现问题[1]的情况。
在桌子上,获取并填充了来自 wiki 的“隔离(数据库系统)”的数据 [2],
在 SQL Server 2008 R2 SSMS 中,我执行了:
1)首先在 SSMS 的第一个选项卡(窗口)中执行
-- transaction isolation level in first window does not influence results (?)
-- initially I thought that second transaction in 2) runs at the level set in first window
begin transaction
INSERT INTO users VALUES ( 3, 'Bob', 27 )
waitfor delay '00:00:22'
rollback
2)紧接着在第二个窗口中
-- this is what I commented/uncommented
-- set transaction isolation level SERIALIZABLE
-- set transaction isolation level READ REPEATABLE
-- set transaction isolation level READ COMMITTED
-- set transaction isolation level READ UNCOMMITTED
SELECT * FROM users --WITH(NOLOCK)
执行更新:
抱歉,结果已更正。
根据 2) 中设置的隔离级别,我的结果是 SELECT 返回:
立即(读取未提交的插入行)
- 适用于所有带有 NOLOCK 的 SELECT 情况
- 对于 READ UNCOMMITTED(选择带或不带 NOLOCK)
正在等待事务 1 的完成)(仅当 SELECT 没有 NOLOCK)并且
- 处于 READ COMMITTED 及更高(REPEATABLE READ、SERIALIZABLE)事务隔离级别
这些结果与问题中描述的情况相矛盾(并在答案中解释?)[1]
(例如,带有 NOCHECK 的 SELECT 正在等待 1) 的完成),等等。
如何解释我的结果和 [1]?
更新2:
这个问题实际上是我的问题[3]的子问题(或者是没有得到回答的结果)。
引用:
[1]
解释 SQL Server 中的锁定行为
解释 SQL Server 中的锁定行为
[2]
“隔离(数据库系统)”
请添加尾随 ) 到链接。我无法在链接中保存它! http://en.wikipedia.org/wiki/Isolation_(database_systems)< br> [3]
NOLOCK 是 SQL Server 2005 中 SELECT 语句的默认设置吗?
NOLOCK 是 SQL Server 中 SELECT 语句的默认值吗2005年?
I tried to reproduce the situation of question [1].
On table, taken and filled with data from wiki's "Isolation (database systems)" [2],
in SQL Server 2008 R2 SSMS, I executed:
1) first in first tab (window) of SSMS
-- transaction isolation level in first window does not influence results (?)
-- initially I thought that second transaction in 2) runs at the level set in first window
begin transaction
INSERT INTO users VALUES ( 3, 'Bob', 27 )
waitfor delay '00:00:22'
rollback
2) immediately after, in second window
-- this is what I commented/uncommented
-- set transaction isolation level SERIALIZABLE
-- set transaction isolation level READ REPEATABLE
-- set transaction isolation level READ COMMITTED
-- set transaction isolation level READ UNCOMMITTED
SELECT * FROM users --WITH(NOLOCK)
Update:
Sorry, results were corrected.
My results, depending on isolation level set in 2), are that SELECT returns:
immediately (reading uncommitted inserted row)
- for all cases of SELECT with NOLOCK
- for READ UNCOMMITTED (SELECT either with or without NOLOCK)
is waiting the completion of transaction 1) (ONLY IF SELECT is without NOLOCK) and
- in READ COMMITTED and higher (REPEATABLE READ, SERIALIZABLE) transaction isolation level
These results contradict to situation described in question (and explained in answers?) [1]
(for example, that SELECT with NOCHECK is waiting completion of 1)), etc.
How can my results and [1] be explained?
Update2:
This question is really subquestion of my questions [3] (or the result of them not being answered).
Cited:
[1]
Explain locking behavior in SQL Server
Explain locking behavior in SQL Server
[2]
"Isolation (database systems)"
Plz add trailing ) to link. I cannot manage to preserve it here in the link!
http://en.wikipedia.org/wiki/Isolation_(database_systems)
[3]
Is NOLOCK the default for SELECT statements in SQL Server 2005?
Is NOLOCK the default for SELECT statements in SQL Server 2005?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个有用的 MSDN 链接,她谈论了 SQL 2008 中的锁定提示。也许在你的例子中,SQL Server 2008不利于你的表锁?
(以下链接中的以下代码段讨论了 SQL Server 2008 可能会忽略的锁)
如以下示例所示,如果事务隔离级别设置为 SERIALIZABLE,并且表级锁定提示 NOLOCK 与 SELECT 语句一起使用,通常用于维护可序列化事务的键范围锁不会被采用。
引用 HumanResources.Employee 的唯一锁是架构稳定性(Sch-S) 锁。在这种情况下,不再保证可串行性。
在 SQL Server 2008 中,A LTER TABLE 的 LOCK_ESCALATION 选项可以不支持表锁,并在分区表上启用 HoBT 锁。此选项不是锁定提示,但可以用于减少锁定升级。有关详细信息,请参阅ALTER TABLE (Transact-SQL)。
There is a useful MSDN link her talk about locking hints in SQL 2008. Maybe in your example its a case of SQL Server 2008 disfavoring your tables locks?
(The following snippet from the link below talks about locks potentially being ingored by SQL Server 2008)
As shown in the following example, if the transaction isolation level is set to SERIALIZABLE, and the table-level locking hint NOLOCK is used with the SELECT statement, key-range locks typically used to maintain serializable transactions are not taken.
The only lock taken that references HumanResources.Employee is a schema stability (Sch-S) lock. In this case, serializability is no longer guaranteed.
In SQL Server 2008, the LOCK_ESCALATION option of A LTER TABLE can disfavor table locks, and enable HoBT locks on partitioned tables. This option is not a locking hint, but can but used to reduce lock escalation. For more information, see ALTER TABLE (Transact-SQL).
第二个查询中的提示会覆盖事务隔离级别。
SELECT ...WITH (NOLOCK)
与SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 基本相同;选择...
。对于任何其他隔离级别,都会遵守锁,因此第二个事务会等待,直到第一个事务释放锁。
The hint in the second query overrides transaction isolation level.
SELECT ... WITH (NOLOCK)
is basically identical toSET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT ...
.With any other isolation level the locks are honored, so the second transaction waits until the locks are released by the first one.