oracle Lob对象空间回收测试

备注:转自网络

SQL> create table t(time date,text clob);
SQL> begin
for i in 1 .. 10000 loop
insert into t values(sysdate,rpad('a', 4000, 'a'));
commit;
end loop;
end;
/

SQL> select count(*) from t; --10000

SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 83886080
SQL> select sum(dbms_lob.getlength(text)) as bytes from t; --40000000
SQL> delete from t;
SQL> commit;
SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 83886080
SQL> select sum(dbms_lob.getlength(text)) as bytes from t; --输出为null
可以看见删掉以后,空间没有释放,再次想表中插入10000跳记录。

SQL> begin
for i in 1 .. 10000 loop
insert into t values(sysdate,rpad('a', 4000, 'a'));
commit;
end loop;
end;
/

SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 167772160

SQL> select sum(dbms_lob.getlength(text)) as bytes from t; --40000000
SQL> delete from t;
SQL> commit;

TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 167772160
SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
SQL> select sum(dbms_lob.getlength(text)) as bytes from t; --输出为null
----------空间依然没有释放,下面释放空间。
SQL> alter table t modify lob (text) (shrink space);
SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 65536
SQL> select sum(dbms_lob.getlength(text)) as bytes from t; --输出为null

SQL> begin
for i in 1 .. 10000 loop
insert into t values(sysdate,rpad('a', 4000, 'a'));
commit;
end loop;
end;
/

SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 83886080
SQL> truncate table t;
SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 65536
可以看见truncate会释放空间。


SQL> begin
for i in 1 .. 10000 loop
insert into t values(sysdate,rpad('a', 4000, 'a'));
commit;
end loop;
end;
/

SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 83886080
SQL> delete from t;
SQL> commit;
SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 83886080
SQL> alter table t move lob(text) store as(tablespace tab1);
SQL> select table_name, column_name, segment_name, a.bytes from dba_segments a join dba_lobs b using (owner, segment_name) where b.table_name = 'T';
TABLE_NAME COLUMN_NAME SEGMENT_NAME BYTES
------------------------------ -------------------- ------------------------------ ----------
T TEXT SYS_LOB0000102100C00002$$ 65536
move表空间也会释放空间。

 

posted @ 2017-06-12 10:06  zfox  阅读(915)  评论(0编辑  收藏  举报