我有一個表我正在循環的表#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/ –