如何优化For Each with Select语句处理Oracle PL/SQL中的数百万行

oiopk7p5  于 2023-03-29  发布在  Oracle
关注(0)|答案(1)|浏览(130)

我有一个CSV文件,其中有2,000,000条记录插入到一个表(blob列)中。我试图创建以下块:

DECLARE
        v_doc          BLOB;
        v_file_content CLOB;
        zip_files      as_zip.file_list;
 BEGIN
    --Get file corresponding to process in progress
        SELECT
            file_content
        INTO v_doc
        FROM
            int_dat_journals_cv027_files
        WHERE
                file_name = 'in_gl_journals_20230322101527_v3.zip' -->This value is dynamic
            AND id_proceso_oic = '1'; -->This value is dynamic

        --Get Files from .zip file
        zip_files := as_zip.get_file_list(v_doc);
        
        --Iterate each file from .zip file
        FOR i IN zip_files.first()..zip_files.last LOOP
            --convert blob file to clob
            SELECT
                blob_to_clob_fn(as_zip.get_file(v_doc, zip_files(i)))
            INTO v_file_content
            FROM
                dual;
            
            --dbms_output.put_line(v_file_content);
            --iterate and split line by line by chr(10), for example:'a,b,c,,,f,g'
            FOR linea IN (
                SELECT
                    TRIM(column_value) l
                FROM
                    dual,
                    xmltable ( ( '"'
                                 || replace(v_file_content, CHR(10), '","')
                                 || '"' ) )
            ) LOOP
                dbms_output.put_line(linea.l);
            END LOOP;

        END LOOP;

    EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('ERROR: ' || sqlerrm);
            dbms_output.put_line(dbms_utility.format_error_backtrace);
    END;

我需要特别优化这个部分,以便能够处理数百万条记录:
FOR line IN(select TRIM(column_value)l DESDE dual,xmltable(('"'||replace(v_file_content,CHR(10),'",“')||“))LOOP dbms_output.put_line(line.l);末端回路;
如何进行优化?

bn31dyow

bn31dyow1#

你不需要这样做。你可以尝试使用csv作为外部表。或者,如果你想索引它,或者有效地使用它,你必须复制数据到另一个表。
如果文件位置在此处,则为csv_dir/yourfile.csv

drop table csv_test_table;

CREATE TABLE csv_test_table (
  COLUMN1 varchar2(255),
  COLUMN2 varchar2(255),
  COLUMN3 varchar2(255),
  COLUMN4 varchar2(255)

)
ORGANIZATION EXTERNAL (
  DEFAULT DIRECTORY csv_dir
  ACCESS PARAMETERS (
    records delimited BY newline
    skip 1
    fields 
    terminated BY ';' optionally enclosed BY '"' --or use ',' here
    lrtrim
    missing field VALUES are NULL
  )
  LOCATION ('yourfile.csv')  
);

相关问题