创建表
SQL> create table scott.big tablespace users as select * from all_objects ;
Table created.
delete删除
SQL> delete from big;
55658 rows deleted.
SQL> commit;
Commit complete.
分析统计信息
SQL> analyze table big estimate statistics;
在user_tables视图中查看收集的统计信息
SQL> select blocks from user_tables where table_name='BIG';
BLOCKS
----------
4906
SQL> select count(*) from big;
big表的统计信息,big中数据被删除了,但数据块中的空间还没有被释放,高水位线依然在原来的位置,当查询该表的时候,空块依然被搜索。
SQL> select num_rows,blocks,empty_blocks ,avg_space,chain_cnt,avg_row_len from user_tables where table_name='BIG';
NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE CHAIN_CNT AVG_ROW_LEN
---------- ---------- ------------ ---------- ---------- -----------
0 825 71 7907 0 0
BLOCKS :825 #高水位以下的数据块数
通过查看试图中AVG_SPACE、BLOCKS可知道段是否需要压缩
AVG_SPACE 默认大小8k,空闲了7K,占块825,NUM_ROWS=0 需要压缩。
对big表进行压缩
1. 开启行迁移
alter table scott.big enable row movement;
2. 收缩空间
alter table scott.big shrink space;
3.查出需要resize的数据文件
select * from dba_segments where tablespace_name='USERS' and segment_name='BIG';
4. 减少数据文件大小(大小一定要大于已用大小)
alter database datafile 7 resize 2M;
delete 方法不会清除高水位线
truncate 可清除高水位线,但不会缩小表空间
此语句是整合表空间的碎片增加表空间的连续性,但是他不会收缩一个文件的大小:
alter tablespace TABLESPACENAME coalesce;
对于undo表空间
SQL> create undo tablespace undotbs1 datafile '+data' size 20M ;
SQL> alter system set undo_tablespace=undotbs1;
System altered.
SQL> drop tablespace undo02;