现有三个表
TBL_A
ID //非自增字段
NAME
_____________
TBL_B
ID //自增字段
AID //对应TBL_A的ID
NAME
___________
TBL_C
ID //自增字段
BID //对应TBL_B的ID
NAME
ID均为自增的字段,三个表均有一个与之对应的历史表,符合一定条件的数据会被移动到对应的三个历史表中,TBL_B的ID字段在数据移动后会发生变化,此时要更新TBL_C中相应的BID,现在我采用的是游标,TBL_A中有10000条,对应每条记录B中有10条,C中10条共有22万条记录,此时采用游标已经慢的无法忍受了。
请问各位有其他什么方法啊。
create or replace procedure test
is
m_bid number;
cursor a_cursor is select id from tbl_a
a_id tbl_a.id%type;
cursor b_cursor(m_id number) is select id from tbl_b where aid=m_id;
b_id tbl_b.id%type;
begin
open a_cursor;
loop
fetch a_cursor into a_id;
exit when a_cursor;
insert into tbl_a_history (id,name) (select id,name from tbl_a);
open b_cursor(a_id);
loop
fetch b_cursor into b_id;
exit when b_cursor;
insert into tbl_b_history (name,aid) (select name,aid from tbl_b where id=b_id);
select seq_b.currval into m_bid from dual;
insert into tbl_c_history (name,bid) (select name,m_bid from tbl_c where bid=b_id);
end loop;
close b_cursor;
end loop;
close a_cursor;
commit;
exception when others then
rollback;
end;
TBL_A
ID //非自增字段
NAME
_____________
TBL_B
ID //自增字段
AID //对应TBL_A的ID
NAME
___________
TBL_C
ID //自增字段
BID //对应TBL_B的ID
NAME
ID均为自增的字段,三个表均有一个与之对应的历史表,符合一定条件的数据会被移动到对应的三个历史表中,TBL_B的ID字段在数据移动后会发生变化,此时要更新TBL_C中相应的BID,现在我采用的是游标,TBL_A中有10000条,对应每条记录B中有10条,C中10条共有22万条记录,此时采用游标已经慢的无法忍受了。
请问各位有其他什么方法啊。
create or replace procedure test
is
m_bid number;
cursor a_cursor is select id from tbl_a
a_id tbl_a.id%type;
cursor b_cursor(m_id number) is select id from tbl_b where aid=m_id;
b_id tbl_b.id%type;
begin
open a_cursor;
loop
fetch a_cursor into a_id;
exit when a_cursor;
insert into tbl_a_history (id,name) (select id,name from tbl_a);
open b_cursor(a_id);
loop
fetch b_cursor into b_id;
exit when b_cursor;
insert into tbl_b_history (name,aid) (select name,aid from tbl_b where id=b_id);
select seq_b.currval into m_bid from dual;
insert into tbl_c_history (name,bid) (select name,m_bid from tbl_c where bid=b_id);
end loop;
close b_cursor;
end loop;
close a_cursor;
commit;
exception when others then
rollback;
end;
解决方案 »
- 对这段代码不理解
- oracle数据库存储过程,公式计算的问题
- 复合索引和一般索引的问题
- 这个在oracle存储过程中用什么实现?
- 下载了Oracle 9i后不知怎样安装?
- sqlserver2000链接Oracle9.02 表中包含BLOB类型字段,执行SELECT操作出错(不包含BLOB字段)
- oracle8i中sql查询的问题。
- 在表A中有一字段A存有数据“a;b;c;d;e”,想写一个触发器将该条记录分散成五条记录(将字段A根据;折分)插入另一表B中,但不知在数据库中
- 请教 多表查询 的问题,非常感谢
- oracle 同一个ID 有多条记录,怎么取时间最大的那一条
- 为什么大家都不用inner join,left join
- 如何写这样的sql语句?查询某一时间字段超过n小时后的记录
create or replace procedure test
is
m_bid number;
begin
insert into tbl_a_history (id,name) (select id,name from tbl_a);
select seq_b.currval into m_bid from dual;
insert into tbl_b_history (name,aid)
(select t.name,t.aid from tbl_b t,tbl_a tt where tt.id=t.aid);
insert into tbl_c_history (name,bid)
(select name,m_bid from tbl_c where bid>m_bid);
commit;
exception when others then
rollback;
end;
/另外,下面这一段没看懂
loop
fetch a_cursor into a_id; //打开游标
exit when a_cursor;
insert into tbl_a_history (id,name) (select id,name from tbl_a);//重复保存tbl_a的数据???在循环内
...
insert into tbl_a_history (id,name) (select id,name from tbl_a);
少了条件"where id=a_id",这样在循环体内这个历史表的记录数暴增...
修改一下:
insert into tbl_a_history (id,name) (select id,name from tbl_a where id=a_id);
select seq_b.currval into m_bid from dual; --取出插入数据前的序号
insert into tbl_b_history (name,aid) --插入数据
(select t.name,t.aid from tbl_b t,tbl_a tt where tt.id=t.aid);
insert into tbl_c_history (name,bid)
(select name,m_bid from tbl_c where bid>m_bid); --根据刚刚插入的数据进行处理(注意:是bid>m_bid 取出的是新数据)