alter table move跟shrink space的区别

alter table move跟shrink space的区别


都知道alter table move 或shrink space能够收缩段,用来消除部分行迁移,消除空间碎片,使数据更紧密,但move 跟shrink space仍是有区别的。
Move会移动高水位,但不会释放申请的空间,是在高水位如下(below HWM)的操做。
而shrink space 一样会移动高水位,但也会释放申请的空间,是在高水位上下(below and above HWM)都有的操做。
也许很难理解吧,看测试就知道了。
 
SQL> select * from v$version;
 
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
 
SQL> create table test (id number) storage (initial 10m next 1m) tablespace users;
 
Table created.
 
SQL> analyze table test compute statistics;
 
Table analyzed.
 
SQL> col SEGMENT_NAME for a10
SQL> select SEGMENT_NAME,EXTENTS,BLOCKS,INITIAL_EXTENT/1024/1024 init from user_segments where SEGMENT_NAME='TEST';
 
SEGMENT_NA    EXTENTS     BLOCKS       INIT
---------- ---------- ---------- ----------
TEST               10       1280         10
 
SQL> col TABLE_NAME for a10
SQL> select TABLE_NAME,BLOCKS,EMPTY_BLOCKS from user_tables where table_name='TEST';
 
TABLE_NAME     BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST                0         1280
--TEST表初始分配了10M的空间,能够看到有10个EXTENTS,1280个BLOCKS。USER_TABLES视图显示有0个使用的BLOCKS,1280个空闲BLOCKS,即该10M空间内的BLOCK都还没被ORACLE”格式化”。
 
SQL> begin
2   for i in 1..100000 loop
3   insert into test values(i);
4   end loop;
5   end;
6   /
 
PL/SQL procedure successfully completed.
 
SQL> analyze table test compute statistics;
 
Table analyzed.
 
SQL> select SEGMENT_NAME,EXTENTS,BLOCKS from user_segments where SEGMENT_NAME='TEST';
 
SEGMENT_NA    EXTENTS     BLOCKS
---------- ---------- ----------
TEST               10       1280
 
SQL> select TABLE_NAME,BLOCKS,EMPTY_BLOCKS from user_tables where table_name='TEST';
 
TABLE_NAME     BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST              186         1094
--插入10W条数据后,分配的空间仍不变,由于10个EXTENTS还没使用完。显示使用了186个BLOCKS,空闲1094个BLOCKS。这时候的186BLOCKS便是高水位线
 
SQL> delete from test where rownum<=50000;
 
50000 rows deleted.
 
SQL> analyze table test compute statistics;
 
Table analyzed.
 
SQL> select SEGMENT_NAME,EXTENTS,BLOCKS from user_segments where SEGMENT_NAME='TEST';
 
SEGMENT_NA    EXTENTS     BLOCKS
---------- ---------- ----------
TEST               10       1280
 
SQL> select TABLE_NAME,BLOCKS,EMPTY_BLOCKS from user_tables where table_name='TEST';
 
TABLE_NAME     BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST              186         1094
 
SQL> select count(distinct dbms_rowid.rowid_block_number(rowid)) used_blocks from test;
 
USED_BLOCKS
-----------
         77
--这边能够看到,删掉一半数据后,仍然显示使用了186个BLOCKS,高水位没变。但查询真正使用的BLOCK数只有77个。因此DELETE操做是不会改变HWM的
 
SQL> alter table test move;
 
Table altered.
 
SQL> analyze table test compute statistics;
 
Table analyzed.
 
SQL> select TABLE_NAME,BLOCKS,EMPTY_BLOCKS from user_tables where table_name='TEST';
 
TABLE_NAME     BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST               81         1199
--MOVE以后,HWM下降了,空闲块也上去了
 
SQL> select SEGMENT_NAME,EXTENTS,BLOCKS from user_segments where SEGMENT_NAME='TEST';
 
SEGMENT_NA    EXTENTS     BLOCKS
---------- ---------- ----------
TEST               10       1280
--可是分配的空间并无改变,仍然是1280个BLOCKS。下面看用SHRINK SPACE的方式
 
SQL> alter table test enable row movement;
 
Table altered.
 
SQL> alter table test shrink space;
 
Table altered.
 
SQL> analyze table test compute statistics;
 
Table analyzed.
 
SQL> select SEGMENT_NAME,EXTENTS,BLOCKS from user_segments where SEGMENT_NAME='TEST';
 
SEGMENT_NA    EXTENTS     BLOCKS
---------- ---------- ----------
TEST                1         88
 
SQL> select TABLE_NAME,BLOCKS,EMPTY_BLOCKS from user_tables where table_name='TEST';
 
TABLE_NAME     BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST               81            7
--分配的空间已经降到最小,1个EXTENTS ,88个BLOCKS
 
 
因此MOVE并不算真正意义上的压缩空间,只会压缩HWM如下的空间,消除碎片。咱们通常建表时没有指定initial参数(默认是8个BLOCK),也就感受不到这个差别。而SHRINK SPACE真正作到了对段的压缩,包括初始分配的也压了,因此它是blow and above HWM操做。
至于须要哪一种方法,得看你的需求来了,须要分析表的增加状况,要是之后还会达到之前的HWM高度,那显然MOVE是更合适的,由于SHRINK SPACE还须要从新申请以前放掉的空间,无疑增长了操做。
 
注意:
1.不过用MOVE的方式也能够作到真正的压缩分配空间,只要指定STORAGE参数便可。
 
SQL> drop table test;
 
Table dropped.
 
SQL> create table test (id number) storage (initial 10m next 1m) tablespace users;
 
Table created.
 
SQL> analyze table test compute statistics;
 
Table analyzed.
 
SQL> select SEGMENT_NAME,EXTENTS,BLOCKS,INITIAL_EXTENT/1024/1024 init from user_segments where SEGME
NT_NAME='TEST';
 
SEGMENT_NA    EXTENTS     BLOCKS       INIT
---------- ---------- ---------- ----------
TEST               10       1280         10
 
SQL> select TABLE_NAME,BLOCKS,EMPTY_BLOCKS from user_tables where table_name='TEST';
 
TABLE_NAME     BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST                0         1280
 
SQL> alter table test move storage (initial 1m);
 
Table altered.
 
SQL> analyze table test compute statistics;
 
Table analyzed.
 
SQL> select SEGMENT_NAME,EXTENTS,BLOCKS,INITIAL_EXTENT/1024/1024 init from user_segments where SEGME
NT_NAME='TEST';
 
SEGMENT_NA    EXTENTS     BLOCKS       INIT
---------- ---------- ---------- ----------
TEST              16        128          1
 
SQL> select TABLE_NAME,BLOCKS,EMPTY_BLOCKS from user_tables where table_name='TEST';
 
TABLE_NAME     BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST               0          128
 
2.使用move时,会改变一些记录的ROWID,因此MOVE以后索引会变为无效,须要REBUILD。
3.使用shrink space时,索引会自动维护。若是在业务繁忙时作压缩,能够先shrink space compact,来压缩数据而不移动HWM,等到不繁忙的时候再shrink space来移动HWM。
4.索引也是能够压缩的,压缩表时指定Shrink space cascade会同时压缩索引,也能够alter index xxx shrink space来压缩索引。
5.shrink space须要在表空间是自动段空间管理的,因此system表空间上的表没法shrink space。    ide

相关文章
相关标签/搜索