使用Merge into优化一个垃圾存储过程
2013-12-22 21:43
399 查看
开发人员反应下面存储过程出结果太慢,表和字段名做了处理
declare
cursor c1 is
select * from user1.tab1;
v_count number:=0;
begin
for v1 in c1 loop
select count(1) into v_count
from user1.tab2
where row_id=v1.row_id;
if v_count=0 then
INSERT INTO user1.tab1
(row_id,
col_1,
col_2,
col_3,
col_4,
col_5,
....
/* 这里有很多字段 */)
VALUES
(v1.row_id,
v1.col_1,
v1.col_2,
v1.col_3,
v1.col_4,
v1.col_5,
.....);
else
UPDATE user1.tab1
SET col_1 = v1.col_1,
col_2 = v1.col_2,
col_3 = v1.col_3,
col_4 = v1.col_4,
col_5 = v1.col_5,
.....
WHERE row_id = v1.row_id;
end if;
commit;
end loop;
end;
分析存储过程,就可以知道开发人员的目的是想从tab2里面查找和tab1里面通过row_id关联匹配的条件,不匹配inset,匹配update,这种过程太TM坑爹了,游标里面传入一个值就要把tab2 QJ一次,这里QJ了3千多万次,像这样的例子完全可以通过Merge into来改写,而且是最最最简单的merge基本用法。
merge into user1.tab1 a
using user1.tabl2 v1
on (v1.row_id=a.row_id)
when matched then
UPDATE
SET a.col_1 = v1.col_1,
a.col_2 = v1.col_2,
a.col_3 = v1.col_3,
a.col_4 = v1.col_4,
a.col_5 = v1.col_5,
.....
when not matched then
INSERT
(a.col1,
a.col2,
a.col3,
a.col4,
a.col5
.....)
VALUES
(v1.row_id,
v1.col_1,
v1.col_2,
v1.col_3,
v1.col_4,
v1.col_5
......);
declare
cursor c1 is
select * from user1.tab1;
v_count number:=0;
begin
for v1 in c1 loop
select count(1) into v_count
from user1.tab2
where row_id=v1.row_id;
if v_count=0 then
INSERT INTO user1.tab1
(row_id,
col_1,
col_2,
col_3,
col_4,
col_5,
....
/* 这里有很多字段 */)
VALUES
(v1.row_id,
v1.col_1,
v1.col_2,
v1.col_3,
v1.col_4,
v1.col_5,
.....);
else
UPDATE user1.tab1
SET col_1 = v1.col_1,
col_2 = v1.col_2,
col_3 = v1.col_3,
col_4 = v1.col_4,
col_5 = v1.col_5,
.....
WHERE row_id = v1.row_id;
end if;
commit;
end loop;
end;
分析存储过程,就可以知道开发人员的目的是想从tab2里面查找和tab1里面通过row_id关联匹配的条件,不匹配inset,匹配update,这种过程太TM坑爹了,游标里面传入一个值就要把tab2 QJ一次,这里QJ了3千多万次,像这样的例子完全可以通过Merge into来改写,而且是最最最简单的merge基本用法。
merge into user1.tab1 a
using user1.tabl2 v1
on (v1.row_id=a.row_id)
when matched then
UPDATE
SET a.col_1 = v1.col_1,
a.col_2 = v1.col_2,
a.col_3 = v1.col_3,
a.col_4 = v1.col_4,
a.col_5 = v1.col_5,
.....
when not matched then
INSERT
(a.col1,
a.col2,
a.col3,
a.col4,
a.col5
.....)
VALUES
(v1.row_id,
v1.col_1,
v1.col_2,
v1.col_3,
v1.col_4,
v1.col_5
......);
相关文章推荐
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- Oracle 10g R2不能使用EM的问题
- PreparedStatement中in子句的处理
- VMware下RedHat4.8_64位安装Oracle 10g RAC--简略脚本
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- 数据库自动备份脚本
- 解决oracle用户连接失败的解决方法
- oracle的一些tips技巧
- Oracle 下的开发日积月累
- Oracle存储过程之数据库中获取数据实例
- Windows下ORACLE 10g完全卸载的方法分析
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ORACLE LATERAL-SQL-INJECTION 个人见解
- Oracle Connect to Idle Instance解决方法
- oracle sys_connect_by_path 函数 结果集连接
- Oracle捕获问题SQL解决CPU过渡消耗
- oracle dba 应该熟悉的命令
- Oracle11.2 命令行手工最简创建数据库的过程
- Oracle 数据库自动存储管理-安装配置
- 基于oracle中锁的深入理解