我有一张#TrackPlayedInformation
我正在循环的桌子。的样本数据#TrackPlayedInformation
如下:
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
是否不存在时,如果存在则更新 aircount 增加 1,否则作为新条目插入。
查询执行大约需要 20 分钟才能完成,日期间隔约为 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 Album.UserId=@UserId) 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<=@EndTime
begin
set @StartTime=DATEADD(minute,1,@StartTime)
--checking for duplication
--SELECT @countCheck= count(TempGraphId) FROM TempGraph WHERE AirTime=@StartTime
--select @countCheck
--if (@countCheck<1)
if not exists(select top 1 TempGraphId from TempGraph where AirTime=@StartTime)
begin
--print('inserting')
insert TempGraph (AirTime,AirCount) values(@StartTime,0)
end
else
begin
--print('updating')
update TempGraph set AirCount=AirCount+1 where AirTime=@StartTime
end
set @LastDate=@StartTime
end
set @LastDate=DATEADD(MINUTE,1,@LastDate);
--deleting row from #TrackPlayedInformation
--print('deleting')
delete from #TrackPlayedInformation where ProfileTrackTimeId=@ProfileTrackTimeId
set @loopCount=@loopCount+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
我正在尝试按分钟拆分时间间隔,例如-将日期 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 .....截至 2014 年 01 01 5.50