您的位置:首页 > 数据库 > Oracle

比较Oracle中的alter table t move和alter table t shrink space

2012-01-28 20:44 369 查看
alter table t move和alter table t shrink space都可以用来进行段收缩,降低高水位HWM,

也都可以用来消除行链接(Row Chaining)和行迁移(Row Migration),

但是有如下区别:

1)使用alter table move,会把表格最多收缩到创建表格时的storage子句指定的初始大小,使用alter table shrink space,则不受此限制。

2)使用alter table move之后,索引会无效,需要重建,使用alter table shrink space,则不会使索引无效。

3)只能在表格所在的表空间是自动段空间管理(创建tablespace时指定了SEGMENT SPACE MANAGEMENT AUTO子句)的时候,才能使用alter table shrink space。

4)可以使用alter table shrink space compact来对表格进行碎片整理,而不调整HWM,之后再次调用alter table shrink space来释放空间。

5)可以使用alter table shrink space cascade来同时对索引都进行收缩,这等同于同时执行alter index shrink space。

下面的例子,创建1个表格T,建表格时的storage子句指定表格初始大小为5M,

数据库block大小为8K,因此等同于5*1024/8=640 block。

使用alter table move,会把表格最多收缩到初始大小640 block。

使用alter table shrink space,则不受此限制。

tony@ORA11GR2> select * from v$version;

BANNER
------------------------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

tony@ORA11GR2> show parameter db_block_size

NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
db_block_size                        integer                8192

tony@ORA11GR2> column segment_name format a40;
tony@ORA11GR2> column table_name format a40;
tony@ORA11GR2> create table t storage (initial 5m) as select * from all_objects;

Table created.

tony@ORA11GR2> exec dbms_stats.gather_table_stats(user, 'T');

PL/SQL procedure successfully completed.

tony@ORA11GR2> select table_name, blocks, empty_blocks from user_tables where table_name = 'T';

TABLE_NAME                                   BLOCKS EMPTY_BLOCKS
---------------------------------------- ---------- ------------
T                                               820            0

tony@ORA11GR2> select segment_name, extents, blocks, initial_extent from user_segments where segment_name = 'T';

SEGMENT_NAME                                EXTENTS     BLOCKS INITIAL_EXTENT
---------------------------------------- ---------- ---------- --------------
T                                                 7        896        5242880

tony@ORA11GR2> select count(distinct dbms_rowid.rowid_block_number(rowid)) from t;

COUNT(DISTINCTDBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID))
---------------------------------------------------
804

tony@ORA11GR2> delete from t where rownum < 50000;

49999 rows deleted.

tony@ORA11GR2> create index t_idx on t(object_id);

Index created.

tony@ORA11GR2> alter table t move;

Table altered.

tony@ORA11GR2> column index_name format a40;
tony@ORA11GR2> select index_name, status from user_indexes where table_name = 'T';

INDEX_NAME                               STATUS
---------------------------------------- ----------------
T_IDX                                    UNUSABLE

tony@ORA11GR2> alter index t_idx rebuild;

Index altered.

tony@ORA11GR2> exec dbms_stats.gather_table_stats(user, 'T');

PL/SQL procedure successfully completed.

tony@ORA11GR2> select table_name, blocks, empty_blocks from user_tables where table_name = 'T';

TABLE_NAME                                   BLOCKS EMPTY_BLOCKS
---------------------------------------- ---------- ------------
T                                                86            0

tony@ORA11GR2> select segment_name, extents, blocks, initial_extent from user_segments where segment_name = 'T';

SEGMENT_NAME                                EXTENTS     BLOCKS INITIAL_EXTENT
---------------------------------------- ---------- ---------- --------------
T                                                 5        640        5242880

tony@ORA11GR2> select count(distinct dbms_rowid.rowid_block_number(rowid)) from t;

COUNT(DISTINCTDBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID))
---------------------------------------------------
82

tony@ORA11GR2> alter table t enable row movement;

Table altered.

tony@ORA11GR2> alter table t shrink space;

Table altered.

tony@ORA11GR2> select index_name, status from user_indexes where table_name = 'T';

INDEX_NAME                               STATUS
---------------------------------------- ----------------
T_IDX                                    VALID

tony@ORA11GR2> exec dbms_stats.gather_table_stats(user, 'T');

PL/SQL procedure successfully completed.

tony@ORA11GR2> select table_name, blocks, empty_blocks from user_tables where table_name = 'T';

TABLE_NAME                                   BLOCKS EMPTY_BLOCKS
---------------------------------------- ---------- ------------
T                                                86            0

tony@ORA11GR2> select segment_name, extents, blocks, initial_extent from user_segments where segment_name = 'T';

SEGMENT_NAME                                EXTENTS     BLOCKS INITIAL_EXTENT
---------------------------------------- ---------- ---------- --------------
T                                                 1         88        5242880

tony@ORA11GR2> select count(distinct dbms_rowid.rowid_block_number(rowid)) from t;

COUNT(DISTINCTDBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID))
---------------------------------------------------
82


下面这个例子可以验证alter table t move和alter table t shrink space都可以用来消除行链接(Row Chaining)和行迁移(Row Migration)。

为此需要先建立chained_rows表格。

首先执行$ORACLE_HOME/RDBMS/ADMIN/utlchain.sql脚本建立chained_rows表格,

然后执行analyze table xxx list chained rows [into chained_rows],

如果存在行链接或者行迁移,查询chained_rows就能找到发生了行链接或者行迁移的行。

tony@ORA11GR2> drop table t purge;

Table dropped.

tony@ORA11GR2> create table t
2  ( x int primary key,
3    y varchar2(4000)
4  );

Table created.

tony@ORA11GR2> insert into t (x,y)
2  select rownum, rpad('*',148,'*')
3    from dual
4  connect by level <= 46;

46 rows created.

tony@ORA11GR2> update t set y = rpad('*',2000,'*') where x = 1;

1 row updated.

tony@ORA11GR2> analyze table t list chained rows;

Table analyzed.

tony@ORA11GR2> select count(*) from chained_rows;

COUNT(*)
----------
1

tony@ORA11GR2> alter table t enable row movement;

Table altered.

tony@ORA11GR2> alter table t shrink space;

Table altered.

tony@ORA11GR2> delete from chained_rows;

1 row deleted.

tony@ORA11GR2> analyze table t list chained rows;

Table analyzed.

tony@ORA11GR2> select count(*) from chained_rows;

COUNT(*)
----------
0

tony@ORA11GR2> update t set y = rpad('*',2000,'*') where x = 2;

1 row updated.

tony@ORA11GR2> analyze table t list chained rows;

Table analyzed.

tony@ORA11GR2> select count(*) from chained_rows;

COUNT(*)
----------
1

tony@ORA11GR2> alter table t move;

Table altered.

tony@ORA11GR2> delete from chained_rows;

1 row deleted.

tony@ORA11GR2> analyze table t list chained rows;

Table analyzed.

tony@ORA11GR2> select count(*) from chained_rows;

COUNT(*)
----------
0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: