Sql 输出结果并更新第二个表,而不使用临时表
我目前有以下问题Sql 输出结果并更新第二个表,而不使用临时表,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,我目前有以下问题 SELECT TOP (@batchSize) * into #t from iv.SomeView where EXISTS(select * from iv.PendingChanges where SomeView.RecordGuid = PendingChanges .RecordGuid) update iv.PendingChanges set IsQueued = 1 where RecordGuid
SELECT TOP (@batchSize) * into #t
from iv.SomeView
where EXISTS(select *
from iv.PendingChanges
where SomeView.RecordGuid = PendingChanges .RecordGuid)
update iv.PendingChanges
set IsQueued = 1
where RecordGuid in (select #t.RecordGuid from #t)
select * from #t
这种模式(在各种查询中使用不同的视图和PendingChanges
表)运行得非常频繁,我试图找出如何减少由于写入#t
而导致的对tempdb
的写入
我提出了这个解决方案,与分析器中的旧版本相比,它的性能确实有所提高
select top (0) * into #t from iv.SomeView
insert into #t with (TABLOCK)
output inserted.*
SELECT TOP (@batchSize) *
from iv.SomeView
where EXISTS(select *
from iv.PendingChanges
where SomeView.RecordGuid = PendingChanges.RecordGuid)
update iv.PendingChanges
set IsQueued = 1
where RecordGuid in (select #t.RecordGuid from #t)
但是有没有更好的方法可以让我既能输出结果,又能更新PendingChanges.IsQueued
,而不需要将结果临时写入#t
重要提示:我只能从
iv.SomeView
中选择一个select
,因为该表非常活跃,执行多个select TOP(@batchSize)
是不确定的,我也没有任何字段可供选择。您是否尝试过这样的代码:
update top (@batchsize) PC
Set IsQueued = 1
output inserted.*
From iv.PendingChanges PC
inner join iv.SomeView SV
on iv.SomeView.RecordGuid = iv.PendingChanges.RecordGuid;