Sunteți pe pagina 1din 41

5 FEATURES YOU OUGHT TO KNOW ABOUT THE ORACLE SCHEDULER

Eddie Awad
awads.net @eddieawad

Imagine you could tell your Oracle Database to:


Run jobs on the last workday of every month, excluding

company holidays. Control how much CPU each of your running jobs can get. Run Operating System commands from inside the database. Run a job only after a successful completion of other jobs.

Use The Oracle Scheduler


By using the Oracle Scheduler, a free utility included in your Oracle database, you will be able to do all of this and much more.

What is the Oracle Scheduler?


Free job scheduling utility that is included in your Oracle

database 10g and above. Manipulated through the provided PL/SQL package DBMS_SCHEDULER.

Why not DBMS_JOB?


DBMS_SCHEDULER superseded DBMS_JOB starting

with Oracle 10gR1. Oracle recommends using DBMS_SCHEDULER. DBMS_SCHEDULER is more robust and has many features that DBMS_JOB does not have.

What about cron?


Cannot make the execution of a job dependent

on the completion of another job. Does not have robust resource balancing and flexible scheduling features. Cannot run jobs based on a database event. Does not have a syntax that works the same regardless of the operating system. Cannot run status reports using the data dictionary.

5 features you ought to know about the Oracle Scheduler


1. 2. 3. 4. 5.

Calendaring syntax Resource manager Job chains External jobs Event-based jobs

A simple job
BEGIN sys.DBMS_SCHEDULER.create_job ( job_name => 'MY_JOB', job_type => 'PLSQL_BLOCK', job_action => 'dbms_lock.sleep(30);', start_date => SYSTIMESTAMP AT TIME ZONE 'US/Pacific', end_date => NULL, repeat_interval => NULL, job_class => 'DEFAULT_JOB_CLASS', comments => 'This is a sample job.', auto_drop => FALSE, enabled => TRUE); END; /

The Calendar
Has a very powerful calendaring syntax. Used in the repeat_interval attribute. Results in a set of timestamps.

Calendaring Syntax Examples


FREQ=DAILY; BYDAY=MON,TUE,WED,THU,FRI;

BYHOUR=22; BYMINUTE=0; BYSECOND=0;


Run at 10:00 pm daily from Monday to Friday.

Calendaring Syntax Examples


FREQ=HOURLY;INTERVAL=1; Run every hour. FREQ=MINUTELY;INTERVAL=5; Run every 5 minutes. FREQ=YEARLY; BYWEEKNO=5,10,15; BYDAY=MON Run on Monday of weeks 5, 10 and 15 every year.

Calendaring Syntax Examples


FREQ=WEEKLY; INTERVAL=2; BYDAY=FRI; Run every other Friday. FREQ=MONTHLY; BYMONTHDAY=-1 Run on the last day of every month. FREQ=MONTHLY; BYMONTHDAY=-2 Run on the next to last day of every month.

Calendaring Syntax Examples


FREQ=MONTHLY; BYDAY=2WED Run on the second Wednesday of each month. FREQ=YEARLY; BYDAY=-1FRI Run on the last Friday of the year. FREQ=HOURLY; BYMONTHDAY=1,2,3 Run hourly for the first three days of every month.

Calendaring Syntax Examples


FREQ=YEARLY; BYYEARDAY=60,120,180 Run on the 60th, 120th and 180th days of the year. FREQ=MONTHLY; BYDAY=MON,TUE,WED,THU,FRI;

BYSETPOS=-1
Run on the last workday of every month, assuming that workdays

are Monday through Friday.

Calendaring Syntax Examples


DBMS_SCHEDULER.create_schedule ( schedule_name => 'COMPANY_HOLIDAYS', repeat_interval => 'FREQ=YEARLY; BYDATE=0530,0704,0905,1124,1125,1225;');
FREQ=MONTHLY; BYDAY=MON,TUE,WED,THU,FRI;

EXCLUDE=COMPANY_HOLIDAYS; BYSETPOS=-1
Run on the last workday of every month, excluding company

holidays.

FREQ=YEARLY; BYDAY=FRI;BYHOUR=12;

INCLUDE=COMPANY_HOLIDAYS

Run at noon every Friday and on company holidays.

Calendaring Syntax Examples


DBMS_SCHEDULER.create_schedule ( schedule_name => 'LAST_SAT', repeat_interval => 'FREQ=MONTHLY;BYDAY=SAT;BYSETPOS=-1;'); DBMS_SCHEDULER.create_schedule ( schedule_name => 'END_QTR', repeat_interval => 'FREQ=YEARLY;BYDATE=0331,0630,0930,1231;');
FREQ=MONTHLY; BYMONTHDAY=-1;

INTERSECT=LAST_SAT,END_QTR
quarter

Run on the last day of the month that is either a Saturday or last day of a

Calendaring Syntax Examples


DBMS_SCHEDULER.create_schedule ( schedule_name => 'FISCAL_YEAR', repeat_interval => 'FREQ=YEARLY; BYDATE=0301,0601,0901,1201; PERIODS=4;');
FREQ=FISCAL_YEAR;BYDAY=-1WED Run on the last Wednesday of every quarter of the fiscal year. FREQ=FISCAL_YEAR;

BYDAY=MON,TUE,WED,THU,FRI; BYPERIOD=2,4; BYSETPOS=-1


Run on the last work day of the 2nd and 4th quarters of the fiscal

year (assuming that workdays are Monday through Friday).

Calendar String Evaluation


DBMS_SCHEDULER.evaluate_calendar_string
Evaluates the calendar expression without having to actually

schedule the job. Tells you what the next execution date and time of a job will be.

DEMO

The Resource Manager


Allocate percentages of CPU time to different users. Limit the degree of parallelism. Create an active session pool. Prevent the execution of long running operations. Limit the amount of time that a session can be idle.

Elements of the Resource Manager


Resource consumer group. Resource plan. Resource plan directive.

The Scheduler and the Resource Manager


1. 2. 3. 4.

Create a resource plan and consumer groups. Create a Scheduler Window and associate it with the resource plan. Create Scheduler job classes and associate each with a consumer group. Create Scheduler jobs and associate each with a Scheduler job class.

The Scheduler and the Resource Manager - Example

The Scheduler and the Resource Manager - Example

DEMO

Chains
A job chain is the means by which you can

implement dependency based scheduling, in which jobs are started depending on the outcomes of one or more previous jobs. A job chain consists of multiple steps. A job chain uses rules to describe the relationship between these steps.

Create a Chain
1. 2. 3. 4. 5. 6.

Create Scheduler program objects for each step of the chain (If steps point to programs). Create a chain object. Define the steps in the chain. Define the rules for the chain. Enable the chain. Create a job (the "chain job") that points to the chain.

Create a Chain - Example

Create a Chain - Example

DEMO

External Jobs
Local external job Its executable runs on the same computer as the database that schedules the job. Remote external jobs (New in 11g) Its executable runs on a remote host. The remote host does not need to have an Oracle database. Must install a Scheduler agent on the remote host and register it with the local database. Use a Scheduler credential object (New in 11g)

for authentication with the OS.

External Jobs
Default Authentication when Credentials are not Present
Unix 10.2.0.2+: As the user and group set in $ORACLE_HOME/rdbms/admin/externaljob.ora. Prior to 10.2.0.2: As the owner and group of the file $ORACLE_HOME/bin/extjob. Windows As the user that the OracleJobScheduler Windows service runs as. Unix and Windows, all releases External jobs in the SYS schema run as the user who installed the Oracle Database.

DEMO

Event-Based Jobs
A job can raise one or more pre-defined events. A job can be triggered based on real-time events.

Raising Events
Instruct a job to raise one or more events by setting its

raise_events attribute. The Scheduler raises these events by enqueuing messages onto the Scheduler event queue SYS.SCHEDULER$_EVENT_QUEUE

Raising Events
job_all_events job_broken job_chain_stalled job_completed job_disabled job_failed job_over_max_dur job_run_completed job_sch_lim_reached job_started job_stopped job_succeeded

Starting a Job Based on an Event


A job started based on an event is called an event-based

job. To create an event-based job, you must set two attributes:


queue_spec event_condition

Starting a Job Based on an Event

queue_spec:
includes the name of the queue where your application enqueues messages to raise job start events or in the case of a secure queue, the queue name followed by a comma and the agent name.

Starting a Job Based on an Event

event_condition:
A conditional expression based on message properties that must evaluate to TRUE for the message to start the job. The expression must have the syntax of an Oracle Streams Advanced Queuing rule.

DEMO

Resources
PL/SQL Packages and Types Reference (11.2) - DBMS_SCHEDULER: http://download.oracle.com/docs/cd/E11882_01/appdev.112/e16760/d_sched.htm Administrator's Guide (11.2) - Scheduling Jobs with Oracle Scheduler: http://download.oracle.com/docs/cd/E11882_01/server.112/e17120/scheduse.htm Oracle Scheduler Forum: http://forums.oracle.com/forums/forum.jspa?forumID=193 Book: Mastering Oracle Scheduler in Oracle 11g Databases: http://www.amazon.com/Mastering-Oracle-Scheduler-11g-Databases/dp/1847195989

S-ar putea să vă placă și