Sie sind auf Seite 1von 32

Automating Tasks with the Scheduler

14
Copyright 2008, Oracle. All rights reserved.

Objectives

%ey Comp& ' Steps Schedules (ob Chains Adv&Concepts

After completing this lesson, you should be able to: Simplify management tasks by using the Scheduler Create a job, program, and schedule onitor job e!ecution "se a time#based or event#based schedule for e!ecuting Scheduler jobs "se job chains to perform a series of related tasks "se advanced Scheduler concepts to prioriti$e jobs

14 - 2

Copyright 2008, Oracle. All rights reserved.

Simplifying

anagement Tasks

)erforming a series of month#end tasks on the last day of each month

*unning a de,ueue procedure as soon as a message is en,ueued

*eplicating table data via materiali$ed view refreshes

*unning a daily job to back up database

Computing table and inde! statistics twice a day

Starting the batch load as soon as the file arrives on the file system

+enerating an hourly report on invalid server access attempts

*ebuilding an inde! when finished rebuilding the current inde!

14 - 3

Copyright 2008, Oracle. All rights reserved.

A Simple (ob
-./0

-.AT

14 - 4

Copyright 2008, Oracle. All rights reserved.

%ey Components and Steps


To simplify management tasks with the Scheduler, perform the following steps: 1& Create a program& 2& Create and use a schedule& 3& Create and submit a job& (ob 4& onitor a job& )rogram Schedule
(ob Arguments

attributes

14 - 5

Copyright 2008, Oracle. All rights reserved.

1& Creating a )rogram

BEGIN DBMS_SCHEDULER.CREATE_PROGRAM( program_name => CALC_STATS! " program_a#$%on => HR.UPDATE_HR_SCHEMA_STATS " program_$&pe => STORED_PROCEDURE " ena'(e) => TRUE*+ END+ ,
14 Copyright 2008, Oracle. All rights reserved.

2& Creating and "sing Schedules

BEGIN DBMS_SCHEDULER.CREATE_SCHEDULE( -#.e)/(e_name => -$a$-_-#.e)/(e " -$ar$_)a$e => S0STIMESTAMP" en)_)a$e => S0STIMESTAMP 1 23" repea$_%n$er4a( => 5RE6=HOURL0+INTER7AL=8 " #ommen$- => E4er& .o/r *+ END+ ,

14 - !

Copyright 2008, Oracle. All rights reserved.

3& Creating and *unning a (ob

14 - 8

Copyright 2008, Oracle. All rights reserved.

4&

onitoring a (ob

SELECT 9o'_name" -$a$/-" error:" r/n_)/ra$%on 5ROM USER_SCHEDULER_;OB_RUN_DETAILS+ ;OB_NAME <<<<<<<<<<<<<<<< GATHER_STATS_;OB STATUS ERROR: RUN_DURATION <<<<<< <<<<<< <<<<<<<<<<<< SUCCESS 3 1333 33=3>=!3 @AB@ 1333 33=33=33

PART_E?CHANGE_;OB 5AILURE

14 - "

Copyright 2008, Oracle. All rights reserved.

"sing a Time#5ased or /vent#5ased Schedule

%ey Comp& ' Steps 6 Schedules (ob Chains Adv&Concepts

Schedule

Time

/vent

14 - 10

Copyright 2008, Oracle. All rights reserved.

Creating a Time#5ased (ob


/!ample: Create a job that calls a backup script every night at 11:77, starting tonight&
BEGIN DBMS_SCHEDULER.CREATE_;OB( 9o'_name=> HR.DO_BACCUP " 9o'_$&pe => E?ECUTABLE " 9o'_a#$%on => ,.ome,/-r,)'a,rman,n%g.$(&_%n#r.-. " -$ar$_)a$e=> S0SDATE" repea$_%n$er4a(=> 5RE6=DAIL0+B0HOUR=!2 " ,D neE$ n%g.$ a$ 88=33 PM D, #ommen$- => N%g.$(& %n#remen$a( 'a#F/p- *+ END+ ,
14 - 11 Copyright 2008, Oracle. All rights reserved.

Creati#g a $i%e-&ased 'o( )*ll +otes ,age

14 - 12

Copyright 2008, Oracle. All rights reserved.

Creating an /vent#5ased Schedule


To create an event#based job, you must set: A ,ueue specification 8where your application en,ueues messages to start a job9 An event condition 8same synta! as an Oracle Streams A: rule condition9 that if TRUE starts the job
Oracle ;atabase 17g

Scheduler /vent A;T 8Abstract ;ata Type9

Application
14 - 13

:ueue

Copyright 2008, Oracle. All rights reserved.

Creating /vent#5ased Schedules with /nterprise anager

14 - 14

Copyright 2008, Oracle. All rights reserved.

Creating an /vent#5ased (ob


/!ample: Create a job that runs if a batch load data file arrives on the file system before <:77 a&m&
BEGIN DBMS_SCHEDULER.CREATE_;OB( 9o'_name=> ADMIN.PER5ORM_DATA_LOAD " 9o'_$&pe => E?ECUTABLE " 9o'_a#$%on => ,.ome,/-r,)'a,rman,repor$_Ga%(/re.-. " -$ar$_)a$e => S0STIMESTAMP" e4en$_#on)%$%on => => $a'./-er_)a$a.o'9e#$_oHner $a'./-er_)a$a.o'9e#$_oHner = = e4en$_#on)%$%on HR an) DATA.T?T HR an) $a'./-er_)a$a.o'9e#$_name $a'./-er_)a$a.o'9e#$_name = = DATA.T?T an) 5ILE_ARRI7AL an) $a'./-er_)a$a.e4en$_$&pe $a'./-er_)a$a.e4en$_$&pe = = 5ILE_ARRI7AL an) an) $a'./-er_)a$a.e4en$_$%me-$amp $a'./-er_)a$a.e4en$_$%me-$amp I IJ J " " K/e/e_-pe# K/e/e_-pe# => => HR.LOAD_;OB_E7ENT_6 HR.LOAD_;OB_E7ENT_6 *+ *+ END+
14 - 15 Copyright 2008, Oracle. All rights reserved.

/vent#5ased Scheduling
/vent types: "ser# or application#generated events Scheduler#generated events /vents raised by Scheduler jobs: ;OB_STARTED JOB_SCH_LIM_REACHED ;OB_SUCCEEDED JOB_DISABLED ;OB_5AILED JOB_CHAIN_STALLED ;OB_BROCEN JOB_ALL_EVENTS ;OB_COMPLETED JOB_RUN_COMPLETED ;OB_STOPPED JOB_OVER_MAX_DUR
/!ample of raising an event: DBMS_SCHEDULER.SET_ATTRIBUTE( .r.)o_'a#F/p " ra%-e_e4en$- " DBMS_SCHEDULER.;OB_5AILED*+
14 - 1 Copyright 2008, Oracle. All rights reserved.

-ve#ts .aised (y the /ched*ler )*ll +otes ,age

14 - 1!

Copyright 2008, Oracle. All rights reserved.

Creating Comple! Schedules

INCLUDE

E?CLUDE

INTERSECT

14 - 18

Copyright 2008, Oracle. All rights reserved.

Creating (ob Chains


1& 2& 3& 4& Create a chain object& ;efine chain steps& ;efine chain rules& Starting the chain:
0 /nable the chain& 0 Create a job that points to the chain&
(ob chain

%ey Comp& ' Steps Schedules 6 (ob Chains Adv&Concepts

(ob

14 - 1"

Copyright 2008, Oracle. All rights reserved.

Creati#g Chai#s )*ll +otes ,age

14 - 20

Copyright 2008, Oracle. All rights reserved.

/!ample of a Chain
;ependency Scheduling
(ob STA*T Loa)_)a$a_e4$ 1 Do_'/(F_(oa)

Schedule

S$op_H.en_ = )%-F_G/((_e4$

Re'/%()_%n)E 3

/0;

R/n_repor$4 8HR.GEN_REPORTS9

BULC_LOAD_CHAIN

14 - 21

Copyright 2008, Oracle. All rights reserved.

1& Creating a Chain Object

Create>job>chain>1&jpg

1 2 3 4 =

14 - 22

Copyright 2008, Oracle. All rights reserved.

2& ;efining Chain Steps


DBMS_SCHEDULER.DE5INE_CHAIN_E7ENT_STEP ( 1 #.a%n_name => '/(F_(oa)_#.a%n " -$ep_name => (oa)_)a$a_e4$ " e4en$_#on)%$%on => $a'./-er_)a$a.o'9e#$_oHner = HR an) $a'./-er_)a$a.o'9e#$_name = DATA.T?T an) $a'./-er_)a$a.e4en$_$&pe = 5ILE_ARRI7AL " K/e/e_-pe# => HR.LOAD_;OB_E7ENT_6 *+ DBMS_SCHEDULER.DE5INE_CHAIN_STEP ( 2 #.a%n_name => '/(F_(oa)_#.a%n " -$ep_name => )o_'/(F_(oa) " program_name => .r.(oa)_)a$a_prog*+ DBMS_SCHEDULER.DE5INE_CHAIN_STEP ( 3 #.a%n_name => '/(F_(oa)_#.a%n " -$ep_name => re'/%()_%n)E " program_name => .r.re'/%()_%n)eEe- *+
14 - 23 Copyright 2008, Oracle. All rights reserved.

3& ;efining Chain *ules

Create>job>chain>2&jpg

14 - 24

Copyright 2008, Oracle. All rights reserved.

4& Starting the Chain

BEGIN DBMS_SCHEDULER.ENABLE ( '/(F_(oa)_#.a%n *+ END+ , BEGIN DBMS_SCHEDULER.CREATE_;OB ( 9o'_name => '/(F_(oa)_#.a%n_9o' " 9o'_$&pe => CHAIN " 9o'_a#$%on => '/(F_(oa)_#.a%n " repea$_%n$er4a( => GreK=)a%(&+'&.o/r=B+ '&m%n/$e=A+'&-e#on)=3 " ena'(e) => TRUE*+ END+ ,

14 - 25

Copyright 2008, Oracle. All rights reserved.

onitoring (ob Chains

LDBA LDBA LDBA LDBA

M M M M

ALL ALL ALL ALL

M M M M

USERN_SCHEDULER_CHAINS USERN_SCHEDULER_CHAIN_RULES USERN_SCHEDULER_CHAIN_STEPS USERN_SCHEDULER_RUNNING_CHAINS

14 - 2

Copyright 2008, Oracle. All rights reserved.

Advanced Scheduler Concepts &

%ey Comp& ' Steps Schedules (ob Chains 6 Adv&Concepts

*esource consumer group

*esource plan

-indow group

(ob chain

(ob class

-indow

)rogram

(ob

Schedule

Arguments

Arguments

Time

/vent

14 - 2!

Copyright 2008, Oracle. All rights reserved.

Creating a (ob Class

E?ECUTE DBMS_SCHEDULER.CREATE_;OB_CLASS( < 9o'_#(a--_name => ADMIN_;OBS " < re-o/r#e_#on-/mer_gro/p => DA0TIME_;OBS " < (ogg%ng_(e4e( => DBMS_SCHEDULER.LOGGING_O55*+
14 - 28 Copyright 2008, Oracle. All rights reserved.

Creating a -indow
Create a window for the month of ;ecember that uses the END_O5_0EAR resource plan and is active every night from ?:77 p&m& to ?:77 a&m& /astern Standard Time 8/ST9&
BEGIN DBMS_SCHEDULER.CREATE_OINDOO( H%n)oH_name => DEC_NIGHTS " re-o/r#e_p(an => END_O5_0EAR " -$ar$_)a$e => 38<DEC<32 3@.33.33 PM EST " repea$_%n$er4a( => 5RE6=DAIL0+ B0HOUR=8> " )/ra$%on => 3 8!=33=33 " en)_)a$e => 28<DEC<32 3@.33.33 AM EST " #ommen$- => E4er& )a& a$ @=33 PM *+ END+ ,
14 - 2" Copyright 2008, Oracle. All rights reserved.

)rioriti$ing (obs -ithin a -indow

'o( (ob1 (ob2 (ob3 OTHER (ob4 (ob= 'o(1 'o(2 'o(3 'o(4 'o(5

,riority 1 2 3 5 2

APPL_;OBS

ADMIN_;OBS ;aytime window

14 - 30

Copyright 2008, Oracle. All rights reserved.

Summary
@n this lesson, you should have learned how to: Simplify management tasks by using the Scheduler Create a job, program, and schedule onitor job e!ecution "se a time#based or event#based schedule for e!ecuting Scheduler jobs "se job chains to perform a series of related tasks "se advanced Scheduler concepts to prioriti$e jobs

14 - 31

Copyright 2008, Oracle. All rights reserved.

)ractice Overview: Automating Tasks with the Scheduler


This practice covers the following topics: Creating a job that runs a program outside the database Creating a program and a schedule Creating a job that uses a program and a schedule Altering the program and schedule for the job and observing the behavior change of the job onitoring job runs

14 - 32

Copyright 2008, Oracle. All rights reserved.