SQL Server基于行锁死锁信息查看

如果锁的资源为行锁,说明表没有主键,并且影响的数据范围非常小,此时SQL语句可能用到了非聚集索引,也可能没有用非聚集索引。
说明:星空产品下表都有聚集索引,不会存在没有聚集索引情况,这里只是作为演示用途。
1.输出死锁信息到错误日志dbcc traceon(1222,-1),这样就可以在错误日志中查到死锁信息
2.准备数据
if object_id('testdeadlock')>0 drop table testdeadlock;
create table testdeadlock(fid int not null,fname varchar(20));
insert into testdeadlock values(1,'bob'),(2,'joe'),(3,'tom');
create index idx_1 on testdeadlock(fid)
insert into testdeadlock select * from testdeadlock --重复执行,产生如10W行数据
commit;
dbcc dbreindex(testdeadlock)
3. 测试
事务一:
步骤1:
begin tran
update top(1) testdeadlock set fid=fid where fid=2;--由于表有大量的重复数据,如果不指定更新行大小,那么锁会变为表锁
--步骤3:
update top(1) testdeadlock set fid=fid where fid=1;
rollback
事务二:
步骤2:
begin tran
update top(1) testdeadlock set fid=fid where fid=1;
--步骤四:
update top(1) testdeadlock set fid=fid
SQL Server基于行锁死锁信息查看
声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。如若本站内容侵犯了原著者的合法权益,可联系本站删除。



