0
任何人都可以幫助我更有效地寫這個查詢嗎?查詢更新rowNum
我有一個捕獲TCP流量的表,我想更新一個名爲RowNumForFlow的列,它與此流中的IP數據包的連續編號類似。下面的代碼工作正常,但速度很慢。
declare @FlowID int
declare @LastRowNumInFlow int
declare @counter1 int
set @counter1 = 0
while (@counter1 < 1)
BEGIN
set @counter1 = @counter1 + 1
-- 1)
select top 1
@FlowID = t.FlowID
from Traffic t
where t.RowNumInFlow is null
if (@FlowID is null)
break
-- 2)
set @LastRowNumInFlow = null
select top 1
@LastRowNumInFlow = RowNumInFlow
from Traffic
where [email protected] and RowNumInFlow is not null
order by ID desc
if @LastRowNumInFlow is null
set @LastRowNumInFlow = 1
else
set @LastRowNumInFlow = @LastRowNumInFlow + 1
update Traffic set RowNumInFlow = @LastRowNumInFlow
where ID = (select top 1 ID from Traffic where
flowid = @FlowID and RowNumInFlow is null)
END
查詢後實例表值已運行:
ID FlowID RowNumInFlow 448923 44 1 448924 44 2 448988 44 3 448989 44 4 448990 44 5 448991 44 6 448992 44 7 448993 44 8 448995 44 9 448996 44 10 449065 44 11 449063 45 1 449170 45 2 449171 45 3 449172 45 4 449187 45 5
這看起來很不錯。分區條款似乎是我需要的。有沒有辦法只更新尚未填充的行? – BrokeMyLegBiking 2010-05-07 05:50:55
這工作:(我添加了where子句)。這比我以前的查詢運行速度快大約10萬倍。 :) 恭喜! 「其中Traffic.ID = T.ID」 – BrokeMyLegBiking 2010-05-07 06:05:49