从日程表中查找作业的顺序?

时间:2013-06-03 21:00:07

标签: sql oracle plsql

我正在尝试根据存储在Oracle数据库中的表格写出批处理作业计划。

我真的不知道从哪里开始。

该表只有两个相关列:

JOBSTREAM     |   FOLLOW_JOBSTREAM

JOBSTREAM是要运行的作业流的名称。 FOLLOW_JOBSTREAM是在作业流运行之前必须完成的作业流的名称。在某些情况下,一个JOBSTREAM条目有几个FOLLOW_JOBSTREAM条目。

例如:

JS001      JS028
JS001A     JS039
JS001B     JS001A
JS002      JS001A
JS002      JS001B

有没有办法使用PL / SQL对它们进行排序?

谢谢!

3 个答案:

答案 0 :(得分:2)

首先,我们需要来自两个具有层次结构链接的列的完整作业列表:

  select  
    all_jobs.job_id              as jobstream,
    job_depends.follow_jobstream as parent_jobstream
  from
    (
      select jobstream job_id from job_list
      union
      select follow_jobstream job_id from job_list
    ) all_jobs,
    job_list job_depends
  where
    job_depends.jobstream (+) = all_jobs.job_id

然后确定每项工​​作的深度。

with full_job_list as (
  select  
    all_jobs.job_id              as jobstream,
    job_depends.follow_jobstream as parent_jobstream
  from
    (
      select jobstream job_id from job_list
      union
      select follow_jobstream job_id from job_list
    ) all_jobs,
    job_list job_depends
  where
    job_depends.jobstream (+) = all_jobs.job_id
)
select 
  CONNECT_BY_ROOT jobstream            as jobstream, 
  level                                as lvl,
  CONNECT_BY_ROOT parent_jobstream     as preceding_jobstream,
  sys_connect_by_path(jobstream, '<-') as path_illustration
from
  full_job_list
connect by 
  prior parent_jobstream = jobstream

最后根据深度安排工作。相同深度的工作可以按任何顺序工作。

with full_job_list as (
  select  
    all_jobs.job_id              as jobstream,
    job_depends.follow_jobstream as parent_jobstream
  from
    (
      select jobstream job_id from job_list
      union
      select follow_jobstream job_id from job_list
    ) all_jobs,
    job_list job_depends
  where
    job_depends.jobstream (+) = all_jobs.job_id
)
select 
  jobstream, 
  depth,
  preceding_jobstream
from (
  select distinct
    jobstream,
    ( 
      last_value(lvl ignore nulls) 
      over ( partition by jobstream 
             order by lvl 
             rows between unbounded preceding and unbounded following
           ) 
    ) as depth,
    (
      last_value(preceding_jobstream ignore nulls) 
      over ( partition by jobstream 
             order by lvl 
             rows between unbounded preceding and unbounded following
           ) 
    ) as preceding_jobstream
  from (
    select 
      CONNECT_BY_ROOT jobstream        as jobstream, 
      level                            as lvl,
      CONNECT_BY_ROOT parent_jobstream as preceding_jobstream
    from
      full_job_list
    connect by 
      prior parent_jobstream = jobstream
  )
)
order by depth, jobstream

SQL Fiddle

更新已更正以获取完整的作业列表并添加了之前的作业。

Update2 更正了路径方向的错误。

答案 1 :(得分:1)

在这种情况下,

Hierarchical queries可以帮助您:

SELECT JOBSTREAM, FOLLOW_JOBSTREAM
FROM <jobstreams>
START WITH FOLLOW_JOBSTREAM NOT IN (SELECT JOBSTREAM 
                                    FROM jobstreams
                                   )
CONNECT BY PRIOR JOBSTREAM = FOLLOW_JOBSTREAM

SQL Fiddle

答案 2 :(得分:1)

有一种官方的Oracle方法可以按计划将多个作业链接在一起。

DBMS_Scheduler允许您根据链中先前步骤的退出条件定义具有依赖关系的作业链。它可能比本土方法更加灵活和复杂,所以我会考虑将来迁移到它。