记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

Oracle 10g r2 有大量表,删除一部分数据后,需要释放高水位表。


首先创建一个表 gps_sq ,导入需要压缩的表名.

# : create table gps_sq as select rownum  sq,table_name from user_tables ;

创建了一个存储过程,分批执行压缩表。


create or replace procedure p_shrink_space(a in number,b in number) is

  t_sql varchar2(2000);
  t_sql1 varchar2(2000);

  t_sql2 varchar2(2000);

   t_sql3 varchar2(2000);

begin
  for c in (select table_name from gps_sq t where  sq>=a and sq <=b) loop
        t_sql := 'delete from ' ||c.table_name|| ' where to_char(t.location_time,'||'''yyyymmdd'''||')<'||'''20150531''';

        t_sql1 :='alter table '||c.table_name||' enable row movement';

     t_sql2 :='alter table '||c.table_name||' shrink space compact';

        t_sql3 :='alter table '||c.table_name||' shrink space';

     execute immediate t_sql;

     commit;

    execute immediate t_sql1;

    execute immediate t_sql2;

    execute immediate t_sql3;

  end loop;

end p_shrink_space;



编写执行脚本,使能后台运行自动运行,并记录日志。

#: vi  p_shrink_space.sh

添加以下内容:


#!/bin/sh
# shrink  tablespace
echo "" > /data/shrink_gps_table.log
su - oracle -c "sh /data/pshrinkgps.sh"  >> /data/shrink_gps_table.log

编写执行过程的脚本。

#: vi pshirnkgps.sh

添加以下内容

sqlplus /nolog <<EOF
connect twohappy/twohappy
set timing on
Select to_char(sysdate,'yyyymmdd HH24:mi:ss') from dual;
execute p_shrink_space(1,5);
execute p_shrink_space(6,10);
execute p_shrink_space(11,15);
execute p_shrink_space(16,20);
Select to_char(sysdate,'yyyymmdd HH24:mi:ss') from dual;
exit;
EOF
exit


编写完后,可以登陆linux系统。

执行后台命令,来实现压缩表。

#: /data/p_shrink.sh &



转载请标明出处【Oracle 存储过程压缩表释放高水位表空间】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论