[英]FORALL insert destination table then a FORALL delete from source table
Cam the below be done without issues? Cam下面没有问题吗? Where as soon as you insert a record into the destination table you can then delete that record from the source table.
只要您将记录插入目标表,就可以从源表中删除该记录。 Obviously this record is in memory within the loop, can you see any problems with this or can it be done in a different way.
显然这条记录在循环内存中,你能看到任何问题,还是可以用不同的方式完成。
I know all of you will say just do a direct SQL insert with APPEND and then truncate the source table. 我知道你们所有人都会说用APPEND做一个直接的SQL插入,然后截断源表。
I'm just throwing the question out there as I'm curious. 因为我很好奇,我只是把问题扔出去了。
PROCEDURE copy_records_back IS
TYPE t_act_plus_triggers_copy1 IS TABLE OF act_plus_triggers_copy1%ROWTYPE; v_act_plus_triggers_copy1 t_act_plus_triggers_copy1;
CURSOR c_act_plus_triggers_copy1 IS SELECT * FROM act_plus_triggers_copy;
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE act_plus_triggers1';
OPEN c_act_plus_triggers_copy1; LOOP
FETCH c_act_plus_triggers_copy1 BULK COLLECT INTO v_act_plus_triggers_copy LIMIT 10000;
FORALL i IN 1..v_act_plus_triggers_copy.COUNT
INSERT /*+ APPEND_VALUES */ INTO act_plus_triggers1 values v_act_plus_triggers_copy(i);
FORALL i IN 1..v_act_plus_triggers_copy.COUNT
DELETE FROM act_plus_triggers_copy
where surr_id = v_act_plus_triggers_copy(i).surr_id
COMMIT;
EXIT WHEN c_act_plus_triggers_copy1%NOTFOUND;
END LOOP;
CLOSE c_act_plus_triggers_copy1;
END copy_records_back;
If the two tables have the same columns, there is a simpler method to do this without copying the data at all. 如果两个表具有相同的列,则有一种更简单的方法可以完成此操作而无需复制数据。 You can create a partitioned table with the same columns as both tables and with only one partition.
您可以使用与两个表相同的列创建分区表,并且只创建一个分区。 Then you can use
ALTER TABLE EXCHANGE PARTITION
to swap the table with the partition, then you can swap in the same way the partition with the target table (if one of the tables is partitioned, you only need to do one swap). 然后,您可以使用
ALTER TABLE EXCHANGE PARTITION
将表与分区交换,然后您可以以与目标表相同的方式交换分区(如果其中一个表已分区,则只需要进行一次交换)。 If the data need to change tablespace (or some other physical properties), you can issue ALTER TABLE MOVE
at the end to accomplish this task. 如果数据需要更改表空间(或其他一些物理属性),则可以在最后发出
ALTER TABLE MOVE
来完成此任务。
In your situation the statements for swapping two tables will look like: 在您的情况下,交换两个表的语句如下所示:
-- initialization (do only once)
CREATE TABLE temp_part_table PARTITION BY HASH (surr_id) (PARTITION single) AS
SELECT * FROM act_plus_triggers_copy WHERE 1=0
/
-- swapping act_plus_triggers_copy with act_plus_triggers1
ALTER TABLE temp_part_table EXCHANGE PARTITION single WITH TABLE act_plus_triggers1
WITHOUT VALIDATION
/
ALTER TABLE temp_part_table EXCHANGE PARTITION single WITH TABLE act_plus_triggers_copy
WITHOUT VALIDATION
/
ALTER TABLE temp_part_table EXCHANGE PARTITION single WITH TABLE act_plus_triggers1
WITHOUT VALIDATION
/
There are three exchanges if you want to swap both tables. 如果要交换两个表,则有三个交换。 If you know that both
temp_part_table
(temporary partitioned table) and act_plus_triggers1
(target table) are initially empty, you can skip the first ALTER
. 如果您知道
temp_part_table
(临时分区表)和act_plus_triggers1
(目标表)最初都为空,则可以跳过第一个ALTER
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.