简体   繁体   中英

Generate XML files from Oracle query

I have a query which return 50 millions rows. I want to generate XML files for each row (file max. size is 100k). Of course I know the tags but I don't know how to write this in the most efficient way. Any help?

Thanks

I wouldn't recommend trying to write 50M files to disk, but here's some code you can play with to demonstrate why its not a good idea

1: a function that writes files to disk using a directory

create or replace function WRITETOFILE (dir in VARCHAR2,fn in VARCHAR2, dd IN clob) return clob AS
  ff UTL_FILE.FILE_TYPE;
  l_amt number := 30000;
  l_offset number := 1;
  l_length number := nvl(dbms_lob.getlength(dd),0);
  buf varchar2(30000);
begin   
    ff := UTL_FILE.FOPEN(dir,fn,'W',32760);

    while ( l_offset < l_length ) loop
      buf := dbms_lob.substr(dd,l_amt,l_offset);
      utl_file.put(ff, buf);
      utl_file.fflush(ff);
      utl_file.new_line(ff);
      l_offset := l_offset+length(buf);
    end loop;

    UTL_FILE.FCLOSE(ff);

    return dd; 
END WRITETOFILE;
/

2: statement creating a table with all rows from a query that makes use of function above - suggesting that you keep the number of rows small to see how it plays

create table tmptbl as 
select writetofile('DMP_DIR','xyz-'||level||'.xml', xmlelement("x", level).getClobVal()) tmpcol, systimestamp added_at
from dual CONNECT BY LEVEL <= 10; 

3: drop table to repeat create table statement with more rows

drop table tmptbl purge; 

I did 10k files in 10 seconds - which would give 1000 seconds for 1M files and 50000 seconds for 50M files (ie just under 14 hours).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM