http://www.cnblogs.com/huangxincheng/p/4292320.html 关于sql 中的锁。
1 排他锁 sql中在做 insert update delete 会存在排它锁,在这几个操作进行时,其他操作都会等待。
2 共享锁 对于select 会加共享锁 ,就是在读取时,其他操作只能读取。
--查询窗口一
begin
tran
update
test
set
c =
'2'
where
id = 2
--查询窗口二
select
*
from
test
where
id = 2
当查询窗口一的事务没有提交时,其它查询是无法读取的。这是sql server 的默认隔离级别,保证了不会出现脏读。接着我们用 nolock 来测试,查询窗口二的sql改为:
--查询窗口二
select
*
from
test
with
(nolock)
where
id = 2
可以看到,即使查询窗口一的事务没有提交,数据依然可以被读取出来。那什么时候出现脏读呢?很简单,当事务rollback时,就读取到脏数据了。
对于一些对数据要求严格的系统,脏读是不可接受的,那么nolock 也就不能使用。但许多时候,我们的查询是可以允许脏读的,因为它带来许多好处。
1 提高查询性能 nolock不受锁的影响,这表示它可以直接读取数据,而不用等到锁释放。
2 避免锁定数据