Practical application of Oracle scheduled task job
-
- 1. Introduction to Oracle scheduled tasks
- 2. The knowledge points involved in dbms_job
- 3. Initialize related parameters job_queue_processes
- 4. Actually create a scheduled task (executed once a minute) to insert data into the table in one minute
-
- 4.1 Create a target table that needs to insert data regularly
- 4.2 Create a stored procedure for timing execution
- 4.3 Create a scheduled one-minute scheduled task job
- 4.5 You can query the newly created job according to the following statement
- 4.6 If we don't need this scheduled task, how to stop it?
- 4.7 Enable the scheduled task that was just disabled
- 5. Job running time in scheduled tasks
1. Introduction to Oracle scheduled tasks
Oracle timing task is a very important subsystem in the oracle system. Proper use can greatly improve the operation and maintenance capabilities of our system. The function of oracle scheduled tasks can execute tasks by itself at a specified time point.
So in actual work, what kind of scenarios will use timed tasks? The following are examples of real business scenarios used in actual work
- In the production environment, sometimes it is necessary to record some business logs in the table. After the system runs for a long time, there will be more and more logs in the table, resulting in a decrease in system performance. At this time, it is necessary to use a scheduled task to delete the table regularly Some of the time is relatively old garbage data.
- In some business scenarios, the amount of data in the detailed table is particularly large, and if you need to query the detailed table to summarize the data, you need to calculate and summarize the data in the detailed table [the amount of business generated by a certain day, the number of people, etc.] to another table In this way, the query efficiency can be optimized when querying. The above operations need to be performed when the business volume is relatively small [usually after the early morning], which requires the use of scheduled tasks.
2. The knowledge points involved in dbms_job
- create job script
variable jobno number;
dbms_job.submit(:jobno, —-job号
'your_procedure;', —-执行的存储过程, ';'不能省略
next_date, —-下次执行时间
'interval' —-每次间隔时间,
interval 以天为单位);
The above is created by script, of course, it can also be created by plsql graphical tool, the specific creation process is as follows
**The system will automatically assign a task number jobno**, and the following timing task operations can be performed according to the jobno
-
Delete job: dbms_job.remove(jobno);
-
Modify the operation to be performed: job:dbms_job.what(jobno, what);
-
Modify the next execution time: dbms_job.next_date(jobno, next_date);
-
Modify the interval time: dbms_job.interval(jobno, interval);
-
Start job: dbms_job.run(jobno);
-
Stop job: dbms.broken(jobno, broken, nextdate); –broken is a boolean value
3. Initialize related parameters job_queue_processes
- job_queue_process indicates the number of jobs that oracle can run concurrently. When the job_queue_process value is 0, it means that all oracle jobs are stopped
- View the job_queue_processes parameter
show parameter job_queue_process;
或者
select * from v$parameter where name='job_queue_processes';
- Modify the job_queue_processes parameter
alter system set job_queue_processes = 10;
4. Actually create a scheduled task (executed once a minute) to insert data into the table in one minute
4.1 Create a target table that needs to insert data regularly
create table t_test (id varchar2(30),
name varchar2(30)
);
4.2 Create a stored procedure for timing execution
create or replace procedure proce_t is
begin
insert into t_test
(id, name)
values
('1', to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'));
commit;
end proce_t;
/
4.3 Create a scheduled one-minute scheduled task job
variable jobno number;
begin
dbms_job.submit(:jobno,
'proce_t;',
sysdate,
'sysdate+1/24/60');
commit;
end;
4.5 You can query the newly created job according to the following statement
select job, next_date, next_sec, failures, broken from user_jobs where job = '1424'
The query results are as follows
SQL> select job, next_date, next_sec, failures, broken from user_jobs where job = '1424'
2 /
JOB NEXT_DATE NEXT_SEC FAILURES BROKEN
---------- ----------- ---------------- ---------- ------
1424 2020-12-30 13:07:14 0 N
Where broken = N indicates that the job has taken effect
Let's see if there is any scheduled data insertion in the target table
SQL> select * from t_test;
ID NAME
------------------------------ ------------------------------
1 2020-12-30 13:05:14
1 2020-12-30 13:03:14
1 2020-12-30 13:04:14
1 2020-12-30 13:08:14
1 2020-12-30 13:06:14
1 2020-12-30 13:07:14
6 rows selected
It can be seen that a piece of data is inserted every minute.
4.6 If we don't need this scheduled task, how to stop it?
1. According to jobno, execute the following script to stop the job
SQL> begin
2 dbms_job.broken(1424, true, sysdate);
3 commit;
4 end;
5 /
PL/SQL procedure successfully completed
Then check whether the scheduled task is disabled successfully
SQL> select job, next_date, next_sec, failures, broken from user_jobs where job = '1424';
JOB NEXT_DATE NEXT_SEC FAILURES BROKEN
---------- ----------- ---------------- ---------- ------
1424 4000-01-01 00:00:00 0 Y
We found that BROKEN=Y indicates that the scheduled task has stopped successfully
4.7 Enable the scheduled task that was just disabled
SQL> begin
2 dbms_job.run(1424);
3 commit;
4 end;
5 /
PL/SQL procedure successfully completed
SQL> select job, next_date, next_sec, failures, broken from user_jobs where job = '1424';
JOB NEXT_DATE NEXT_SEC FAILURES BROKEN
---------- ----------- ---------------- ---------- ------
1424 2020-12-30 13:20:53 0 N
BROKEN = N , the scheduled task just started again
5. Job running time in scheduled tasks
The following summarizes the running time commonly used in some scheduled tasks
-
Execute every minute: TRUNC(sysdate,'mi') + 1/(24*60)
-
Half an hour: sysdate+30/(24*60)
-
** Execute at 1 am every day: **TRUNC(sysdate) + 1 +1/(24)
-
Execute every Monday at 1 am: TRUNC(next_day(sysdate,'Monday'))+1/24
-
Executed at 1 am on the 1st of each month: TRUNC(LAST_DAY(SYSDATE))+1+1/24
-
Execute at 1 am on the first day of each quarter: TRUNC(ADD_MONTHS(SYSDATE,3),'Q') + 1/24
-
Every July 1st and January 1st at 1 am: ADD_MONTHS(trunc(sysdate,'yyyy'),6)+1/24
-
Execute at 1 am on January 1 every year: ADD_MONTHS(trunc(sysdate,'yyyy'), 12)+1/24
-
** Run at the 15th minute of each hour, for example: 8:15, 9:15, 10:15...: **trunc(sysdate,'hh')+(60+15)/(24*60)