search
HomeDatabaseMysql Tutorialdbms_jobvsOracleScheduler

在oracle 10g之前,我们常使用DBMS_JOB提供的相关软件包来管理oracle数据库中的定时任务,虽然10g之后,更多推荐功能更强大的DBMS_SCHEDULER,但由于习惯问题,很多时候还是会用到DBMS_JOB,下面简单介绍下两种工作的用法。 DBMS_JOB 1,参数JOB_QUEUE_PROCE

在oracle 10g之前,我们常使用DBMS_JOB提供的相关软件包来管理oracle数据库中的定时任务,虽然10g之后,更多推荐功能更强大的DBMS_SCHEDULER,但由于习惯问题,很多时候还是会用到DBMS_JOB,下面简单介绍下两种工作的用法。

DBMS_JOB

1,参数JOB_QUEUE_PROCESSES
oracle已job queue队列的形式管理定时任务,因此需要先设置JOB_QUEUE_PROCESSES参数为非0的数值,一般10即可
ALTER SYSTEM SET JOB_QUEUE_PROCESSES = 10 scope=both;
2, DBMS_JOB用法及例子

SUBMIT:提交任务到队列

VARIABLE jobno NUMBER
BEGIN
   DBMS_JOB.SUBMIT(:jobno,
      'DBMS_DDL.ANALYZE_OBJECT(''TABLE'',''SYS'', ''TEST'',''ESTIMATE'', NULL, 50);',
      SYSDATE, 'SYSDATE + 1');
   COMMIT;
END;
/
sys@MS4ADB3(dtydb5)> PRINT jobno

     JOBNO
----------
         1

查看任务执行情况
sys@MS4ADB3(dtydb5)> select job,what,NEXT_DATE from dba_jobs;

       JOB WHAT                                                                                                 NEXT_DATE
---------- ---------------------------------------------------------------------------------------------------- -------------------
         1 DBMS_DDL.ANALYZE_OBJECT('TABLE','SYS', 'TEST','ESTIMATE', NULL, 50);                                 2014-04-01 14:16:53

有效job的描述例子如下
'myproc(''10-JAN-99'', next_date, broken);'
'scott.emppackage.give_raise(''JFEE'', 3000.00);'
'dbms_job.remove(job);'

常用的job时间间隔
'SYSDATE + 7'
每隔7天

'SYSDATE + 1/48'
每半个小时

'NEXT_DAY(TRUNC(SYSDATE), ''MONDAY'') + 15/24'
每周一下午三点

'NEXT_DAY(ADD_MONTHS(TRUNC(SYSDATE, ''Q''), 3), ''THURSDAY'')'
每季度的第一个周四

REMOVE
从队列中删除
BEGIN
DBMS_JOB.REMOVE(14144);
END;
/
CHANGE
修改任务的属性
BEGIN
DBMS_JOB.CHANGE(14144, NULL, NULL, 'SYSDATE + 3');
END;
/
WHAT
修改任务的描述
BEGIN
DBMS_JOB.WHAT(1,
      'DBMS_DDL.ANALYZE_OBJECT(''TABLE'',''SYS'', ''T2'',''ESTIMATE'', NULL, 50);');
END;
/
NEXT_DATE
任务下一次执行的时间
BEGIN
DBMS_JOB.NEXT_DATE(14144, SYSDATE + 4);
END;
/
INTERVAL
任务执行的时间间隔

BROKEN
终止任务的执行

RUN
运行任务
BEGIN
DBMS_JOB.RUN(1);
END;
/

查看job的相关信息,可以查看视图DBA_JOBS和DBA_JOBS_RUNNING
sys@MS4ADB3(dtydb5)> SELECT JOB, NEXT_DATE, NEXT_SEC, FAILURES, BROKEN
  2     FROM DBA_JOBS;

       JOB NEXT_DATE           NEXT_SEC                                                           FAILURES BR
---------- ------------------- ---------------------------------------------------------------- ---------- --
         1 2014-04-01 14:39:39 14:39:39                                                                  0 N    


例外,jobs运行失败的日志在alert log里面。

DBMS_SCHEDULER

DBMS_SCHEDULER功能更强大,具有如下优点

Logging of job runs (job history) --job运行的日志
Simple but powerful scheduling syntax (similar to but more powerful than cron syntax) --更简单强大的语法
Running of jobs outside of the database on the operating system (see below)--运行os命令
Resource management between different classes of jobs--资源管理功能
Use of job arguments including passing of objects into stored procedures--使用任务参数
Privilege-based security model for jobs -- 更好的权限控制
Naming of jobs and comments in jobs --job命名和描述
Stored, reusable schedules --存储和重用存储过程

--常见用法
BEGIN
  DBMS_SCHEDULER.create_job (
    job_name        => 'test_full_job_definition',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN DBMS_STATS.gather_schema_stats(''WJ''); END;',
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=hourly; byminute=0',
    end_date        => NULL,
    enabled         => TRUE,
    comments        => 'Job defined entirely by the CREATE JOB procedure.');
END;

运行os命令的例子
BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
     job_name           => 'my_OS_job',
     job_type           => 'EXECUTABLE',
     job_action         => 'ls /tmp',
     repeat_interval    => 'FREQ=MINUTELY',
     enabled            => TRUE
  );
END;

--运行一个job
EXEC dbms_scheduler.run_job('TEST_FULL_JOB_DEFINITION');

SELECT * FROM dba_scheduler_jobs WHERE job_name = 'TEST_FULL_JOB_DEFINITION';

REPEAT_INTERVAL的例子
'FREQ=DAILY; BYDAY=SUN; BYHOUR=18;',
 
'freq=MINUTELY;interval=1',  
 
'freq=MINUTELY;interval=5',  
 
'freq=HOURLY;interval=1',    
 
'FREQ=DAILY; BYDAY=MON,TUE,WED,THU,FRI,SAT,SUN; BYHOUR=22;',

更多关于dbms_scheduler的用法可以常见官方文档

http://docs.oracle.com/cd/E11882_01/server.112/e25494/scheduse.htm#ADMIN13380

参考文档:

http://docs.oracle.com/cd/B10501_01/server.920/a96521/jobq.htm#9499
http://www.mandsconsulting.com/oracle-dbms_scheduler-vs-dbms_job-create-run-monitor-remove
http://docs.oracle.com/cd/E11882_01/server.112/e25494/scheduse.htm#ADMIN13380

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor