默认truncate操作下,Oracle会删除所有数据,回收所有段结构后重新分配一个新的extent。内部的段结构上,Oracle认为是在原来段头位置上重新分配的新段。
3、Truncate reuse storage行为
下面来测试一下reuse storage参数行为。首先需要重建表数据内容和充实段结构。
SQL> insert into t select * from dba_objects;
99693 rows inserted
SQL> commit;
Commit complete
SQL> select EXTENT_ID, FILE_ID, BLOCK_ID, BYTES, BLOCKS from dba_extents where segment_name='T' and owner='TEST';
EXTENT_ID FILE_ID BLOCK_ID BYTES BLOCKS
---------- ---------- ---------- ---------- ----------
0 4 28808 65536 8
1 4 28816 65536 8
(篇幅原因,有省略……)
26 4 30720 1048576 128
27 rows selected
SQL> select EXTENT_ID, FILE_ID, BLOCK_ID, BYTES, BLOCKS from dba_extents where segment_name='IDX_T_ID' and owner='TEST';
EXTENT_ID FILE_ID BLOCK_ID BYTES BLOCKS
---------- ---------- ---------- ---------- ----------
0 4 28936 65536 8
1 4 28840 65536 8
(篇幅原因,有省略……)
17 4 30208 1048576 128
18 rows selected
操作reuse storage。
SQL> truncate table t reuse storage;
Table truncated
SQL> select EXTENT_ID, FILE_ID, BLOCK_ID, BYTES, BLOCKS from dba_extents where segment_name='T' and owner='TEST';
EXTENT_ID FILE_ID BLOCK_ID BYTES BLOCKS
---------- ---------- ---------- ---------- ----------
0 4 28808 65536 8
1 4 28816 65536 8
(篇幅原因,有省略……)
26 4 30720 1048576 128
27 rows selected
SQL> select EXTENT_ID, FILE_ID, BLOCK_ID, BYTES, BLOCKS from dba_extents where segment_name='IDX_T_ID' and owner='TEST';
EXTENT_ID FILE_ID BLOCK_ID BYTES BLOCKS
---------- ---------- ---------- ---------- ----------
0 4 28936 65536 8
1 4 28840 65536 8
2 4 28904 65536 8
(篇幅原因,有省略……)
17 4 30208 1048576 128
18 rows selected
数据的确删除。
SQL> select count(*) from t;
COUNT(*)
----------
0
Reuse storage情况下,段结构没有回收,数据却被删除了!从段结构情况看,Oracle依然视之为新段,data_object_id发生变化。
SQL> select object_name, object_id, data_object_id from dba_objects where owner='TEST' and object_name in ('T','IDX_T_ID');
OBJECT_NAME OBJECT_ID DATA_OBJECT_ID
--------------- ---------- --------------
IDX_T_ID 123667 123670
T 123666 123671