3000万条记录.将它们分成24小时.总结一个月.冲洗并重复

Ron*_*gen 5 sql t-sql sql-server

我需要检查3000万条记录(一个月价值)的每日票证验证(Unix日期时间),并将它们分成24个一小时的时间段,用于211个站点.

首先,我创建了一个视图,选择我正在寻找的月份(以及设备类型),然后为每个Unix日期时间创建一个Windows Datetime值.

SELECT TOP (100) PERCENT StationName AS Station, MainTable.UnixDateTime AS ValTime, 
DATEADD(s, MainTable.UnixDateTime, CONVERT(DATETIME, '1970-01-01 00:00:00', 102)) AS WinTime
FROM  MainTable 
INNER JOIN StationName ON MainTable.StationID = StationName.StationID
WHERE (StationName.ValidStationCode = 32767) [use only valid stations] 
AND (MainTable. UnixDateTime >= 1264996800) 
AND (MainTable. UnixDateTime < 1267416000) 
AND (MainTable.EquipmentCode IN (33, 36)) [examine only this equipment]
ORDER BY Station
Run Code Online (Sandbox Code Playgroud)

然后,我运行主程序,对于每个一小时的时段,在该视图上使用select语句.这是211个站中每个站的24个选择语句.

例1)

Update table Set [0102]= (select count(ValTime) 
from view 
where Station = @thisStation and DatePart (Hour, WinTime)>= 1 and DatePart (Hour, WinTime)< 2)
from view 
where table.Station = @thisStation
Run Code Online (Sandbox Code Playgroud)

例2)

Update table Set [0203]= (select count(ValTime) 
from view 
where Station = @thisStation and DatePart (Hour, WinTime)>= 2 and DatePart (Hour, WinTime)< 3) 
from view 
where table.Station = @thisStation
Run Code Online (Sandbox Code Playgroud)

等等

程序有效 - 耶!这需要11个小时 - 嘘:(

输出例如:

         0001 0102 0203 0304...2324
Station1   27  34   567  231... 123
Station2  245  57   23   198... 21
etc.
Run Code Online (Sandbox Code Playgroud)

输出表有25列.

有一种更快,更好的方法,但我不知道它是什么.(买一台超级计算机?)我考虑过排名和分区,但无法想象它是如何工作的.那里有专家吗?

Dav*_*sky 3

DatePart 函数可能会影响您的性能。我认为将日期分成日、小时、分钟列会大大加快速度。向这些列添加索引,您可以查询文字值而不是计算值。