我有下面的存储过程,这基本上是一个基于视图刷新日期清理表的存储过程现在我关心的是我的存储过程工作正常但是作为 一个开发者我更专注于选择,而不是使用不正确的synta,
所以请求大家,请告知并指导我是否在下面存储的程序中使用任何不正确的语法或如何优化他在下面存储的程序,请告知我,因为我更倾向于优化和效率请告知。
create or replace procedure TABLE_CLEAN_UPABLE_CLEAN_UP
is
v_last_cleanup_time timestamp(6);
v_last_mv_refresh_time timestamp(6);
v_last_mv0_refresh_time timestamp(6);
outcome VARCHAR2(100);
i NUMBER := 0;
begin
Select LAST_START_DATE+LAST_RUN_DURATION into v_last_cleanup_time from USER_SCHEDULER_JOBS
where JOB_NAME='DELTA_TABLE_CLEAN_UP_JOB';
SELECT count(*) into i FROM all_mviews WHERE owner = 'XP_TM_TO' AND
mview_name in ('CASHFLOW_VIEW', 'CASHFLOW_VIEW_0') and LAST_REFRESH_TYPE='COMPLETE'
and CAST(LAST_REFRESH_DATE AS TIMESTAMP) > v_last_cleanup_time;
if i=2 then
SELECT CAST(LAST_REFRESH_DATE AS TIMESTAMP) into v_last_mv_refresh_time FROM all_mviews WHERE owner = 'XP_TM_TO' AND mview_name = 'CASHFLOW_VIEW' and LAST_REFRESH_TYPE='COMPLETE';
SELECT CAST(LAST_REFRESH_DATE AS TIMESTAMP) into v_last_mv0_refresh_time FROM all_mviews WHERE owner = 'XP_TM_TO' AND mview_name = 'CASHFLOW_VIEW_0' and LAST_REFRESH_TYPE='COMPLETE';
delete CASHFLOW_DELTA where (TRADE_ID,SOURCE_SYSTEM,TRADE_VERSION)
NOT in
(select table1.TRADE_ID,table1.SOURCE_SYSTEM,table1.TRADE_VERSION from CASHFLOW_DELTA table1
INNER JOIN (select TRADE_ID,SOURCE_SYSTEM,max(TRADE_VERSION) as MAX_TRADE_VERSION
from CASHFLOW_DELTA GROUP BY TRADE_ID, SOURCE_SYSTEM) table2
on table1.TRADE_ID=table2.TRADE_ID
where table1.SOURCE_SYSTEM=table2.SOURCE_SYSTEM
and table1.TRADE_VERSION=table2.MAX_TRADE_VERSION
and table1.BROKERAGE_REALISED='YES')
and createddate<v_last_mv_refresh_time
and createddate<v_last_mv0_refresh_time;
end if;
end;
/
答案 0 :(得分:0)
CREATE OR REPLACE PROCEDURE TABLE_CLEAN_UPABLE_CLEAN_UP
IS
v_last_mv_refresh_time TIMESTAMP (6);
i NUMBER := 0;
BEGIN
SELECT MIN(CAST (LAST_REFRESH_DATE AS TIMESTAMP)), COUNT (*)
INTO v_last_mv_refresh_time,i
FROM all_mviews
WHERE owner = 'XP_TM_TO'
AND mview_name IN ('CASHFLOW_VIEW', 'CASHFLOW_VIEW_0')
AND LAST_REFRESH_TYPE = 'COMPLETE'
AND CAST (LAST_REFRESH_DATE AS TIMESTAMP) > (SELECT LAST_START_DATE + LAST_RUN_DURATION FROM USER_SCHEDULER_JOBS WHERE JOB_NAME = 'DELTA_TABLE_CLEAN_UP_JOB');
IF i = 2 THEN
DELETE CASHFLOW_DELTA
WHERE ((TRADE_ID, SOURCE_SYSTEM, TRADE_VERSION) NOT IN ( SELECT TRADE_ID, SOURCE_SYSTEM, MAX (TRADE_VERSION) AS MAX_TRADE_VERSION
FROM CASHFLOW_DELTA
GROUP BY TRADE_ID, SOURCE_SYSTEM)
OR NVL(BROKERAGE_REALISED,'NO') <> 'YES')
AND createddate < v_last_mv_refresh_time;
END IF;
END;
/