dbms_jobs vs. dbms_scheduler_jobs
dba_jobs:This view lists all jobs inthe database.
dba_scheduler_jobs:Job scheduler job detail.
http://www.dba-oracle.com/t_dbms_job_scheduler.htm
I don't think you answered the question What's the difference between these two views?
The answer is, There's no relationship.
Traditional DBMS jobs (as we often call) are shown in dba_jobs.
10g new type of scheduler jobs are shown in dba_scheduler_jobs.
These are two types of jobs, and Oracle suggests we all use the new scheduler jobs
because you can have more and better control and monitoring.
If you schedule something to run using both dbms_jobs and dbms_scheduler_jobs packages,
it will be run according to both schedulers and they're independent ofeach other.
Of course it's silly to schedule the same thing twice.
--sys用户执行以下sql语句,查看job的运行日志
select t.owner,
t.job_name,
t.status,
to_char(t.actual_start_date, 'yyyy-mm-dd hh24:mi:ss'),
t.additional_info,
t.error#
from dba_scheduler_job_run_details t
where t.owner = 'PSD'
order by log_date desc;
SELECT job_name, job_name,
avg(EXTRACT( DAY FROM run_duration )*24*60*60 + EXTRACT( HOUR FROM run_duration )*60*60 + EXTRACT( MINUTE FROM run_duration )*60 + EXTRACT( SECOND FROM run_duration ))
FROM dba_scheduler_job_run_details
GROUP BY job_name, job_name
HAVING avg(EXTRACT( DAY FROM run_duration )*24*60*60 + EXTRACT( HOUR FROM run_duration )*60*60 + EXTRACT( MINUTE FROM run_duration )*60 + EXTRACT( SECOND FROM run_duration )) > 0
ORDER BY 3 DESC;
Starting with Oracle Database 10g, the Oracle scheduler was greatly improved with the dbms_scheduler package. Replacing the dbms_job with dbms_scheduler offers additional features by adding the ability to tie jobs with specific user-type privileges and roles:
|
See from Dr. Timothy Hall's book "Oracle Job Scheduling" these tips on migrating from dbms_job to dbms_scheduler:
|
|
The new 10g job scheduling views
Oracle MOSC also offers advice on moving from dbms_job to dbms_scheduler and notes that the dba_jobs view is obsolete with dbms_scheduler and we use dba_scheduler_jobs:
select job_name, enabled from user_scheduler_jobs;
and this:
select job_id, freq_type, freq_interval, freq_subday_type, freq_subday_interval, freq_relative_interval, freq_recurrence_factor, active_start_date, active_end_date, active_start_time, active_end_time, schedule_id from dba_scheduler_jobs;
MOSC also notes that internally-scheduled tasks (job queues, automatic statistics) can be seen by querying the dba_scheduler_jobs view:
As the SYS user, use the following query to check for this job:
SELECT STATE FROM DBA_SCHEDULER_JOBS WHERE JOB_NAME = 'GATHER_STATS_JOB';
This DBASupport article shows the main DBA Scheduler views and a handy query:
-
DBA_SCHEDULER_SCHEDULES - provides me with information about the schedules that are in effect in the database.
-
DBA_SCHEDULER_PROGRAMS - shows all program objects and their attributes, while view DBA_SCHEDULER_PROGRAM_ARGS shows all program arguments for programs that have them.
-
DBA_SCHEDULER_JOBS - shows all job objects and their attributes.
http://docs.oracle.com/cd/E11882_01/server.112/e25494/schedadmin.htm#ADMIN12035 Setting Oracle Scheduler Privileges
You must have the SCHEDULER_ADMIN role to perform all Oracle Scheduler administration tasks. Typically, database administrators already have this role with the ADMIN option as part of the DBA role. For example, users SYS and SYSTEM are granted the DBA role. You can grant this role to another administrator by issuing the following statement: GRANT SCHEDULER_ADMIN TO username;
Because the SCHEDULER_ADMIN role is a powerful role allowing a grantee to execute code as any user, you should consider granting individual Scheduler system privileges instead. Object and system privileges are granted using regular SQL grant syntax. An example is if the database administrator issues the following statement: GRANT CREATE JOB TO scott;
After this statement is executed, scott can create jobs, schedules, programs, file watchers, and credentials in his schema. Another example is if the database administrator issues the following statement: GRANT MANAGE SCHEDULER TO adam;
After this statement is executed, adam can create, alter, or drop windows, job classes, or window groups. He will also be able to set and retrieve Scheduler attributes and purge Scheduler logs.
--dbms_job interval
例如:
declare
JOB_ILEARN_ONLINE number :=1;
begin
dbms_job.submit(JOB_ILEARN_ONLINE,'insert into testdate(today) values(sysdate);',sysdate,'TRUNC(sysdate,''mi'') + 1 / (24*60) ');
commit;
end;
1、每分钟执行
Interval => TRUNC(sysdate, 'mi')+1/(24*60)
2、每天定时执行
例如:每天的凌晨2点执行
Interval => TRUNC(sysdate)+1+2/(24)
3、每周定时执行
例如:每周一凌晨2点执行
Interval => TRUNC(next_day(sysdate, 2))+2/24 --星期一,一周的第二天
4、每月定时执行
例如:每月1日凌晨2点执行
Interval => TRUNC(LAST_DAY(SYSDATE))+1+2/24
5、每季度定时执行
例如每季度的第一天凌晨2点执行
Interval => TRUNC(ADD_MONTH(SYSDATE), 3),'Q')+2/24
6、每半年定时执行
例如:每年7月1日和1月1日凌晨2点
Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),6)+2/24
7、每年定时执行
例如:每年1月1日凌晨2点执行
Interval => ADD_MONTHS(trunc(sysdate,'yyyy'),12)+2/24