4

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

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

该表只有两个相关列:

JOBSTREAM     |   FOLLOW_JOBSTREAM

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

例如:

JS001      JS028
JS001A     JS039
JS001B     JS001A
JS002      JS001A
JS002      JS001B

有没有办法使用 PL/SQL 对这些进行排序?

谢谢!

4

3 回答 3

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小提琴

更新更正以获得完整的工作列表并添加了先前的工作。

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

于 2013-06-04T05:56:36.453 回答
1

在这种情况下,分层查询可以帮助您:

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

SQL小提琴

于 2013-06-03T22:18:37.783 回答
1

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

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

于 2013-06-03T22:25:23.970 回答