标题可能和正文不太相符。我主要是记录工作中遇到使用游标的语句改成普通set-based operation,执行时间快了很多。

1、游标语句

declare @startDate dateTime
declare @endDate dateTime
set @startDate = convert(varchar(10),dateAdd(day,-1,getDate()),120)
set @endDate = convert(varchar(10),getDate(),120)

declare @serverID int
declare loop_cursor cursor for
select distinct serverID from OnLineUserStat with(nolock)
where realTime between @startDate and @endDate
open loop_cursor
fetch next from loop_cursor into @serverID
while @@fetch_status = 0
begin
    declare @loopTime dateTime
    set @loopTime = @startDate
    while @loopTime < @endDate
    begin
        insert into OnLineUserStat2
        (serverID,kindID,OnLineUserCount,playUserCount,RoomName,StatTime,RealTime)
        select top 1 serverID,kindID,OnLineUserCount,playUserCount,RoomName,@looptime,RealTime
        from OnLineUserStat with(nolock)
        where serverID = @serverID and statTime <= @loopTime
        and dateDiff(minute,statTime,@loopTime) <= 7
        order by statTime desc
        
        set @loopTime = dateAdd(minute,5,@loopTime)
    end
    fetch next from loop_cursor into @serverID
end
close loop_cursor
deallocate loop_cursor
View Code

相关文章:

  • 2021-06-22
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-02
  • 2021-10-23
  • 2021-11-14
  • 2021-11-05
  • 2022-12-23
  • 2021-10-13
  • 2021-08-06
相关资源
相似解决方案