暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 每日插入,在表格上删除

askTom 2017-03-06
254

问题描述

我有一个表tb_card_data

卡 _ 无类型电子邮件模块创建 _ 在seq _ 否
-
12345 C x@a.com MOD_1 06/03/2017 20170306000001 (创建序列号)
..
所以模块-MOD_1的大约500k记录

模块-MOD_2的大约500k记录
模块-MOD_3的大约500k记录

我们需要在CREATED_ON & SEQ_NO的基础上处理MOD_1的所有记录。
处理完成后,我们只需要从上表中删除MOD_1的记录。

索引是在CREATED_ON列上创建的。

既然我们每天都在表上插入/删除,我们是否需要处理高水位线或收缩表等问题??

专家解答

这取决于您如何插入数据。如果使用常规插入/删除,新行将低于高水位标记 (如果有空格):

create table t as
  select sysdate dt, a.* from all_objects a
  where  1=0;
  
insert into t
  select trunc(sysdate) dt, a.* from all_objects a;
  
commit;

select bytes from user_segments
where  segment_name = 'T';

BYTES       
11,763,712 

delete t
where  dt = trunc(sysdate);

commit;

insert into t
  select trunc(sysdate)+1 dt, a.* from all_objects a;
  
commit;

select bytes from user_segments
where  segment_name = 'T';

BYTES       
11,763,712  

delete t
where  dt = trunc(sysdate)+1;

commit;

select bytes from user_segments
where  segment_name = 'T';

BYTES       
11,763,712  
复制


所以空间被重复使用,没有必要回收它。

但是,如果使用insert /* append */,则新行将始终高于高水印。因此,即使行数是恒定的,您的表的大小也会不断增长!

insert /*+ append */into t
  select trunc(sysdate) dt, a.* from all_objects a;
  
commit;

select bytes from user_segments
where  segment_name = 'T';

BYTES       
26,304,512  

delete t
where  dt = trunc(sysdate);

commit;

insert /*+ append */into t
  select trunc(sysdate)+1 dt, a.* from all_objects a;
  
commit;

select bytes from user_segments
where  segment_name = 'T';

BYTES       
39,411,712  

delete t
where  dt = trunc(sysdate)+1;

commit;

select bytes from user_segments
where  segment_name = 'T';

BYTES       
39,411,712  
复制


在这种情况下,您需要使用收缩或移动来回收空间。或者只是不使用append ;)

如果您要添加的数据量随着时间的推移而缩小,您可能还需要恢复空间。例如,从500k/day -> 400k/day -> 300k/day -> 等开始。在这种情况下,表仍将消耗前500k行所需的空间。但是通常数据会随着时间的推移而增长,所以这很少是一个问题!

如果您总是按模块和日期访问行,则可能需要探索另一种选择:

分区!

(假设您已为此获得许可)。

通过CREATED_ON分区和通过模块进行子分区 (反之亦然) 使您可以加载数据并从 (子) 分区中读取所需内容。然后通过截断 (子) 分区将其擦除。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论