发现临时表空间在不停增长。问题就在于aaa := replace(aaa, 'b', ';;').Replace()函数会先将传入的CLOB参数变量在temp空间上保存一份,然后再转换为varchar2类型进行操作。这样,每次进行了replace操作后,就多占据了一份temp空间。
我们可以通过包dbms_lob中提供的copy、instr和write函数来自己编写一个clob的字符替换函数,来避免temp空间的泄露。以下存储过程实现了CLOB中的字符串替换。
create or replace procedure lob_replace( p_lob in out clob,
p_what in varchar2,
p_with in varchar2 )
as
n number;
len number;
begin
n := dbms_lob.instr( p_lob, p_what );
while ( nvl(n,0) > 0 ) loop
len := dbms_lob.getlength(p_lob);
if (n+length(p_with)-1 > len)
then
dbms_lob.writeappend( p_lob, n+length(p_with)-1 - len, p_with );
end if;
if (len-n-length(p_what)+1 > 0)
then
dbms_lob.copy( p_lob,
p_lob,
len-n-length(p_what)+1,
n+length(p_with),
n+length(p_what) );
end if;
dbms_lob.write( p_lob, length(p_with), n, p_with );
if ( length(p_what) > length(p_with) )
then
dbms_lob.trim( p_lob,
dbms_lob.getlength(p_lob)-(length(p_what)-length(p_with)) );
end if;
n := dbms_lob.instr( p_lob, p_what );
end loop;
end;
p_what in varchar2,
p_with in varchar2 )
as
n number;
len number;
begin
n := dbms_lob.instr( p_lob, p_what );
while ( nvl(n,0) > 0 ) loop
len := dbms_lob.getlength(p_lob);
if (n+length(p_with)-1 > len)
then
dbms_lob.writeappend( p_lob, n+length(p_with)-1 - len, p_with );
end if;
if (len-n-length(p_what)+1 > 0)
then
dbms_lob.copy( p_lob,
p_lob,
len-n-length(p_what)+1,
n+length(p_with),
n+length(p_what) );
end if;
dbms_lob.write( p_lob, length(p_with), n, p_with );
if ( length(p_what) > length(p_with) )
then
dbms_lob.trim( p_lob,
dbms_lob.getlength(p_lob)-(length(p_what)-length(p_with)) );
end if;
n := dbms_lob.instr( p_lob, p_what );
end loop;
end;
用以上过程代替replace函数,再次运行程序,用以上语句观察,不再存在临时空间泄露。