热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->oracle 
站内搜索:
在ORACLE里用存储过程定期分割表(2)
作者:不详 来源:互联网 整理日期:2007-8-17

 if c2%notfound and c1%found then

  query_str :=alter table ||source_name1|| rename to ||target_name1;

  execute immediate query_str;

  dbms_output.put_line(rename success!);

  end if;

  close c1;

  close c2;

  exception

  WHEN OTHERS THEN

  times:=times+1;

  if times<100 then

  -- dbms_output.put_line(times:||times);

  rename_table(source_name1,target_name1,times);

  else

  dbms_output.put_line(SQLERRM);

  dbms_output.put_line(error over 100 times,exit);

  end if;

  end;

  /

  截断分割log表的存储过程log_history:

  create or replace procedure log_history

  is

  query_str varchar2(32767);

  year_month varchar2(8);

  times number;

  begin

  select to_char(sysdate-15,YYYYMM) into year_month from dual;

  times:=0;

  query_str :=create table log_new pctfree 10 pctused 80

  as select * from log where 1=2;

  execute immediate query_str;

  query_str :=alter table log_new add constraints log_||year_month||_pk

  primary key (id) tablespace indx nologging pctfree 10;

  execute immediate query_str;

  query_str :=alter table log_his modify logtime default sysdate;

  execute immediate query_str;

  query_str :=create index log_||year_month||_logtime on log(logtime)

  tablespace indx nologging pctfree 10;

  execute immediate query_str;

  rename_table(log,log||year_month,times);

  query_str :=alter table log_new rename to log;

  execute immediate query_str;

  end;

  /

  当然您工作环境的日志表可能和 这个做例子的日志表结构上有所不同,约束条件、索引和默认值都不尽相同。

  只要稍加修改就可以了。

  三、用户需要有create any table系统权限(不是角色里包含的权限)

  因为在执行存储过程时,由角色赋予的权限会失效, 所以执行log_history的用户一定要有DBA单独赋予的

  create any table系统权限。

  最后在OS里定时每月一号凌晨0:00分执行log_history,让存储过程定期分割表。

  如果要分割的日志表很多,模仿log_history可以写很多类似的存储过程来分割不同项目里的日志表。

  然后让OS按月,按周或者不定期地执行这些存储过程, 管理员只要查看日志就可以了。

  四、其它注意事项

  如果应用程序有BUG,可能对在用原始日志表产生长期不能释放的锁,执行log_history重命名会不成功。

  这时DBA可以查看数据字典:

  select object_id,session_id,locked_mode from v$locked_object;

  select t2.username,t2.sid,t2.serial#,t2.logon_time

  from v$locked_object t1,v$session t2

  where t1.session_id=t2.sid order by t2.logon_time;

  如果有长期出现的一模一样的列(包括登录时间),可能是没有释放的锁。

  们要在执行分割日志表的存储过程前,用下面SQL语句杀掉长期没有释放非正常的锁:

  alter system kill session sid,serial#;

  五、结束语

  用上面介绍的存储过程定期分割日志表有很大的灵活性。历史数据不仅查询方便,转移和备份起来也都很容易。

  Unix和Windows平台的都可以使用。对服务器硬盘空间较小的中小型公司意义尤其明显。

[1]  [2]  
相关文章