Oracle 释放表高水位线(HWM)的五种方法

表高水位线概述:
高水线(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:代表分配给该表,但是在高水位线以上的数据块,即从来没有使用的数据块。

一、move表

1.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

1.2.move表之前,查看表上是否存在索引

SQL> select index_name,table_name,table_owner from dba_indexes where table_name='big_table';

1.3.如存在索引,move表后需重建索引(可选)

SQL> alter index <index_name> rebuild;

1.4.进行move

SQL> alter table big_table move; 

Table altered.

1.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

二、收缩表

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.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;

2.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  ---表示可以进行表收缩

2.4.收缩表数据

SQL> alter table test.big_table shrink space compact;

2.5.降低高水位线

SQL> alter table test.big_table shrink space;

2.6.收缩索引

SQL> alter table test.big_table shrink space cascade;

2.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

2.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;

三、数据泵导出导入重构表

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.对表进行导出操作

expdp \'/ as sysdba\' directory=dir dumpfile=big_table.dmp logfile=table.log schemas=test tables=big_table

3.3.删除表

SQL> drop table big_table purge;

Table dropped.

3.4.导入表

impdp \'/ as sysdba\' directory=dir dumpfile=big_table.dmp logfile=table2.log tables=test.big_table

3.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

四、复制表数据的方式

(也就是将保留的数据复制到新表,drop原表,最后将新表rename为原表的方式)

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.创建新表

SQL> create table copy_big_table as select * from big_table;

Table created.

4.3.drop原表

SQL> drop table big_table purge;

Table dropped.

4.4.rename新表

SQL> alter table copy_big_table rename to big_table;

Table altered.

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
---------- ------------ ----------
   14998	    362      1010504

五、truncate

(仅在表中没有数据的情况下使用,谨慎使用)

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.删除表中的所有数据

SQL> delete big_table where id>0;

2000000 rows deleted.

SQL> commit;

Commit complete.

5.3.在表中没有数据的情况下,执行 truncate 来释放高水位线

SQL> truncate table big_table;

Table truncated.

5.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

你可能感兴趣的:(oracle,数据库,dba)