获取运行频率数据并转换为有效时间的最有效方法


Most efficient way to take run frequency data and convert to valid times

这是一个有点令人困惑的问题。希望我能用一种有意义的方式来表达。

在我的sql server 2008 r2数据库中,我有一个名为expectedTimes的表,它告诉任务以开始和结束时间运行的频率。它看起来像下面的

| taskID | startTime | endTime | freq |
|________|___________|_________|______|
|    1   |    08:00  |   13:45 |  30  |
|    2   |    00:00  |   23:59 |  15  |
|    3   |    06:35  |   20:20 |  10  |
|    4   |    08:00  |   09:00 |  5   |
|________|___________|_________|______|

我将每15分钟运行一个脚本,检查期望运行的任务。在8:36返回以下内容,最有效的显示方式是什么?我需要一种有效的方法来做到这一点的原因是expecteTimes表中有1500行。每隔15分钟就得计算出到达当前时间的次数,这似乎非常耗费资源

| taskID | expected |
|________|__________|
|    1   |   08:30  |
|    2   |   08:30  |
|    3   |   08:25  |
|    3   |   08:35  |
|    4   |   08:25  |
|    4   |   08:30  |
|    4   |   08:35  |

所以我写了这个sql -希望对大家有用。

create table temp
(
  taskID int,
  starttime time,
  EndTime time,
  freq int 
)
insert into temp (taskID,starttime,EndTime,freq)
select 1   ,    '08:00'  ,   '13:45' ,  30  
union all
select 2   ,    '00:00'  ,   '23:59' ,  15  
union all
select 3   ,    '06:35'  ,   '20:20' ,  10  
union all
select 4   ,    '08:00'  ,   '09:00' ,  5   
    Declare @date datetime
    SELECT @date = Getdate() 
    ;with src as (
    SELECT  
    convert(datetime2(0),cast(cast(getdate() as date) as nvarchar)+' '+cast(starttime as nvarchar),102) startDate,
    convert(datetime2(0),cast(cast(getdate() as date) as nvarchar)+' '+cast(Endtime as nvarchar),102) endDate
    ,* FROM    temp
    )
    SELECT 
    case when @date between startDate and endDate then dateadd(minute,(datediff(minute,startdate,getdate())/freq+1)*15,startDate)
    else dateadd(d,1,startDate) end as NextRun,
    *
    FROM    src