0
我需要更新名爲table_to_update
的表中名爲text
的列。字段text
是來自另一個表other_table
的3個字符串的連接。該腳本按預期工作,但問題是執行(> 20小時)需要很長時間,因爲表table_to_update
中需要更新的表中有大約200萬個數據集。如何優化這個PL/SQL塊?
有什麼想法可以改善這個腳本的性能嗎?
DECLARE
v_field1 VARCHAR(20);
v_field2 VARCHAR(20);
v_field3 VARCHAR(20);
v_text VARCHAR(100);
BEGIN
FOR rec IN (select t_pk from table_to_update where state = 'STATE_1' and text is null order by t_pk desc) -- 20 millions data sets
LOOP
v_text := null;
FOR other_record IN (select distinct field1, field1, field3 from other_table where t_fk = rec.t_pk)
LOOP
v_field1 := other_record.field1;
v_field2 := other_record.field2;
v_field3 := other_record.field3;
v_text := v_text || v_field2 || ';' || v_field1 || ': '|| v_field3 || ' ';
END LOOP;
update table_to_update set text = v_text where t_pk = rec.t_pk;
END LOOP;
COMMIT;
END;
這是否必須是PL/SQL?爲什麼不使用原始SQL和子查詢來處理字符串操作? – mmmmmpie 2015-03-19 13:18:18
@mmmmmpie:內部循環聚合了連接的字符串,因此它看起來不那麼簡單。 – Allan 2015-03-19 13:29:41
正確的是,當您回答時,我正在輸入有關「MERGE」語句的註釋。 :) – mmmmmpie 2015-03-19 13:30:13