我有2个表Target和Source,我必须通过比较Source表在Target表上执行Insert和delete。我必须在块中执行此操作,因为我希望保持最大并发性,因为多个线程正在使用Source和Target表,而源Table有1000万条记录。我有两个问题 -
我害怕在生产中使用我的以下代码。所以我想确认我的下面的代码是否可以安全使用,并且它会提高性能。这两个表在连接列(ID)上都有聚簇索引,在列上有非聚集索引 - name,v1,v2,v3。
declare @i int = 1
while @i > 0
begin
merge top (1000) target as a
using source as b
on a.id = b.id
when not matched by target then
insert (id,name,v1,v2,v3)
values(b.id,b.name,b.v1,b.v2,b.v3)
when not matched by source then
delete;
set @i = @@rowcount
end
我的第二个问题是 - 我对Merge语句进行了一些搜索,发现大多数开发人员都喜欢使用旧方法插入和删除表中使用单独的Insert和delete语句的行,因为存在因为合并导致锁定问题。我想知道merge语句的所有缺点和锁定问题的原因。
请指教?谢谢.. :))