转自:http://blog.itpub.net/30130773/viewspace-2100915/
降低高水位方法
1. move
a.move不但可以重置水位线(HWM),解决松散表带来的IO浪费,还可以解决表中的行迁移问题;
b.move可以将表移动到其他表空间,也可以在原表空移动,这样可以一定程度解决表空间碎片;
c.如果表空间上有大量表、索引被drop(或者truncate),导致表空间前半部分出现大量空闲空间,可以通过move将靠后的表移动到前面的空闲空间,从而收缩数据文件。
实验:
sys@ORCL>conn shall/shall Connected. shall@ORCL>create table zhong(x int); Table created. shall@ORCL>begin 2 for i in 1..100000 loop 3 insert into zhong values(i); 4 end loop; 5 commit; 6 end; 7 / PL/sql procedure successfully completed.
----收集统计信息
shall@ORCL>analyze table zhong compute statistics; Table analyzed. shall@ORCL>select table_name,blocks,empty_blocks from user_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 244 12
shall@ORCL>delete zhong; 100000 rows deleted. shall@ORCL>analyze table zhong compute statistics; Table analyzed. shall@ORCL>select table_name,empty_blocks from user_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 244 12
----move整理碎片
shall@ORCL>alter table zhong move; Table altered. 或者 alter table zhong move tablespace hct; ----move到hct表空间 /* 如果move到hct表空间了,可以看见表空间已经变了,如下 shall@ORCL>select table_name,tablespace_name from user_tables; TABLE_NAME TABLESPACE_NAME ------------------------------ ------------------------------ TTTT USERS ZHONG HCT */ shall@ORCL>analyze table zhong compute statistics; Table analyzed. shall@ORCL>select table_name,empty_blocks from user_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 0 8
----高水位已经降下来了。move到该表空间,需要保证有足够的剩余空间
----重建索引
shall@ORCL> alter index inx_t_x rebuild; Index altered. 或 alter index inx_t_x rebuild tablespace users;
----查看索引状态
SCott@test> set linesize 200 SCott@test> select index_name,table_name,tablespace_name,status from user_indexes;
----注意事项:
----Rebuild index
在对表进行move操作后,表中的rowid发生了改变,这样导致索引无法定位到原来表中的数据,从而触发了索引失效,所以需要alter index index_name rebuild [online]的命令进行重建。
----空间分配
alter table move操作,必须给move的表空间足够的剩余空间,否则可能会出现ORA-01652告警。
----exclusive lock
move操作相当于将表中所有数据移动,因此在move的过程中,oracle会对表放置了exclusive lock锁,此时只能对它进行select操作。
2. shrink space
此命令为Oracle 10g新增功能,shrink操作是将原本松散的数据存放结构,通过将表中靠后的行向前面的空闲块迁移,在完成后将完全空闲的区释放,并前置HWM到表中最后一个使用块的位置,从而实现松散表重新结构紧凑。
使用条件:
自动段管理模式。只支持ASSM管理的表空间,如果不是会报ORA-10635: Invalid segment or tablespace type
打开行移动 alter table table_name enable row movement
参数:
alter table TABLE_NAME shrink space [compact|cascate] alter table TABLE_NAME shrink space; --整理碎片并回收空间 alter table TABLE_NAME shrink space compact; --只整理碎片 不回收空间 alter table TABLE_NAME shrink space cascate; --整理碎片回收空间 并连同表的级联对象一起整理(比如索引)
使用步骤
1. alter table t1 enable ROW MOVEMENT;
2. shrink操作
3. alter table t1 disable ROW MOVEMENT;
----查看表空间段管理模式
sys@ORCL>select tablespace_name,block_size,extent_management,allocation_type,segment_space_management from dba_tablespaces order by segment_space_management; TABLESPACE_NAME BLOCK_SIZE EXTENT_MAN ALLOCATIO SEGMEN ------------------------------ ---------- ---------- --------- ------ SYSAUX 8192 LOCAL SYstem AUTO HCT 8192 LOCAL SYstem AUTO USERS 8192 LOCAL SYstem AUTO EXAMPLE 8192 LOCAL SYstem AUTO TEMP 8192 LOCAL UNIFORM MANUAL UNDOTBS1 8192 LOCAL SYstem MANUAL SYstem 8192 LOCAL SYstem MANUAL
sys@ORCL>select username,default_tablespace,temporary_tablespace from dba_users where username='SHALL'; USERNAME DEFAULT_TABLESPACE TEMPORARY_TABLESPACE ------------------------------ ------------------------------ ------------------------------ SHALL USERS TEMP
----创建表及插入数据
sys@ORCL>conn shall/shall Connected. shall@ORCL>create table shall(ttt int); Table created. sys@ORCL>begin 2 for i in 1..1000000 loop 3 insert into shall values(i); 4 end loop; 5 commit; 6 end; 7 / PL/sql procedure successfully completed. shall@ORCL>analyze table shall compute statistics; Table analyzed. shall@ORCL>select table_name,empty_blocks from user_tables where table_name='SHALL'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ SHALL 1630 34
----delete表shall
shall@ORCL>delete shall; 1000000 rows deleted. shall@ORCL>analyze table shall compute statistics; Table analyzed. shall@ORCL>select table_name,102)">----开始shrink整理碎片shall@ORCL>alter table shall enable row movement; Table altered. shall@ORCL>alter table shall shrink space; Table altered. shall@ORCL>alter table shall disable row movement; Table altered.----未刷新统计信息之前,高水位未降
shall@ORCL>select table_name,empty_blocks from user_tables where table_name='SHALL'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ SHALL 1630 34 shall@ORCL>analyze table shall compute statistics; Table analyzed. shall@ORCL>select table_name,empty_blocks from user_tables where table_name='SHALL'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ SHALL 1 7使用shrink降低高水位的优点:
1)能在线进行,不影响表上的DML操作,当然,并发的DML操作在shrink结束的时刻会出现短暂的block;
2)shrink的另外一个优点是在碎片整理结束后,表上相关的index仍然enable。
对于第二点进一步说明下,shrink在整理表碎片的时候,行的rowid已经发生改变,那为什么相关的索引还能enable呢?其实oracle在进行shrink的时候会对相应的索引进行维护,以保证index在shrink结束的时候index仍然有效。这个维护不同于索引rebuild,不会对索引的空间进行整理,shrink有cascede选项,如果在shrink的时候加上该选项,就会对表上相应的索引空间进行整理。ALTER TABLE tablename SHRINK SPACE CASCADE;
shrink也可以分两步进行
1)先执行ALTER TABLE tablename SHRINK SPACE compact,此时oracle会在高水位线以下将row尽量向segment的顶部移动,但不收缩高水位线,即不释放空间。这个操作对于那些在尝试读取已经被释放的块的查询是有益的。
2)然后在执行ALTER TABLE test SHRINK SPACE,此时第一步中的结果已经存储到磁盘,不会重新在整理碎片,只是收缩高水位,释放空间。第二步操作应该在系统不繁忙时候进行。
shrink的工作原理
shrink的算法是从segment的底部开始,移动row到segment的顶部,移动的过程相当于delete/insert操作的组合,在这个过程中会产生大量的undo和redo信息。
另外,对于空间的要求,shrink不需要额外的空间,move需要两倍的空间。
3. rename to
复制要保留的数据到临时表t,drop原表,然后rename to临时表t为原表
验证:
begin for i in 1..100000 loop insert into t2 values(i); end loop; commit; end; / analyze table t2 compute statistics; select table_name,empty_blocks from dba_tables where table_name='T2'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ T2 152 103 sql> delete t2; 100000 rows deleted. sql> create table t3 as select * from t2; sql> analyze table t2 compute statistics; sql> select table_name,empty_blocks 2 from dba_tables 3 where table_name='T2'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ T2 152 103 sql> drop table t2; sql> alter table t3 rename to t2; sql> analyze table t2 compute statistics; sql> select table_name,empty_blocks 2 from dba_tables 3 where table_name='T2'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ T2 1 64. exp/imp
用EXP导出后,删除原表/表空间,之后用IMP重新导入
shall@ORCL>create table zhong(id int); Table created. shall@ORCL>begin 2 for i in 1..1000000 loop 3 insert into zhong values(i); 4 end loop; 5 commit; 6 end; 7 / PL/sql procedure successfully completed. shall@ORCL>analyze table zhong compute statistics; Table analyzed. sys@ORCL> select table_name,empty_blocks from dba_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 1630 34----删除然后导出表
shall@ORCL>delete zhong where id>50000; 950000 rows deleted. [oracle@zyx ~]$ exp \'/ as sysdba\' tables=shall.zhong file=zhong.dmp log=zhong.log Export: Release 11.2.0.4.0 - Production on Sun May 1 18:34:39 2016 copyright (c) 1982,2011,Oracle and/or its affiliates. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning,OLAP,Data Mining and Real Application Testing options Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set About to export specified tables via Conventional Path ... Current user changed to SHALL . . exporting table ZHONG 50000 rows exported Export terminated successfully without warnings. [oracle@zyx ~]$----drop原表
shall@ORCL>drop table zhong; Table dropped.
----导入表
[oracle@zyx ~]$ imp \'/ as sysdba\' tables=zhong file=zhong.dmp fromuser=shall touser=shall; Import: Release 11.2.0.4.0 - Production on Sun May 1 18:37:44 2016 copyright (c) 1982,Data Mining and Real Application Testing options Export file created by EXPORT:V11.02.00 via conventional path import done in ZHS16GBK character set and AL16UTF16 NCHAR character set . importing SHALL's objects into SHALL . . importing table "ZHONG" 50000 rows imported Import terminated successfully without warnings. [oracle@zyx ~]$----未刷新统计信息时
sys@ORCL>select table_name,empty_blocks from dba_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 1630 0----刷新统计信息后
shall@ORCL>analyze table zhong compute statistics; Table analyzed. sys@ORCL>select table_name,empty_blocks from dba_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 110 1554---- BLOCKS列代表该表中曾经使用过得数据库块的数目,即水线。EMPTY_BLOCKS代表分配给该表,但是在水线以上的数据库块,即从来没有使用的数据块
5. deallocate unused
alter table table_name deallocate unused;注:这证明,DEALLOCATE UNUSED为释放HWM上面的未使用空间,但是并不会释放HWM下面的自由空间,也不会移动HWM的位置。
truncate table 后,有可能表空间仍没有释放,可以使用如下语句:
alter table 表名称 deallocate UNUSED KEEP 0;
例如:
alter table tablename deallocate UNUSED KEEP 0; 或者: truncate table tablename DROP STORAGE;才能释放表空间。
注意:如果不加KEEP 0的话,表空间是不会释放的。
实验:接上面导入导出实验
sys@ORCL>select table_name,102)">----开始整理sys@ORCL>alter table shall.zhong deallocate unused keep 0; Table altered. sys@ORCL>select table_name,empty_blocks from dba_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 110 1554 sys@ORCL>analyze table shall.zhong compute statistics; Table analyzed.----整理之后
sys@ORCL>select table_name,empty_blocks from dba_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 110 18
6. truncate
实验:接上面实验
sys@ORCL>select table_name,empty_blocks from dba_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 110 18 sys@ORCL>truncate table shall.zhong; Table truncated. sys@ORCL>analyze table shall.zhong compute statistics; Table analyzed. sys@ORCL>select table_name,empty_blocks from dba_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 0 128 sys@ORCL>alter table shall.zhong deallocate unused keep 0; Table altered. sys@ORCL>analyze table shall.zhong compute statistics; Table analyzed. sys@ORCL>select table_name,empty_blocks from dba_tables where table_name='ZHONG'; TABLE_NAME BLOCKS EMPTY_BLOCKS ------------------------------ ---------- ------------ ZHONG 0 24版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。