IT分销/经销数据库

怎么解决定期删除大表的数据

db2v9.7
现在库里有几张以日期命名的表,从0101 到1231日 365张表
数据保留90天左右,那么超过90天的表需要删除。

这样类型的表有4张,大家有什么好的方法来做这个事情!  每张表每天大概300万的数据量,以后会逐步增加

我自己想到的几个方法:
方法一:
delete table    我担心大表delete 太慢,会影响其他事务处理
reorge table

方法2:
truncate table immediate; 但是在储存过程中调用这个的时候报错 SQL0428N
查询了一下原因 ,大概意思是说 执行这个命令前 需要 关闭游标(储存过程中就是用游标把表名选择出来的,关闭游标,那么就无法循环了)

方法3:
alter table activate not logged initially with empty table;        --把表变成 not logged ,怎么把表变回logged 呢?
reorg table table_name    ;

想征求一个比较好的方法 ,我在一个表里维护了要删除表的信息
t_delete_manage表里维护了一下信息
TNAME(不好意思,表名替换一下)                 DELETE_COL                     DELETE_PERIOD      
a                                                 BUSINESSDAY                                      90
b                                                 BUSINESSDAY                                      90
c                                                BUSINESSDAY                                      90
d                                                 BUSINESSDAY                                      90
e                                                 FILE_RECEIVED_TIME                               90
f                                              FILE_RECEIVED_TIME                               90
根据这个表里的信息,组成sql 进行 删除操作
我写了这么个储存过程
CREATE OR REPLACE  PROCEDURE  CRB_TEST
()
LANGUAGE SQL
BEGIN
    declare      sqlcode         integer default 0; --
    declare      v_notfound       integer default 0; --
    declare      v_tname          varchar(50);--
    declare      v_delete_col     varchar(30); --
    declare      v_delete_rows    integer;--
    declare      v_delete_log     varchar(100);--
    declare      v_delete_period  integer;--
    declare      sqlStr           varchar(300);--
    declare      condition1       varchar(100);--
    declare      n_is_partition  integer;--
    declare fetchSeqCursor cursor for
            select tname,delete_col,delete_period,IS_PARTITION from tsy_delete_manage order by tname;--
  
    declare  CONTINUE  handler for NOT FOUND set v_notfound = 100; --
    set  v_notfound = 0;--
    open fetchSeqCursor;--
    fetch fetchSeqCursor into v_tname, v_delete_col,v_delete_period,n_is_partition;--
    WHILE  v_notfound=0 DO
   
              set sqlStr='truncate table  '||v_tname||'_'||to_char(current_date-90 days,'mm')||to_char(current_date-90 days,'dd')||' IMMEDIATE';--
              execute immediate sqlStr ;--
              
    set  v_notfound = 0;--
    fetch fetchSeqCursor into v_tname, v_delete_col,v_delete_period,n_is_partition;--
    end WHILE;--
    close fetchSeqCursor;--
END
commit;

call crb_test
SQL0428N  只允许将该 SQL 语句作为工作单元的第一条语句。  SQLSTATE=25001
大家有什么好的方法赐教一下 ,谢谢了
参与26

25同行回答

weiruan85weiruan85数据库管理员ibm
IMMEDIATESpecifies that the truncate operation is processed immediately and cannot be undone. The statement must be the first statement in a transaction (SQLSTATE 25001). The truncated table is immediately available for use in the same unit of work. ...显示全部
IMMEDIATE
Specifies that the truncate operation is processed immediately and cannot be undone. The statement must be the first statement in a transaction (SQLSTATE 25001).
The truncated table is immediately available for use in the same unit of work. Although a ROLLBACK statement is allowed to execute after a TRUNCATE statement, the truncate operation is not undone, and the table remains in a truncated state. For example, if another data change operation is done on the table after the TRUNCATE IMMEDIATE statement and then the ROLLBACK statement is executed, the truncate operation will not be undone, but all other data change operations are undone.

share 一下truncate 在存储过程里边应该注意的地方。收起
政府机关 · 2012-03-02
浏览1134
weiruan85weiruan85数据库管理员ibm
WHILE  v_notfound=0 DO                set sqlStr='delete   '||v_tname||'_'||to_char(current_date-90 days,'mm')||to_char(current_date-90 days,'dd');--       ...显示全部
WHILE  v_notfound=0 DO
   
             set sqlStr='delete   '||v_tname||'_'||to_char(current_date-90 days,'mm')||to_char(current_date-90 days,'dd');--
              execute immediate sqlstr ;--
              set sqlstr='reorg table '||v_tname||'_'||to_char(current_date-90 days,'mm')||to_char(current_date-90 days,'dd');--
              call SYSPROC.ADMIN_CMD(sqlstr);--
    set  v_notfound = 0;--
    fetch fetchSeqCursor into v_tname, v_delete_col,v_delete_period,n_is_partition;--
    end WHILE;--
    close fetchSeqCursor;--

有可能是while 最后一步拼sql的时候拼的出现了问题,然后报了这个错,
SYSPROC.ADMIN_CMD 调用reorg 没有一点问题。兄好好调试下存储过程收起
政府机关 · 2012-03-02
浏览1132
weiruan85weiruan85数据库管理员ibm
回复 9# wp28556259      通常的做法是 先detach  ,然后集中到一个时间点进行drop显示全部
回复 9# wp28556259


     通常的做法是 先detach  ,然后集中到一个时间点进行drop收起
政府机关 · 2012-03-02
浏览1126

    提问者

    nowhill
    系统工程师

    相关问题

    相关资料

    相关文章

    问题状态

  • 发布时间:2012-03-02
  • 关注会员:1 人
  • 问题浏览:12249
  • 最近回答:2012-03-03
  • X社区推广