Skip to Main Content
  • Questions
  • Performance of temporary clobs vs table clobs

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Andrew.

Asked: April 22, 2010 - 7:03 pm UTC

Last updated: May 06, 2010 - 1:42 pm UTC

Version: 10.2.0

Viewed 1000+ times

You Asked

Hi Tom, a real pleasure to be able to submit a question to you...

My original question was going to be why is "method B" below so slow? Then I found a "method A" and was going to ask which is faster:

A) create a temporary clob, put data in it, then insert the clob into a table,
or
B) insert a clob into a table, open it, then write data into it

but then I did my own homework and came up with my own test cases and realized that of course method A is much faster, so now my questions are:

1) if I had these clob operations from method B in a procedure in between a lot of SQL statements, and noticed slow execution times, how would I be able to quickly diagnose that the clob operations (and not the SQL) are the cause of the problem? i.e. These slow clob operations don't show up in a trace & tkprof report, and don't even seem to show up in one of the profiler tools I use either.

2) why is method B so much slower? is there a way to enable cached writing to the clob so it is faster too?

setup:
create table temp_clob (id number, text clob);


method A:
declare 
  lc clob; n number := 1;
begin
  dbms_lob.CREATETEMPORARY(lc,TRUE);

  for rec in (select s.text from dba_source s where rownum <= 1000) loop
    dbms_lob.writeappend(lc,length(rec.text),rec.text);
  end loop;
  INSERT INTO temp_clob VALUES (n, lc);
  dbms_lob.FREETEMPORARY(lc);   
end;

This took only 0.187 seconds to run. Woohoo!


method B:
declare 
  lc clob; n number := 1;
begin
   EXECUTE IMMEDIATE 'INSERT INTO temp_clob VALUES (:i1, empty_clob()) RETURNING text INTO :o1'
   using IN n, OUT lc;
   
   dbms_lob.open(lc, DBMS_LOB.lob_readwrite);

  for rec in (select s.text from dba_source s where rownum <= 1000) loop
    dbms_lob.writeappend(lc,length(rec.text),rec.text);
  end loop;
   dbms_lob.close(lc);
end;

This took a whopping 24 seconds to run!

and Tom said...

I do not see the same personally, however, I can say that working on the item in temp will in general work in memory (until it gets large then temp), without redo and without using direct path IO.


by default, your table clob is NOCACHE, every read, every write is a direct IO (there were lots of direct read/write waits in the method B, not so in method A).

lobs are versioned in the lob segment - not undo. So, each modification you made had to version itself in the lob segment using direct IO as well.

By doing it in "temp", you did it mostly in memory - without physical IO.

Probably, if you created your table:

create table temp_clob( n number, text clob ) lob (text) store as ( cache );


you would see more "similar" results - but doing the thing "in memory" and then putting it into the table would make sense for something like this.


Just like if I was to use utl_file to read lines and write them out - I would find it to be faster to build up a big string with as much data as I could and call utl_file.fwrite less often....

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Store as cache helps a little

Andrew C, May 04, 2010 - 4:38 pm UTC

Thanks, the "store as (cache)" clause helps a little. It improved the poor-performing method by 75%, bringing it down from 24 seconds to about 6 seconds, but still far short of the temp clob method.

I would be interested to know, if I had a slow-running application and had no idea what was slowing it down, what statistics would I look at that would tell me that it was the clobs that were so slow?
Tom Kyte
May 06, 2010 - 1:42 pm UTC

you could use the profiler if it was in plsql code, that would help isolate it

also, you would see (or expect to see) IO waits if it was the cache/nocache situation.


and a 7 year old server I have did it in a lot less than six seconds either way (I do not observe a large difference) - what kind of machine are you using?

and remember, temp is pga not sga and temp would be in general non-latched access since you "own it", as opposed to buffer cache.

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here