我们设计了一个自动分区的超级大表,分区采用以时间分区的方式,结果数据爆掉了,没办法必须删除其中比较早的分区数据,我设计了一个存储过程完成这个过程,
create or replace procedure ICE_Drop_PARTITIONS(dateBefore in Date, tableName in varchar2) as
partitionName varchar2(20);
v_sql varchar2(1024);
temp varchar2(1024);
temp_D date;
v_temp varchar2(1024);
v_cs varchar2(1024);
v_error varchar2(1024);
CURSOR cur_partition IS
select t.partition_name,t.high_value from user_tab_partitions t where t.table_name=tableName order by t.last_analyzed desc;
begin
for for_partition in cur_partition loop
begin
v_temp := 'select to_char('||for_partition.high_value||',''yyyy-MM-dd'') from dual';
execute immediate v_temp into temp ;
select to_date(temp,'yyyy-mm-dd') into temp_D from dual;
if (temp_D<dateBefore) then
v_sql := 'alter table '||tableName||' truncate partition ' ||for_partition.partition_name;
Dbms_Output.put_line(v_sql);
execute immediate v_sql;
commit;
end if;
end;
end loop;
close cur_partition;
exception when others then
v_error := sqlcode || ' - ' || sqlerrm;
dbms_output.put_line(v_error);
rollback;
end ICE_Drop_PARTITIONS;
dateBefore in Date, tableName in varchar2
这两个参数分别是,
dateBefore :删除几号以前的数据,
tableName :分区所在的表
其中用到了游标:
CURSOR cur_partition IS
select t.partition_name,t.high_value from user_tab_partitions t where t.table_name=tableName order by t.last_analyzed desc;
这个是遍历user_tab_partitions ,找到指定表名的所有分区,
v_temp := 'select to_char('||for_partition.high_value||',''yyyy-MM-dd'') from dual';
execute immediate v_temp into temp ;
select to_date(temp,'yyyy-mm-dd') into temp_D from dual;
这个是找到分区的条件,实质就是for_partition.high_value就是一个
类似这种的存在,他是一个long型,所以把它转化为date,后面就是比较日期,
v_sql := 'alter table '||tableName||' truncate partition ' ||for_partition.partition_name;
Dbms_Output.put_line(v_sql);
execute immediate v_sql;
这就是最后删除分区的代码...