我有一个表我正在循环的表#TrackPlayedInformation
。的#TrackPlayedInformation
样本数据如下:SQL Server循环执行需要太多时间
ProfileTrackTimeId JukeBoxTrackId ProfileId EndTime SessionId StartTime
14 52 33 2014-08-16 05:47:19.410 23424234 2014-08-16 05:45:19.410
15 51 33 2014-11-16 05:47:19.410 23424234 2014-08-16 05:45:19.410
我循环通过#TrackPlayedInformation
并分割的开始时间和结束时间上每分钟之间的时间间隔。新的时间插在一个物理表TempGraph
TempGraph的结构是
TempGraphId AirTime AirCount
170390 2014-08-16 05:46:19.410 0
170391 2014-08-16 05:47:19.410 0
虽然插入到TempGraph
如果不存在被检查,如果存在,它更新由1 aircount递增否则插入新的条目。
查询执行大约需要20分钟才能完成id,日期间隔约为3个月。有没有更快的方法来实现结果?
我锻炼的查询如下:
USE [SocialMob]
GO
/****** Object: StoredProcedure [dbo].[pDeleteTempGraph] Script Date: 01/02/2015 09:00:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[pDeleteTempGraph]
AS
BEGIN
print('start')
declare @UserId int
declare @ProfileTrackTimeId int
set @UserId=1048
drop table #TrackPlayedInformation
delete from TempGraph
declare @loopCount int
declare @StartTime datetime
declare @LastDate datetime
declare @tempCount int
declare @EndTime datetime
declare @SaveTime datetime
declare @checkDate datetime
declare @countCheck int
--querying input--
--drop table #TrackPlayedInformation
--declare @UserId int
--set @UserId=33
SELECT ProfileTrackTimeId,ProfileTrackTime.JukeBoxTrackId,ProfileId,EndTime,SessionId,StartTime into #TrackPlayedInformation
FROM ProfileTrackTime LEFT JOIN
(SELECT JukeBoxTrackId FROM JukeBoxTrack INNER JOIN
Album ON JukeBoxTrack.AlbumId=Album.AlbumId WHERE [email protected]) as AllTrackId
ON ProfileTrackTime.JukeBoxTrackId=AllTrackId.JukeBoxTrackId
set @loopCount=0
declare @count as int
select @count=COUNT(ProfileTrackTimeId) from #TrackPlayedInformation
set @LastDate=GETDATE()--storing current datetime
print('looping starts')
while @loopCount<@count
begin
select @StartTime=StartTime from #TrackPlayedInformation
select @EndTime=EndTime from #TrackPlayedInformation
select @ProfileTrackTimeId=ProfileTrackTimeId from #TrackPlayedInformation
--select @checkDate=AirTime from TempGraph
while @StartTime<[email protected]
begin
set @StartTime=DATEADD(minute,1,@StartTime)
--checking for duplication
--SELECT @countCheck= count(TempGraphId) FROM TempGraph WHERE [email protected]
--select @countCheck
--if (@countCheck<1)
if not exists(select top 1 TempGraphId from TempGraph where [email protected])
begin
--print('inserting')
insert TempGraph (AirTime,AirCount) values(@StartTime,0)
end
else
begin
--print('updating')
update TempGraph set AirCount=AirCount+1 where [email protected]
end
set @[email protected]
end
set @LastDate=DATEADD(MINUTE,1,@LastDate);
--deleting row from #TrackPlayedInformation
--print('deleting')
delete from #TrackPlayedInformation where [email protected]
set @[email protected]+1 --incrementing looping condition
end
begin
insert TempGraph (AirTime,AirCount) values(@LastDate,0)
end
begin
declare @nowdate datetime
set @nowdate=GETDATE()
insert TempGraph (AirTime,AirCount) values(@nowdate,0)
end
select * from TempGraph;
delete from TempGraph;
END
我将尝试分钟分裂的时间间隔为EG-考虑日期2014 01 01 5.40为开始时间和2014年01 01 5.50为结束再寄一次需要进入TempGraph 2014年01 01 5.41,2014 01 01 5.42,2014 01 01 5.43 ..... upto 2014 01 01 5.50
最后两个插入(即)'@ LastDate'和'@ nowdate'有什么用处。 –
- @ LastDate用于插入最后一个条目,并且 - @ nowdate用于在当前日期时间插入条目 – M14
*我正在尝试按分钟将时间间隔拆分为eg-考虑日期2014 01 01 5.40作为开始时间和2014 01 01 5。50作为结束时间*只需使用递归cte生成一个0到10之间的数字序列(分钟),然后将'DATEADD'到他们的开始时间。 http://smehrozalam.wordpress.com/2009/06/09/t-sql-using-common-table-expressions-cte-to-generate-sequences/ –