MPTY_BLOCKS --------------- --------------- ---------- ------------ EMPLX LXTBS2 23 0
SQL> select * from emplx;
no rows selected
Execution Plan ---------------------------------------------------------- Plan hash value: 155704778
--------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3584 | 112K| 8 (0)| 00:00:01 | | 1 | TABLE ACCESS FULL| EMPLX | 3584 | 112K| 8 (0)| 00:00:01 | ---------------------------------------------------------------------------
#执行truncate操作: SQL> truncate table emplx;
Table truncated. #再对进行分析 SQL> analyze table emplx compute statistics;
Table analyzed.
#truncate降低高水位线,只剩余一个区,并且会降低全表扫描的代价 SQL> select table_name,tablespace_name,blocks, empty_blocks from dba_tables where table_name='EMPLX';
TABLE_NAME TABLESPACE_NAME BLOCKS EMPTY_BLOCKS --------------- --------------- ---------- ------------ EMPLX LXTBS2 0 7
3.delete操作后降低表空间。(重点) 使用alter table emplx move 和alter table emplx shrink space都可以用来进行段收缩,降低高水位线,也都可以用来消除行连接和行迁移。 ①.使用alter table move会把表最多收缩到创建表时的storage子句中指定的初始大小,使用later shrink space 不会受此限制。 ②.使用alter table move 之后,索引会无效,需要重建,使用alter table shrink space 则不会。 ③.只能在表所在的表空间是自动段管理(ASSM)的时候,才能使用alter table shrink space; 而move在自动段管理(ASSM)和手动段管理(MSSM)方式下都能使用。 ④.可以使用alter table shrink space compact来对表进行碎片整理,而不调整高水位线,之后再次调用alter table shrink space 来释放空间。 ⑤.可以使用alter table shrink space cascade;指定了cascade,则收缩行为将级联到所有支持收缩操作的从属段,但是 物化视图、LOB索引、和IOT表例外; ⑥.move 更快 shrink space 较慢。
例:alter table emplx move 和alter table emplx shrink space使用:在MSSM管理的表空间上创建EMPLX表
SQL> select table_name,tablespace_name, blocks,empty_blocks from dba_tables where table_name='EMPLX';
TABLE_NAME TABLESPACE_NAME BLOCKS EMPTY_BLOCKS ------------ ------------------------------ ---------- ------------ EMPLX LXTBS2 0 7
#插入数据 SQL> insert into emplx select * from emplx;
1792 rows created. #分析表 SQL> analyze table emplx compute statistics;
Table analyzed.
SQL> select table_name,tablespace_name, blocks,empty_blocks from dba_tables where table_name='EMPLX';
TABLE_NAME TABLESPACE_NAME BLOCKS EMPTY_BLOCKS ------------ ------------------------------ ---------- ------------ EMPLX LXTBS2 23 0 #删除数据 SQL> delete from emplx where rownum <=2000;
2000 rows deleted.
SQL> commit;
Commit complete.
SQL> select table_name,tablespace_name, blocks,empty_blocks from dba_tables where table_name='EMPLX';
TABLE_NAME TABLESPACE_NAME BLOCKS EMPTY_BLOCKS ------------ ------------------------------ ---------- ------------ EMPLX LXTBS2 23 0 #使用shrink space 的前奏 SQL> alter table emplx enable row movement ;
Table altered. #使用shrink space降低高水位线,报错,因为在shrink space只能使用在assm管理段空间方式的表空间上。 SQL> alter table emplx shrink space; alter table emplx shrink space * ERROR at line 1: ORA-10635: Invalid segment or tablespace type
#使用move降低高水位线 SQL> alter table emplx move;
Table altered. #分析表 SQL> analyze table emplx compute statistics;
Table analyzed. #查看块的使用情况 SQL> select table_name,tablespace_name, blocks,empty_blocks from dba_tables where table_name='EMPLX';
TABLE_NAME TABLESPACE_NAME BLOCKS EMPTY_BLOCKS ------------ ------------------------------ ---------- ------------ EMPLX LXTBS2 10 5