对临时表做insert插入时没有做批量提交,可能会导致undo表空间撑爆,修改脚本对插数进行2万一次的批量提交,并且修改索引和同义词创建时间在插数操作结束后。
原语句:
insert into 目标表 select * from 源表;
改为2w次一提交:
DECLARE
CURSOR data_cursor IS
SELECT *
FROM 源表;
TYPE data_array IS TABLE OF data_cursor%ROWTYPE;
data data_array;
BEGIN
OPEN data_cursor;
LOOP
FETCH data_cursor BULK COLLECT INTO data LIMIT 20000; -- 每次获取2万条数据
FORALL i IN 1..data.COUNT
INSERT INTO 目标表
VALUES data(i);
COMMIT; -- 每次提交
EXIT WHEN data_cursor%NOTFOUND;
END LOOP;
CLOSE data_cursor;
END;
/
删除目标表数据(2022-12-31 23:59:59之前的表数据)
DECLARE
COMMIT_INTERVAL NUMBER := 10000; -- 每次提交的记录数
COMMIT_COUNT NUMBER := 0;
BEGIN
FOR rec IN (SELECT * FROM 目标表 WHERE createtime < '2022-12-31 23:59:59' ) LOOP
DELETE FROM ER_VOUCHER WHERE id = rec.id;
COMMIT_COUNT := COMMIT_COUNT + 1;
-- 每次删除一万条记录后提交
IF COMMIT_COUNT >= COMMIT_INTERVAL THEN
COMMIT;
COMMIT_COUNT := 0;
END IF;
END LOOP;
-- 提交剩余的记录
COMMIT;
END;