Sql游标循环花了一分钟循环遍历20行

use*_*408 0 sql t-sql sql-server loops cursor

下面循环中的代码需要一分钟才能完成.我已经删除了循环体中的所有处理/计算,试图找出所花费的时间如此之长,因此看起来只是迭代行(只有20)才需要时间.是否会将其重写为while循环增加性能,或者下面的代码是否存在固有的错误?

declare tl_cursor cursor local forward_only dynamic optimistic
for 
select EId, Date, Hrs, Code1, Code2,  TLId, Acc
from TLines where Account < 0 and Date=@magicdate
for update of Cost, Charge, Acc, RowId, ParentTLId

open tl_cursor
fetch next from tl_cursor
into @tl_eid, @tl_date, @tl_hrs, @tl_account, @tl_tlid

while @@fetch_status = 0
begin
   --Removed all calculations in there to narrow down bottleneck
NextRecord:
    update TLines set Acc = 0 where current of tl_cursor

    fetch next from tl_cursor
    into @tl_eid, @tl_date, @tl_hrs, @tl_account, @tl_timelineid
end

close tl_cursor
deallocate tl_cursor
Run Code Online (Sandbox Code Playgroud)

Twi*_*les 7

与以下内容不同:

UPDATE TLines 
SET Acc = 0
WHERE Account < 0 AND Date=@magicdate
Run Code Online (Sandbox Code Playgroud)