ORACLE 历史数据管理策略--数据清理
背景由于性能数据每天导入量,数据库表空间每天增长很快,且不需要太长的保存周期,为避免爆表,因此需要定制定期清理计划。 分区方案目前有两种方案,一种是按照ingerval分区,未定义分区oracle会智能分区,分区简单,但是带来的问题就是分区名字无法直接确定,后期维护不方便 关于索引表分区以后,同时需要同步修改索引,这里根据我们的应用场景,需要构建LNP(LOCAL NON PREFIXED) INDEX--引入的虚拟列作为分区字段,没有其它功能。 select INDEX_NAME,PARTITIONING_TYPE,LOCALITY,ALIGNMENT from all_part_indexes where table_name='xxx' select index_name,status from user_indexes where index_name='xxx' select INDEX_NAME,PARTITION_NAME,status from User_Ind_Partitions a where a.Index_Name='xxx' 新增虚拟列新增虚拟列语法v_month as (substr(datadate,6,2)) partition by list(v_month) ( partition p1 values('01'),partition p2 values('02'),partition p3 values('03'),partition p4 values('04') ); 新增虚拟列不会增加存储空间消耗,但是会增加CPU消耗,即新增列的信息仅写入metadata. SELECT TABLE_NAME,HIGH_VALUE FROM user_tab_partitions WHERE TABLE_NAME= select TABLE_NAME,PARTITIONING_TYPE from user_part_tables where table_name=' select segment_name||' '||partition_name||' '||segment_type from user_segments where segment_name like 应用程序变化SELECTSELECT * 会现实虚拟列 INSERT不支持 insert into table xx values() 需要显式指定插入列: insert into table xx(col1,col2,...) values() update同insert 按月份分区数据清理表按照月分区,共12个分区,数据保留3个月,每个月出清理三个月之前的分区数据,即清理脚本每月执行 from datetime import date,timedelta from monthdelta import MonthDelta current_day = date.today() prev_2month = current_day- MonthDelta(2) month_of_partition = prev_2month.month print 'current day is:{0} and previous day of last 2 months is:{1},so the partition need to truncate is:{2}'.format(current_day,prev_2month,month_of_partition) with open("partition_by_day_table") as f: for table in f: print 'alter table {0} truacate partition p{1}'.format(table.strip(),month_of_partition) 确定分区后,通过定时任务执行对应的SQL即可。 按天分区数据清理表按照天分区,数据至少保留7天以上 #!/usr/bin/python from datetime import date,timedelta,datetime current_day = date.today() prev_8day = current_day-timedelta(days=8) day_of_partition = prev_8day.day print 'current day is: {0} and previsus day of 8 day is:{1},so the partition need to trucate is:{2}'.format(current_day,prev_8day,day_of_partition) print '#'*72 fout=open('/home/oracle/scripts/minute.log','a') with open("/home/oracle/scripts/partition_by_day_tables") as f: for table in f: syntax= 'alter table {0} truacate partition p{1}'.ljust(72,' ').format(table.strip(),day_of_partition)+'; commit;n' #print syntax fout.write(syntax) now=datetime.now().strftime('%Y-%m-%d %H:%M:%S') fout.write(now+'n') f.close() print '#'*72 对应的SQL脚本如下: alter table xx1 truacate partition p3 ; commit; alter table xx2 truacate partition p3 ; commit; alter table xx3 truacate partition p3 ; commit; 确定分区后,通过定时任务执行对应的SQL即可。 定时脚本通过crontab定时任务完成 5 4 * * * --daily 5 4 1 * * ---monthly (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |