一点一点学习jobs的各个方面比较长,比较烦,但是应该看完后会对jobs比较好的应用
一、学习准备
开始dbms_job学习前,先认识一个参数job_queue_processes
a、job_queue_processes参数决定了job作业能够使用的总进程数。b、当该参数为0值,任何job都不会被执行,建议合理设置该值且至少大于1。c、对于job运行时间也应该尽量合理的设置间隔以及启动时间。d、如果同一时间内运行的Job数很多,过小的参数值导致job不得不进行等待。而过大的参数值则消耗更多的系统资源。f、对于存在依赖关系的job,尽可能将其进行合并到一个job中,如使用chain等。想细致了解的请前往在使用dbms_jobs时首先确认job_queue_processes的值(10g系统默认为10)SQL> SHOW PARAMETER JOB_QUEUE_PROCESSES;NAME TYPE VALUE------------------------------------ ----------- ------------------------------job_queue_processes integer 10
另外10g开始提供了一个新的job管理包:DBMS_SCHEDULER
http://space.itpub.net/8858072/viewspace-425547http://www.cnblogs.com/lanzi/archive/2012/11/23/2784815.html二、创建第一个jobSQL> create table a(a date);Table created.SQL> create or replace procedure job_test as 2 begin 3 insert into a values(sysdate); 4 end; 5 /Procedure created.SQL> exec job_test;PL/SQL procedure successfully completed.SQL> select * from a;A-------------------2013-05-31 15:45:32
SQL>variable job1 number;--注意别漏了定义变量SQL> begin 2 dbms_job.submit(:job1,'job_test;',sysdate,'sysdate+1/64800'); 3 commit; 4 end; 5 /PL/SQL procedure successfully completed
variable job1 number;beginsys.dbms_job.submit(job => :job1,what => 'job_test;',next_date => sysdate,interval => 'sysdate+1/64800');--每天64800秒,即每秒执行一次,但是实际oracle的jobs不能精确到秒,这里只是为了快速测试效果commit;end;
SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs; JOB N_DATE WHAT INTERVAL B---------- ------------------- ---------- -------------------- - 4 2013-05-31 15:49:24 job_test; sysdate+1/64800 NSQL> select count(*) from a; COUNT(*)---------- 9SQL> / COUNT(*)---------- 10SQL> / COUNT(*)---------- 10SQL> select * from a;A-------------------2013-05-31 15:45:322013-05-31 15:48:582013-05-31 15:49:032013-05-31 15:49:082013-05-31 15:49:132013-05-31 15:49:182013-05-31 15:49:232013-05-31 15:49:282013-05-31 15:49:332013-05-31 15:49:382013-05-31 15:49:4311 rows selected.
SQL> exec dbms_job.change(4,null,null,'sysdate+1/1440');PL/SQL procedure successfully completed.SQL> commit; --一定要记得commit否则重启后会丢失Commit complete.SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs; JOB N_DATE WHAT INTERVAL B---------- ------------------- ---------- -------------------- - 4 2013-05-31 15:57:08 job_test; sysdate+1/1440 NSQL> truncate table a;Table truncated.SQL> select * from a;no rows selectedSQL> select * from a;A-------------------2013-05-31 15:57:08SQL> /A-------------------2013-05-31 15:57:082013-05-31 15:58:082013-05-31 15:59:082013-05-31 16:00:082013-05-31 16:01:08
修改成功
但是有瑕疵,08秒,假如我想精确,可以通过oracle的trunc函数(最后实验结果告诉我trunc可以输出00秒,但是jobs不能精确到秒)SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';Session altered.SQL> select sysdate from dual;SYSDATE-------------------2013-05-31 16:02:25SQL> select trunc(sysdate,'mi') from dual;TRUNC(SYSDATE,'MI')-------------------2013-05-31 16:02:00
SQL> exec dbms_job.change(4,null,null,'trunc(sysdate,''mi'')+1/1440');PL/SQL procedure successfully completed.SQL> commit;SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs; JOB N_DATE WHAT INTERVAL B---------- ------------------- ---------- -------------------- - 4 2013-05-31 16:12:00 job_test; trunc(sysdate,'mi')+ N 1/1440 SQL> select * from a;.....A-------------------2013-05-31 16:08:092013-05-31 16:09:092013-05-31 16:10:092013-05-31 16:11:092013-05-31 16:12:042013-05-31 16:13:042013-05-31 16:14:042013-05-31 16:15:042013-05-31 16:16:042013-05-31 16:17:042013-05-31 16:18:04
SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs; JOB N_DATE WHAT INTERVAL B---------- ------------------- ---------- -------------------- - 4 2013-05-31 16:12:00 job_test; trunc(sysdate,'mi')+ N 1/1440SQL> exec dbms_job.broken(4,true);PL/SQL procedure successfully completed.SQL> commit;Commit complete.SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs; JOB N_DATE WHAT INTERVAL B---------- ------------------- ---------- -------------------- - 4 4000-01-01 00:00:00 job_test; trunc(sysdate,'mi')+ Y 1/1440
SQL> exec dbms_job.broken(4,false);PL/SQL procedure successfully completed.SQL> commit;Commit complete.SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs; JOB N_DATE WHAT INTERVAL B---------- ------------------- ---------- -------------------- - 4 2013-05-31 16:36:24 job_test; trunc(sysdate,'mi')+ N 1/1440
SQL> exec dbms_job.remove(4);PL/SQL procedure successfully completed.SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs;no rows selectedSQL> rollback;--不commit咱rollback看看Rollback complete.SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs; JOB N_DATE WHAT INTERVAL B---------- ------------------- ---------- -------------------- - 4 2013-05-31 16:36:24 job_test; trunc(sysdate,'mi')+ N 1/1440--jobs又回来了SQL> exec dbms_job.remove(4);PL/SQL procedure successfully completed.SQL> commit;Commit complete.SQL> select job,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') as n_date,what,interval,broken from user_jobs;no rows selected--commit后就真的没了
1)SQL> select trunc(sysdate) from dual ; --返回当前日期,精确到凌晨0点0分TRUNC(SYSDATE)-------------------2013-06-04 00:00:002)SQL> select trunc(sysdate, 'mm') from dual;--mm格式为返回当月第一天TRUNC(SYSDATE,'MM')-------------------2013-06-01 00:00:00 3)SQL> select trunc(sysdate,'yy') from dual;--yy格式,返回当年第一天TRUNC(SYSDATE,'YY')-------------------2013-01-01 00:00:004)SQL> select trunc(sysdate,'dd') from dual; --dd返回当前年月日,与trunc(sysdate)等效TRUNC(SYSDATE,'DD')-------------------2013-06-04 00:00:005)SQL> select trunc(sysdate,'yyyy') from dual; --yy跟yy一样,返回当年第一天TRUNC(SYSDATE,'YYYY-------------------2013-01-01 00:00:006)SQL> select trunc(sysdate,'d') from dual; --d返回本周第一天,按老美的,第一天是周日TRUNC(SYSDATE,'D')-------------------2013-06-02 00:00:007)SQL> select trunc(sysdate, 'hh') from dual ; --hh返回当前时间,精确到TRUNC(SYSDATE,'HH')-------------------2013-06-04 16:00:008)SQL> select trunc(sysdate, 'mi') from dual; --mi返回当前时间精确到分钟,TRUNC()函数没有秒的精确TRUNC(SYSDATE,'MI')-------------------2013-06-04 16:29:00
SQL> select last_day('2013-10-5') from dual;LAST_DAY('2013-10-5-------------------2013-10-31 00:00:00SQL> select last_day(sysdate) from dual;LAST_DAY(SYSDATE)-------------------2013-06-30 18:16:30想要下个月的3号SQL> select last_day(sysdate)+3 from dual;LAST_DAY(SYSDATE)+3-------------------2013-07-03 18:17:26再组合下truncSQL> select trunc(last_day(sysdate)+3) from dual;TRUNC(LAST_DAY(SYSD-------------------2013-07-03 00:00:00再组合一个小时分钟SQL> select trunc(last_day(sysdate)+3)+3/24+30/1440 from dual;TRUNC(LAST_DAY(SYSD-------------------2013-07-03 03:30:00
n/24表示小时
n/1440表示分钟n/64800表示秒6.3 next_day返回下一个星期某一天next_day(date,n)想要每周3早上3点SQL> select next_day(sysdate,4) from dual;--按老美的每周第一天是周日,所以周三是4NEXT_DAY(SYSDATE,4)-------------------2013-06-05 18:22:59
SQL> select trunc(next_day(sysdate,4))+3/24+30/1440 from dual;TRUNC(NEXT_DAY(SYSD-------------------2013-06-05 03:30:00
SQL> select trunc(add_months(sysdate,2))+3/24+30/1440 from dual;TRUNC(ADD_MONTHS(SY-------------------2013-08-04 03:30:00SQL> select trunc(add_months(trunc(sysdate,'yy'),2))+3/24+30/1440 from dual;TRUNC(ADD_MONTHS(TR-------------------2013-03-01 03:30:00
也可以简单点具体到每2个月那一天
SQL> select trunc(add_months('2013-01-03',2))+3/24+30/1440 from dual;TRUNC(ADD_MONTHS('2-------------------2013-03-03 03:30:00
SQL> variable job1 number;SQL> begin 2 dbms_job.submit(:job1,'job_test;',sysdate,'trunc(last_day(sysdate)+3)+3/24+30/1440'); 3 commit; 4 end; 5 /PL/SQL procedure successfully completed.SQL> set linesize 200SQL> col job format 999SQL> col what format a50SQL> col interval format a50SQL> col next_date format a50SQL> select job,what,interval,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') from user_jobs; JOB WHAT INTERVAL TO_CHAR(NEXT_DATE,'---- -------------------------------------------------- -------------------------------------------------- ------------------- 8 job_test; trunc(last_day(sysdate)+3)+3/24+30/1440 2013-07-03 03:30:00
SQL> variable job1 number;SQL> begin 2 dbms_job.submit(:job1,'job_test;',sysdate,'trunc(next_day(sysdate,4))+3/24+30/1440'); 3 commit;end; 4 5 /PL/SQL procedure successfully completed.SQL>SQL> select job,what,interval,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') from user_jobs; JOB WHAT INTERVAL TO_CHAR(NEXT_DATE,'---- -------------------------------------------------- -------------------------------------------------- ------------------- 8 job_test; trunc(last_day(sysdate)+3)+3/24+30/1440 2013-07-03 03:30:00 9 job_test; trunc(next_day(sysdate,4))+3/24+30/1440 2013-06-05 03:30:00
SQL> variable job1 number;SQL> begin 2 dbms_job.submit(:job1,'job_test;',sysdate,'trunc(sysdate+1)+3/24'); 3 commit; 4 end; 5 /PL/SQL procedure successfully completed.SQL> select job,what,interval,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') from user_jobs; JOB WHAT INTERVAL TO_CHAR(NEXT_DATE,'---- -------------------------------------------------- -------------------------------------------------- ------------------- 10 job_test; trunc(sysdate+1)+3/24 2013-06-05 03:00:00 8 job_test; trunc(last_day(sysdate)+3)+3/24+30/1440 2013-07-03 03:30:00 9 job_test; trunc(next_day(sysdate,4))+3/24+30/1440 2013-06-05 03:30:00
variable job1 number;begindbms_job.submit(:job1,'job_test;',sysdate,'to_date(to_char(add_months(sysdate,1),''yyyymm'')||''03 10:00:00'',''yyyy-mm-dd hh24:mi:ss'')');commit;end;/
dbms_job.submit(:job1,'job_test;',sysdate,'sysdate+1/24');
每十分钟
dbms_job.submit(:job1,'job_test;',sysdate,'sysdate+10/1440');
最后到指定某个特定时间点
variable job1 number;begindbms_job.submit(:job1,'job_test;',to_date('2013-06-04 20:00:00','YYYY-MM-DD HH24:MI:SS'),'');commit;end;/SQL> select job,what,interval,to_char(next_date,'yyyy-mm-dd hh24:mi:ss') from user_jobs; JOB WHAT INTERVAL TO_CHAR(NEXT_DATE,'---- -------------------------------------------------- -------------------------------------------------- ------------------- 12 job_test; null 2013-06-04 20:00:00
DBMS_JOB.CHANGE(JOB IN BINARY_INTEGER, --job号WHAT IN VARCHAR2, --执行什么NEXT_DATE, --下一次执行时间INTERVAL IN VARCHAR2, --执行间隔INSTANCE IN BINARY_INTEGER DEFAULT NULL,FORCE IN BOOLEAN DEFAULT FALSE);
所以
exec dbms_job.change(4,null,null,'sysdate+1/1440'); __| |__ |___ |____________ | | | | job号 what next_date interval commit;
PROCEDURE BROKEN( JOB IN BINARY_INTEGER, BROKENIN BOOLEAN, NEXT_DATE IN DATE := SYSDATE)
2、change()过程:用来改变指定工作的设置
DBMS_JOB.CHANGE(JOB IN BINARY_INTEGER,WHAT IN VARCHAR2,NEXT_DATE,INTERVAL IN VARCHAR2,INSTANCE IN BINARY_INTEGER DEFAULT NULL,FORCE IN BOOLEAN DEFAULT FALSE);
PROCEDURE INTERVAL( JOB IN BINARY_INTEGER, INTERVAL IN VARCHAR2)
4、Isubmit()过程:用来用特定的工作号提交一个工作
PROCEDURE ISUBMIT( JOB IN BINARY_INEGER, WHAT IN VARCHAR2, NEXT_DATE IN DATE, INTERVAL IN VARCHAR2, NO_PARSE IN BOOEAN := FALSE)
5、Next_Date()过程:用来显式地设定一个工作的执行时间
PROCEDURE NEXT_DATE( JOB IN BINARY_INEGER, NEXT_DATE IN DATE)
6、Remove()过程:来删除一个已计划运行的工作
PROCEDURE REMOVE(JOB IN BINARY_INEGER);
7、Run()过程:用来立即执行一个指定的工作
PROCEDURE RUN(JOB IN BINARY_INEGER)
8、Submit()过程:工作被正常地计划好
PROCEDURE SUBMIT( JOB OUT BINARY_INEGER, WHAT IN VARCHAR2, NEXT_DATE IN DATE, INTERVAL IN VARCHAR2, NO_PARSE IN BOOEAN := FALSE)
9、User_Export()过程:返回一个命令,此命令用来安排一个存在的工作以便此工作能重新提交。
PROCEDURE USER_EXPORT( JOB IN BINARY_INEGER, MY_CALL IN OUT VARCHAR2)
PROCEDURE WHAT( JOB IN BINARY_INEGER, WHAT IN OUT VARCHAR2)
其实...可以通过desc获得:
很多的dbms工具包都可以通过desc查看
SQL> desc dbms_job;FUNCTION BACKGROUND_PROCESS RETURNS BOOLEANPROCEDURE BROKEN Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN BROKEN BOOLEAN IN NEXT_DATE DATE IN DEFAULTPROCEDURE CHANGE Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN WHAT VARCHAR2 IN NEXT_DATE DATE IN INTERVAL VARCHAR2 IN INSTANCE BINARY_INTEGER IN DEFAULT FORCE BOOLEAN IN DEFAULTPROCEDURE INSTANCE Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN INSTANCE BINARY_INTEGER IN FORCE BOOLEAN IN DEFAULTPROCEDURE INTERVAL Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN INTERVAL VARCHAR2 INPROCEDURE ISUBMIT Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN WHAT VARCHAR2 IN NEXT_DATE DATE IN INTERVAL VARCHAR2 IN DEFAULT NO_PARSE BOOLEAN IN DEFAULTFUNCTION IS_JOBQ RETURNS BOOLEANPROCEDURE NEXT_DATE Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN NEXT_DATE DATE INPROCEDURE REMOVE Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER INPROCEDURE RUN Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN FORCE BOOLEAN IN DEFAULTPROCEDURE SUBMIT Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER OUT WHAT VARCHAR2 IN NEXT_DATE DATE IN DEFAULT INTERVAL VARCHAR2 IN DEFAULT NO_PARSE BOOLEAN IN DEFAULT INSTANCE BINARY_INTEGER IN DEFAULT FORCE BOOLEAN IN DEFAULTPROCEDURE USER_EXPORT Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN MYCALL VARCHAR2 IN/OUTPROCEDURE USER_EXPORT Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN MYCALL VARCHAR2 IN/OUT MYINST VARCHAR2 IN/OUTPROCEDURE WHAT Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- JOB BINARY_INTEGER IN WHAT VARCHAR2 IN