Oracle 11g Lightweight Jobs(原创)

Overview Lightweight Job

In Oracle Database 10g, there was only a single type of Scheduler job. In Oracle Database 11g, you can also create what’s called a lightweight job (also referred to as a persistent lightweight job), which derives its privileges, and in some cases, its job metadata, from a job template. Regular jobs, which are fully self-contained unlike the lightweight jobs, are still the more flexible type of jobs, but you incur overhead each time you create one. In a situation in which the database may have to create and delete thousands of jobs, it may be smarter to use a lightweight job instead. If you’re going to use a small number of infrequent jobs, you are better off sticking to the traditional regular jobs. Lightweight jobs aren’t schema objects, as regular Scheduler jobs are. Thus, the overhead in creating and dropping a lightweight job is much smaller when compared with a regular job. Lightweight jobs are also faster to create and take up space only for the job metadata and runtime data. Lightweight jobs help make recovery and load balancing easier in an RAC environment because they have a smaller physical footprint and require less redo because of the minimal amount of metadata that’s created for the jobs. The overall goal is to reduce the time it takes to create jobs and to lower the overhead involved in the job creation process.

Unlike a regular Scheduler job, you must use a job template when creating a lightweight job. The job template is a new type of database object that provides the metadata for a lightweight job, in addition to providing privileges that the lightweight jobs will inherit. Either a stored procedure or a Scheduler program can serve as a template for lightweight job. The lightweight jobs thus inherit their privileges from the parent job template. Although they offer superior overhead cost, lightweight jobs have the following disadvantages when compared to a regular job:

  • You can’t create a fully self-contained lightweight job. Instead, you must use a template. You can use either a stored procedure or a Scheduler program as a template for a lightweight job.
  • You can’t set the privileges on a per job basis because the lightweight jobs inherit privileges from the parent job template.
  • Only a limited set of job attributes are applicable to lightweight jobs, whereas a regular job offers more choices.

To summarize, then, lightweight jobs are a special type of Scheduler jobs that you can use instead of traditional jobs if your database has to quickly create a large number of jobs.

Creating Lightweight Job

Create Job template

You can create a job template, which is mandatory for lightweight jobs, by using the CREATE_PROGRAM procedure from the DBMS_SCHEDULER package
BEGIN
    DBMS_SCHEDULER.create_program(
        program_name => 'test_prog',
        program_action =>
              'declare
                    current_time date;
                begin
                   select sysdate into current_time from dual;
                   dbms_output.put_line(current_time);
                end;',
        program_type => 'plsql_block',
        enabled   => true);
END;
/

The program_type attribute lets you specify the type of program you are creating.  For a lightweight job, you can use either plsql_block or stored_procedure as the value for the program_type attribute. You specify the plsql_block value for an anonymous PL/SQL block, which is the case in this example. You specify the value stored_procedure for a program that’s a PL/SQL or Java stored procedure or an external C subprogram.

Note that a lightweight job always needs a job template, which is based on a procedure or a Scheduler program. Unlike a regular job, you can’t inline a lightweight job, but must always use a named program. The program test_prog in our example serves as the template for this lightweight job.

Creating a Single Lightweight Job

For a lightweight job, you must use the new job parameter job_style and assign it a value of LIGHTWEIGHT. You can create a lightweight job by specifying the time and frequency directly within the CREATE_JOB procedure, or use a schedule to set the timing and frequency attributes for the job. You can specify only a few parameters for a lightweight job. These are the job parameters and the schedule parameter. The lightweight jobs inherit the other metadata for running the job, as well as its privileges, from the parent job template. Following is a examplt to create the lightweight job:

BEGIN
    DBMS_SCHEDULER.create_job(
        job_name          => 'test_ltwtjob1',
        program_name      => 'test_prog',
        repeat_interval   => 'FREQ=daily;BYHOUR=10',
        end_date          => '31-DEC-08 06:00:00 AM Australia/Sydney',
        comments          => 'A lightweight job');
END;
/

You can also create a lightweight job based on a named program (which acts as the template) and a preexisting schedule, as shown in this example:
BEGIN
    DBMS_SCHEDULER.create_job(
        job_name          => 'test_ltwtjob2',
        program_name      => 'test_prog',
        schedule_name     => 'DAILY_PURGE_SCHEDULE',
        job_style         => 'lightweight',
        comments          => 'A lightweight job based on program adn schedule');
END;
/

Creating an Array of Lightweight Jobs

Oracle Database 11g offers you a way to create a set of jobs through the creation of a job array. When you need to create a fairly large number of Scheduler jobs (regular or lightweight), it’s far more efficient to create a job array and submit it once, instead of submitting a large number of single job creation requests to the database. You can use the concept of a job array for both regular jobs and the new lightweight jobs. To submit the job array as a single transaction, we can use the CREATE_JOBS procedure. In the following example,I'll show you the efficient of lightweight job and job array.

DECLARE
    testjob           sys.job;
    testjobarr        sys.job_array;
    l_start           NUMBER;
BEGIN
    testjobarr    := sys.job_array();
    testjobarr.extend(10000);
   
    l_start := DBMS_UTILITY.get_time;
   
    FOR I in 1 .. 5000 loop
        testjob := sys.job(job_name => 'TETJOB'    || TO_CHAR(I),
        job_style     => 'LIGHTWEIGHT',
        job_template  => 'TEST_PROG',
        enabled       => TRUE);
        testjobarr(i) := TESTJOB;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Lightweight Array(hsec): ' || (DBMS_UTILITY.get_time - l_start));
    l_start := DBMS_UTILITY.get_time;
    FOR I in 5001 .. 10000 loop
        testjob := sys.job(job_name => 'TETJOB'    || TO_CHAR(I),
        job_style     => 'REGULAR',
        job_template  => 'TEST_PROG',
        enabled       => TRUE);
        testjobarr(i) := TESTJOB;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Regular Array(hsec): ' || (DBMS_UTILITY.get_time - l_start));
    l_start  := DBMS_UTILITY.get_time;
    FOR I IN 1 .. 100 LOOP
        DBMS_SCHEDULER.create_job(
            job_name        => 'lightweight_job_' || i,
            program_name    => 'lightweight_program',
            job_style       => 'LIGHTWEIGHT',
            enabled         => TRUE);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Lightweight(hsecs): ' || (DBMS_UTILITY.get_time - l_start));
    l_start  := DBMS_UTILITY.get_time;
    FOR I IN 1 .. 100 LOOP
        DBMS_SCHEDULER.create_job(
            job_name        => 'regular_job_' || i,
            program_name    => 'lightweight_program',
            job_style       => 'REGULAR',
            enabled         => TRUE);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Regular(hsecs): ' || (DBMS_UTILITY.get_time - l_start));

    dbms_scheduler.create_jobs (testjobarr, 'transactional');
END;
/

To drop a job you can use following command:

BEGIN
   DBMS_SCHEDULER.DROP_JOB('myjob1');
END;
/

Use the same views that you use for regular jobs to view information about lightweight jobs. For example, you can query the DBA_SCHEDULER_JOBS view to find out details about lightweight jobs, as shown here:

select job_name,program_name,job_style,job_action
from dba_scheduler_jobs

Note that you can’t view any lightweight jobs in the DBA_OBJECTS view because, unlike a regular Scheduler job, lightweight jobs aren’t database objects.

 

参考至:《McGraw.Hill.OCP.Oracle.Database.11g.New.Features.for.Administrators.Exam.Guide.Apr.2008》

                 http://www.oracle-base.com/articles/11g/scheduler-enhancements-11gr1.php

                 http://docs.oracle.com/cd/E11882_01/server.112/e25494/schedover.htm#CHDECJDB

                 http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_sched.htm#BABCIJJA

                 http://docs.oracle.com/cd/E11882_01/server.112/e25494/appendix_a.htm#adminAppendixA

本文原创,转载请注明出处、作者

如有错误,欢迎指正

邮箱:[email protected]

猜你喜欢

转载自czmmiao.iteye.com/blog/1953440
今日推荐