表高水位线概述:
高水线(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:代表分配给该表,但是在高水位线以上的数据块,即从来没有使用的数据块。
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
SQL> select index_name,table_name,table_owner from dba_indexes where table_name='big_table';
SQL> alter index <index_name> rebuild;
SQL> alter table big_table move;
Table altered.
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
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
查看当前并行:
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;
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 ---表示可以进行表收缩
SQL> alter table test.big_table shrink space compact;
SQL> alter table test.big_table shrink space;
SQL> alter table test.big_table shrink space cascade;
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
SQL> alter table test.big_table disable row movement;
SQL> alter table test.big_table parallel 1;
SQL> alter table test.big_table parallel;
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
expdp \'/ as sysdba\' directory=dir dumpfile=big_table.dmp logfile=table.log schemas=test tables=big_table
SQL> drop table big_table purge;
Table dropped.
impdp \'/ as sysdba\' directory=dir dumpfile=big_table.dmp logfile=table2.log tables=test.big_table
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为原表的方式)
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
SQL> create table copy_big_table as select * from big_table;
Table created.
SQL> drop table big_table purge;
Table dropped.
SQL> alter table copy_big_table rename to big_table;
Table altered.
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
(仅在表中没有数据的情况下使用,谨慎使用)
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
SQL> delete big_table where id>0;
2000000 rows deleted.
SQL> commit;
Commit complete.
SQL> truncate table big_table;
Table truncated.
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