Oracle Big Table cleanup truncate... reuse storage
Recently, a large table needs to be cleaned up, which requires no impact on performance. In MySQL, we can minimize I/O by using coreutils and hard links. Oracle can also minimize I/O by recycling space in batches. What is the problem.
1. truncate table syntax
Truncate table [schema_name.] table_name
[Preserve materialized view log | purge materialized view log]
[Drop storage | reuse storage]; 1
-- Only the description of reuse storage is listed below
REUSE STORAGE
Specify reuse storage to retain the space from the deleted rows allocated to the table. storage values are not reset to the values when the table was created. this space can subsequently be used only by new data in the table resulting from insert or update operations. this clause leaves storage parameters at their current settings.
This setting is useful as an alternative to deleting all rows of a very large table-when the number of rows is very large, the table entails contains thousands of extents, and when data is to be reinserted in the future. truncate table with reuse storage performs several orders of magn1_faster than deleting all rows, but has the following drawbacks:
• You cannot roll back a truncate table statement.
• All cursors are invalidated.
• You cannot flash back to the state of the table before the truncate operation.
This clause is not valid for temporary tables. A session becomes unbound from the temporary table when the table is truncated, so the storage is automatically dropped.
If you have specified more than one free list for the object you are truncating, then the reuse storage clause also removes any mapping of free lists to instances and resets the high-water mark to the beginning of the first extent.
Ii. Demonstration of truncate table .. reuse storage (11g)
SQL> select * from v $ version where rownum = 1;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0-64bit Production
SQL> create table tb_reuse as select * from dba_objects;
Table created.
SQL>/-- multiple executions
37200896 rows created.
SQL> create table tb_noreuse as select * from tb_reuse;
Table created.
SQL> select count (*) from tb_reuse;
COUNT (*)
----------
37200896
SQL> select count (*) from tb_noreuse;
COUNT (*)
----------
37200896
SQL> select segment_name, bytes/1024/1024 from dba_segments where segment_name in ('tb _ reuse', 'tb _ noreuse ');
SEGMENT_NAME BYTES/1024/1024
--------------------------------------------------
TB_REUSE 4165 -- occupied space close to 4 GB
TB_NOREUSE 4172.
SQL> truncate table tb_noreuse; -- directly truncate, fast
Table truncated.
Elapsed: 00:00:00. 25
SQL> select segment_name, bytes/1024/1024 from dba_segments where segment_name in ('tb _ reuse', 'tb _ noreuse ');
SEGMENT_NAME BYTES/1024/1024
--------------------------------------------------
TB_REUSE 4165
TB_NOREUSE. 0625 -- the space has been recycled
Elapsed: 00:00:00. 03
SQL> truncate table tb_reuse reuse storage; -- using the reuse storage method does not improve performance much.
Table truncated.
Elapsed: 00:00:00. 07
SQL> alter table tb_reuse deallocate unused keep 2048; -- the specified m is missing. The default value is byte.
Table altered.
Elapsed: 00:00:00. 36
SQL> select segment_name, bytes/1024/1024 from dba_segments where segment_name in ('tb _ reuse', 'tb _ noreuse ');
SEGMENT_NAME BYTES/1024/1024
--------------------------------------------------
Tb_reused. 0625
TB_NOREUSE. 0625
Elapsed: 00:00:00. 03
Iii. Demonstration of truncate table .. reuse storage (12g)
SQL> select * from v $ version where rownum = 1;
BANNER CON_ID
------------------------------------------------------------------------------------------
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0-64bit Production 0
SQL> create table tb_12_use as select * from dba_objects;
Table created.
SQL> insert into tb_12_use select * from tb_12_use;
90903 rows created.
SQL>/
11635584 rows created.
SQL> create table tb_12_nouse as select * from tb_12_use;
Table created.
SQL> select segment_name, bytes/1024/1024 from dba_segments where segment_name in ('tb _ 12_USE ', 'tb _ 12_NOUSE ');
SEGMENT_NAME BYTES/1024/1024
---------------------------------------------
TB_12_NOUSE 3074 -- 3 GB space used
TB_12_USE 3072
SQL> select 'lesham' As author, 'HTTP: // blog.csdn.net/leshami' as Blog from dual;
AUTHOR BLOG
-----------------------------------
Leshami