Accurate statistics are essential for Oracle database performance, enabling the query optimizer to create efficient execution plans. For a table like test, which may experience frequent data changes, automating statistics collection ensures consistent query performance without manual effort. This article provides a corrected PL/SQL script to create an Oracle Scheduler job for gathering statistics on the test table, along with practical guidance for implementation and maintenance.
Importance of Automating Statistics for the test Table
Statistics describe the data distribution in tables and indexes, directly influencing Oracle’s query optimizer. For the test table, frequent updates or inserts can make statistics outdated, leading to inefficient query plans and slower performance. Automating statistics collection offers:
Using Oracle Scheduler with DBMS_STATS, DBAs can streamline this process for the test table.
PL/SQL Script: Automating Statistics for the test Table
The following PL/SQL script creates a scheduler job to collect statistics for the test table weekly, optimized for performance and reliability.
Corrected PL/SQL Script
sql
BEGIN
-- Create the scheduler job
SYS.DBMS_SCHEDULER.CREATE_JOB(
job_name => 'BUGRA.JOB_GATHER_TEST_STATS',
start_date => TO_TIMESTAMP_TZ('2025/06/06 23:00:00.000000 Europe/Istanbul', 'YYYY/MM/DD HH24:MI:SS.FF TZR'),
repeat_interval => 'FREQ=WEEKLY;BYDAY=FRI;BYHOUR=23;BYMINUTE=0;BYSECOND=0',
end_date => NULL,
job_class => 'DEFAULT_JOB_CLASS',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => ''BUGRA'', tabname => ''test'', degree => 8, granularity => ''AUTO'', cascade => TRUE); END;',
comments => 'Weekly statistics collection for the test table'
);
-- Set job attributes
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE(
name => 'BUGRA.JOB_GATHER_TEST_STATS',
attribute => 'RESTARTABLE',
value => TRUE
);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE(
name => 'BUGRA.JOB_GATHER_TEST_STATS',
attribute => 'LOGGING_LEVEL',
value => DBMS_SCHEDULER.LOGGING_FULL
);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE_NULL(
name => 'BUGRA.JOB_GATHER_TEST_STATS',
attribute => 'MAX_FAILURES'
);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE_NULL(
name => 'BUGRA.JOB_GATHER_TEST_STATS',
attribute => 'MAX_RUNS'
);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE(
name => 'BUGRA.JOB_GATHER_TEST_STATS',
attribute => 'STOP_ON_WINDOW_CLOSE',
value => FALSE
);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE(
name => 'BUGRA.JOB_GATHER_TEST_STATS',
attribute => 'JOB_PRIORITY',
value => 3
);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE_NULL(
name => 'BUGRA.JOB_GATHER_TEST_STATS',
attribute => 'SCHEDULE_LIMIT'
);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE(
name => 'BUGRA.JOB_GATHER_TEST_STATS',
attribute => 'AUTO_DROP',
value => FALSE
);
-- Enable the job
SYS.DBMS_SCHEDULER.ENABLE(
name => 'BUGRA.JOB_GATHER_TEST_STATS'
);
END;
/
Key Corrections and Improvements
How to Use the Script
Best Practices for Statistics Automation
Conclusion
Automating statistics collection for the test table using Oracle Scheduler and DBMS_STATS ensures optimal query performance with minimal manual effort. The provided PL/SQL script offers a reliable solution for weekly statistics updates, tailored to the test table’s needs. By implementing this automation and following best practices, DBAs can maintain efficient query execution and reduce performance bottlenecks.
Additional Notes
Tags: