转自:https://blog.csdn.net/loveLAxin/article/details/129357083
1.表高水位线概述:
高水线(HWM)是数据块未格式化且从未使用过的数据段中的点。也就是说高水位线存在于段。 查询表数据时,会扫描高水位线下的所有数据块。(包括删除以后不存在数据的块) 原则上高水位线只会增大,不会缩小。 查看表的高水位线,示例:SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
---------- ------------ ----------
30329 391 2010778
注:
BLOCKS:代表该表中曾经使用过的数据块的数量,即高水位线。
EMPTY_BLOCKS:代表分配给该表,但是在高水位线以上的数据块,即从来没有使用的数据块。
2.move表
2.1查看当前高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
---------- ------------ ----------
30329 391 2010778
2.2move表之前,查看表上是否存在索引
SQL> select index_name,table_name,table_owner from dba_indexes where table_name='big_table';
2.3如存在索引,move表后需重建索引(可选)
SQL> alter index <index_name> rebuild;
2.4进行move
SQL> alter table big_table move;
Table altered.
2.5释放后的高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
---------- ------------ ----------
15001 359 1011059
3.收缩表
3.1查看当前高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
---------- ------------ ----------
30329 391 2010778
3.2开启行移动和并行
查看当前并行:
SQL> select OWNER,TABLE_NAME,DEGREE from DBA_TABLES where OWNER ='TEST' and TABLE_NAME = 'BIG_TABLE';
OWNER TABLE_NAME DEGREE
--------- ------------ ---------
TEST BIG_TABLE DEFAULT
设置并行:
alter table test.BIG_TABLE parallel 2;
开启行移动:
alter table test.big_table enable row movement;
3.3检查表能否被收缩
SQL> alter table test.big_table shrink space check;
alter table test.big_table shrink space check
*
ERROR at line 1:
ORA-10655: Segment can be shrunk ---表示可以进行表收缩
3.4收缩表数据
SQL> alter table test.big_table shrink space compact;
3.5降低高水位线
SQL> alter table test.big_table shrink space;
3.6收缩索引
SQL> alter table test.big_table shrink space cascade;
3.7.释放后的高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
--------- ------------ ----------
14984 168 1002139
3.8.禁用行移动和并行
SQL> alter table test.big_table disable row movement;
SQL> alter table test.big_table parallel 1;
SQL> alter table test.big_table parallel;
4.数据泵导出导入重构表
4.1查看当前高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
---------- ------------ ----------
30329 391 2010778
4.2对表进行导出操作
expdp \'/ as sysdba\' directory=dir dumpfile=big_table.dmp logfile=table.log schemas=test tables=big_table
4.3删除表
SQL> drop table big_table purge;
Table dropped.
4.4导入表
impdp \'/ as sysdba\' directory=dir dumpfile=big_table.dmp logfile=table2.log tables=test.big_table
4.5释放后的高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
--------- ------------ ----------
14988 372 1010535
5.复制表数据的方式
(也就是将保留的数据复制到新表,drop原表,最后将新表rename为原表的方式)5.1查看当前高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
---------- ------------ ----------
30329 391 2010778
5.2创建新表
建议使用dbms_metadata.get_ddl方法进行获取完整的表结构https://www.cnblogs.com/dclogs/p/18189838
SQL> create table copy_big_table as select * from big_table;
Table created.
5.3drop原表
SQL> drop table big_table purge;
Table dropped.
5.4rename新表
SQL> alter table copy_big_table rename to big_table;
Table altered.
5.5.释放后的高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
---------- ------------ ----------
14998 362 1010504
6.truncate
(仅在表中没有数据的情况下使用,谨慎使用)6.1查看当前高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
---------- ------------ ----------
30329 391 2010778
6.2删除表中的所有数据
SQL> delete big_table where id>0;
2000000 rows deleted.
SQL> commit;
Commit complete.
6.3在表中没有数据的情况下,执行 truncate 来释放高水位线
SQL> truncate table big_table;
Table truncated.
6.4释放后的高水位线
SQL> analyze table big_table estimate statistics;
SQL> select blocks,empty_blocks,num_rows from user_tables where table_name='BIG_TABLE';
BLOCKS EMPTY_BLOCKS NUM_ROWS
--------- ------------ ----------
0 8 0
标签:表高,HWM,BLOCKS,big,blocks,----------,SQL,Oracle,table
From: https://www.cnblogs.com/dclogs/p/18190285