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来清理分区