ALTER TABLE MOVE和SHRINK SPACE區別
Alter table move或者shrink space可以收縮段,消除部分行遷移、消除空間碎片、使資料更緊密,但move跟shrink space還是有區別的。
Move會移動高水位,但不會釋放申請的空間,是在HWM以下的操作。
Shrink space同樣會移動hwm,但也會釋放申請的空間,是在hwm上下都有的操作。
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
首先用alter table move方式:
建立測試表
SQL> create table test(id number) storage(initial 10m next 1m) tablespace scott_test;
Table created.
SQL> analyze table test compute statistics;
Table analyzed.
SQL> select table_name,blocks,empty_blocks from user_tables
2 where
table_name='TEST';
TABLE_NAME
BLOCKS EMPTY_BLOCKS
--------------------- ------------------ -------------------------
TEST 0 1280
SQL> select segment_name,extents,blocks from user_segments
2 where segment_name='TEST';
SEGMENT_NA
EXTENTS BLOCKS
TEST 10 1280
SQL> begin
2 for i in 1..100000 loop
3 insert into test values(i);
4 end loop;
5 end;
6 /
SQL> analyze table test compute statistics; --對test表操作後,只有對該表做過分析後(compute
statistics),資料字典才會記錄該表修改後的狀態
Table analyzed.
SQL> col segment_name format a20
SQL> select table_name,blocks,empty_blocks from user_tables
2 where table_name='TEST';
TABLE_NAME
BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST 186
1094 --空閒塊降低了
SQL> select segment_name,extents,blocks from user_segments
2 where
segment_name='TEST';
SEGMENT_NA
EXTENTS BLOCKS
---------- ---------- ----------
TEST 10
1280
插入10萬行後,分配空間沒變,分配的10個extents並未使用完,使用了186個blocks,空閒1094個blocks,此時hwm為186。
SQL> delete from test where rownum <=50000; --刪除一半的資料
50000 rows deleted.
SQL> analyze table test compute statistics;
Table analyzed.
SQL> select table_name,blocks,empty_blocks from user_tables
2 where
table_name='TEST';
TABLE_NAME
BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST 186 1094
SQL> select segment_name,extents,blocks from user_segments
2 where segment_name='TEST';
SEGMENT_NA
EXTENTS BLOCKS
---------- ---------- ----------
TEST 10 1280
SQL> select count(distinct dbms_rowid.rowid_block_number(rowid))
used_blocks
2 from test;
-----------
77
刪除一半資料後仍然使用186個blocks,而真正使用了77個blocks。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
2 where
table_name='TEST';
TABLE_NAME BLOCKS
EMPTY_BLOCKS
---------- ---------- ------------
TEST 81
1199 --move後,空閒塊增加了,hwm降低了
SQL> select segment_name,extents,blocks from
user_segments
2 where
segment_name='TEST';
SEGMENT_NA
EXTENTS BLOCKS
---------- ---------- ----------
TEST 10
1280 --但分配空間並沒有降低
move後,空閒塊增加了,hwm降低了,但分配空間並沒有降低。
使用alter table shrink space方式:
SQL> create table test(id number) storage(initial 10m next 1m) tablespace scott_test;
Table created.
SQL> analyze table test compute statistics;
Table analyzed.
SQL> col segment_name for a10
SQL> select table_name,blocks,empty_blocks from
user_tables where
2 table_name='TEST';
TABLE_NAME
BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST 0
1280
SQL> select segment_name,extents,blocks from
user_segments
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 table_name,blocks,empty_blocks from user_tables
2 where table_name='TEST';
TABLE_NAME BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST 186 1094
SQL> select segment_name,extents,blocks from user_segments
2 where segment_name='TEST';
SEGMENT_NA EXTENTS BLOCKS
---------- ---------- ----------
TEST 10 1280
SQL> delete from test where rownum<=50000;
50000 rows deleted.
SQL> select table_name,blocks,empty_blocks from user_tables
2 where table_name='TEST';
TABLE_NAME BLOCKS EMPTY_BLOCKS
----------------------- --------------- ---------------------
TEST 186 1094
SQL> select segment_name,extents,blocks from user_segments
2 where segment_name='TEST';
SEGMENT_NA EXTENTS BLOCKS
---------------------- ----------------- -----------------
TEST 10 1280
SQL> select count(distinct dbms_rowid.rowid_block_number(rowid)) used_blocks from test;
USED_BLOCKS
-----------
77
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 table_name,blocks,empty_blocks from user_tables
2 where table_name='TEST';
TABLE_NAME BLOCKS EMPTY_BLOCKS
---------- ---------- ------------
TEST 79 9 --shrink space後,空閒減少了,hwm降低了
SQL> select segment_name,extents,blocks from user_segments
2 where segment_name='TEST';
SEGMENT_NA EXTENTS BLOCKS
---------- ---------- ----------
TEST 1 88 --同時分配空間也降低了
透過上面比較,move不算真正意義上的壓縮空間,只會壓縮HWM以下的空間,消除碎片。shrink space真正做到了對段的壓縮,包括初始分配的也壓縮了,所以是hwm上和下的操作。根據需求來選擇壓縮方式。如果以後還會達到以前hwm高度,則選擇move更合適。
1、move可以透過制定storage引數做到真正壓縮分配空間;
2、使用move時會改變一些記錄的rowid,所以move後索引會變為無效,需要rebuild;
3、使用shrink space時,索引會自動維護。如果在業務繁忙時做壓縮,可以先shrink space compact,來壓縮,shrink space cascade會同時壓縮索引。可以用alter index xxx shrink space;
4、shrink space需要在表空間是自動段空間的,所以system表空間上的表無法shrink space。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12615085/viewspace-747059/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- alter table move 和 alter table shrink space的區別
- ALTER TABLE MOVE | SHRINK SPACE區別
- alter table move 與shrink space的區別
- alter table move跟shrink space的區別
- alter table move跟shrink space的區別(轉)
- Oracle 11g alter table move與shrink spaceOracle
- [Oracle] Shrink space & Table move比較Oracle
- table move 與 shrink 的區別
- oracle 10g__alter table shrink space compactOracle 10g
- Oracle 10g Shrink Table - Shrink Space 收縮空間Oracle 10g
- alter table table_name move ; 在自身表空間move是如何操作的?
- oracle10g_alter table shrink space_compact_cascade回收空間測試(一)Oracle
- 表、索引遷移表空間alter table move索引
- alter database和alter system和alter session的區別DatabaseSession
- v$lock之alter table drop column與alter table set unused column區別系列五
- 測試alter table shrink space compact cascade及學習user_tables相關列的含義
- alter table列管理的一些區別
- Oracle IZ0-053 Q277(Table shrink space)Oracle
- Oracle move和shrink釋放高水位空間Oracle
- How to adjust the high watermark in ORACLE 10g – ALTER TABLE SHRINKOracle 10g
- 測試alter table storage及dbms_space_admin包
- oracle shrink tableOracle
- drop table和truncate table的區別
- 忍不住問下alter system 和alter database的區別Database
- [重慶思莊每日技術分享]-在為表新增了列後執行ALTER TABLE SHRINK SPACE 提示ORA-8102
- shrink space的最佳實踐
- 【轉】Oracle:MOVE與SHRINK命令相比較Oracle
- Oracle中shrink space命令詳解Oracle
- alter system switch logfile和alter system archive log current的區別Hive
- alter system switch logfile 和 alter system archive log current 的區別Hive
- Alter table for ORACLEOracle
- Oracle --- PLAN_TABLE$和PLAN_TABLE區別Oracle
- [轉]alter system switch logfile和alter system archive log current的區別Hive
- alter system set event和set events的區別
- oracle10g shrink space 降低HWMOracle
- alter system events與alter system event的區別
- 7 、shrink table and its dependent segments
- alter index rebuild和rebuild online的區別IndexRebuild