联系我们

  服务热线:
  • 杭 州 :400-889-1122
  • 上 海 :400-899-1122
  • 深 圳 :0755-88848278
  • 宁 波 :0574-87298883
  • 广 州 :400-889-1122
  • 温 州 :0577-88844623
数据恢复品牌
  • NAS服务器
  • IBM NAS HP NAS SUN NAS EMC NAS Dell NAS 联想 NAS 清华同方NAS 浪潮NAS 方正 NAS
  • 硬盘系列
  • 易拓(Excelstor) 昆腾(Quantum) 三星(SAMSUNG) 日立IBM(HITACHI) 希捷(Seagate) 迈拓(Maxtor) 西部数据(WD) 东芝(TOSHIBA) 富士通(FUJITSU)
  • 服务器系列
  • IBM系列 HP 系列 Dell系列 Sun系列 联想系列 康柏系列 浪潮系列 长城系列 曙光系列
  • 文件系统
  • windows系列 Solaris系列 Novell系列 IBM-AIX系列 MAC系列 Linux系列 SCO系列 FreeBSD系列 HP-UX系列
当前位置:技 术首页 >> Raid服务器数据库 >> 在ORACLE里用存储过程定期分割表
在ORACLE里用存储过程定期分割表
2006-09-29 05:52:42  作者:  来源:  浏览次数:686  文字大小:【】【】【
google_ad_client = "pub-5920153652866057";google_ad_width = 336;google_ad_height = 280;google_ad_format = "336x280_as";google_ad_type = "text_image";google_ad_channel = "";google_color_border = "FFFFFF";google_color_bg = "FFFFFF";google_color_link = "CC0000";google_color_text = "000000";google_color_url = "7F7F7F";
Oracle数据库里存放着各种各样的数据,其中有一些数据表会随着时间的推移,越来越大。如交友聊天的日志、短信收发的日志、生产系统的日志、动态网站发布系统的日志等等。这样的信息又和时间紧密相关,有没有办法让这些日志表能按时间自动分割成历史年月(如log200308,log200309)的表呢? 请看看用存储过程定期分割表的方法吧。

一、问题的引出

  1.初学数据库时只知道用delete来删除表里的数据。但在Oracle数据库里,大量delete记录后,并不能释放表所占用的物理空间,这里面有一个高水位的概念,所以我们不能用delete来分割表。

  2.用重命名(rename)表的方法

   (1) 先建一个和原来日志表(假如是log)数据结构一模一样的新表(如log_new),建约束、索引及指定字段的默认值;

   (2) 重命名表log到log_YYYYMM;

要注意的问题是OLTP系统可能会因为DML操作阻碍重命名执行成功,出现ORA-00054资源正忙的错误提示,需要试多次才能成功。

   (3) 重命名表log_new到log。

  这样应用程序不用修改(受影响的时间仅几秒钟),日志表就被截断分割了。

  上述步骤可以在Oracle里用存储过程来实现。

二、用存储过程来分割表

  可以看到在重命名表的方法中,步骤(2)是个关键。下面这个rename_table过程会在有锁阻碍的情况下用递归的方式重试100次。

重命名原始表到目标表的存储过程rename_table:

create or replace procedure rename_table
(source_name	in	varchar2,
target_name	in 	varchar2,
times		in out	number)
  is
query_str  varchar2(4000);
source_name1  varchar2(64);
target_name1  varchar2(64);
cursor c1 is select segment_name from user_segments 
 where segment_name=upper(source_name);
dummy c1%rowtype; 
cursor c2 is select segment_name from user_segments
 where segment_name=upper(target_name);
dummy2 c2%rowtype; 
begin
source_name1:=source_name;
target_name1:=target_name;
open c1;
fetch  c1  into   dummy;
--   if c1%found then
--   dbms_output.put_line(source_name1||'exist!');
--   end if;
open c2;
fetch  c2  into   dummy2;
--   if c2%notfound then
--   dbms_output.put_line(target_name1||'not exist!');
--   end if;
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,'YYYYMMDD') 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;
/


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

本新闻共2页,当前在第1页  1  2  

友情链接 | SQL数据库恢复 | 硬盘数据恢复 | Raid服务器数据恢复 | 公司简介 | 数据恢复