我有一个存储过程 proc P
表a 表b
其中
第一步 先执行了一个 select 联合a和b查询 需要花费5分钟
第二步 对b执行了delete 删除部分数据
第三步 对b执行了insert 数据就是第一步的结果集1、请问这样执行第一步的时候 会锁b表么?
我把第二步放在第一步前面好像是锁了。2、如果查询期间不锁表的话,那么查询花费这么多时间,期间b表又老在变化(会影响a,b查询时条件范围)
会有问题么?3、我的目的是在第一步执行的时候不锁住b表,因为有其他操作要频繁操作更新b表。
请问上面3个操作要怎样做?
表a 表b
其中
第一步 先执行了一个 select 联合a和b查询 需要花费5分钟
第二步 对b执行了delete 删除部分数据
第三步 对b执行了insert 数据就是第一步的结果集1、请问这样执行第一步的时候 会锁b表么?
我把第二步放在第一步前面好像是锁了。2、如果查询期间不锁表的话,那么查询花费这么多时间,期间b表又老在变化(会影响a,b查询时条件范围)
会有问题么?3、我的目的是在第一步执行的时候不锁住b表,因为有其他操作要频繁操作更新b表。
请问上面3个操作要怎样做?
If not object_id('[a]') is null
Drop table [a]
Go
Create table [a](xm varchar(2),sl int)
Insert [a]
Select 'P1',10 union all
Select 'p2',20 union all
Select 'p3',30
GoIf not object_id('[b]') is null
Drop table [b]
Go
Create table [b](xm varchar(2),sl int)
Insert [b]
Select 'P1',10 union all
Select 'p3',30
Goselect * from a ---原始表
select * from b/*
xm sl
---- -----------
P1 10
p2 20
p3 30(3 行受影响)xm sl
---- -----------
P1 10
p3 30
*/
If not object_id('ff') is null
Drop proc ff
Go
create proc ff
as
select a.* into #t from a where not exists(select 1 from b where a.xm=b.xm) ---1delete from b where xm='p1' --2insert b select * from #t --3
go
exec ffselect * from a ---执行存储过程之后
select * from b/*
xm sl
---- -----------
P1 10
p2 20
p3 30(3 行受影响)xm sl
---- -----------
p2 20 ---应该在第一步执行时没有锁B
p3 30(2 行受影响)
*/
第二部:会X锁的
3、如果非要这样的话,可以select * from B (nolock)强制不加锁
这样也行吧?