【无标题】oracle表分区自动创建与清理

oracle自动创建的分区表,在视图user_tab_partitions中可看但java不能直接用,得特殊处理。

S1: 创建处理函数high_value_to_date

create or replace function high_value_to_date(
    in_table_name varchar, 
    in_part_name varchar2
) return date as
vc_high_value varchar2(4000);
to_date_sql varchar2(4000);
result date;

begin
	select t.high_value into vc_high_value
	from user_tab_partitions t
	where t.table_name = in_table_name
	  and t.partition_name = in_part_name;
	
	to_date_sql := 'select ' || vc_high_value || ' from dual';
	execute immediate to_date_sql into result;
	return result;

end;

S2: 创建分区表

-- 按月自动创建分区
create table cccb_incall_log
(
	request_seq varchar2(32) not null,
    caller_request_seq varchar2(32),
    //...
    time_cost number(9),
    created_time date
)
partition by range(created_time) interval (numtoyminterval(1, 'month'))
(partition part_t1 value less than(to_date('20230101', 'yyyymmdd')));

S3: 清理分区表

查询分区,以及分区的数据范围日期上限

select 
	table_name, partition_name, 
	high_value_to_date(table_name, partion_name) high_value_date
from user_tab_partitions
where table_name = #{table_name}
order by 3 asc

清理分区并更新索引

-- 这里的动态sql,是mybatis那边动态拼接
alter table ${table_name} drop partition ${partition_name} update global indexes;

S4: 添加表分区清理登记簿

创建表cccb_table_part_clean_book

  • table_name 表名
  • data_keep_time 数据保持时间
  • time_unit 时间单位(D-天,M-月,Y-年)
  • last_clean_date 上次清理日期
  • last_clean_part_name 上次清理分区名
  • last_clean_part_high_value 上次清理的分区high_value

可以添加定时任务每天跑,根据配置表里面的表名 + 数据保持时间,检查数据是否有表分区需要清理。如果分区的数据已经超过数据有效时间,则使用上面S3来清理分区

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiaoFan012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值