需求:在个储存过程里有多个select多表联合查询,共同点是都联合到同一个表,我能不能做类似如下的优化(ps:这储存过程要被循环八百多万次,不优化不行):注意那个变量v_array
select acct_type into v_array from bonus_acct_type where is_use = '0'; select nvl(sum(a.xxx), 0)
into v_test_1
from 表1 a
where a.state = '00A'
and a.cust_id = 1102061249
and a.acct_type in (v_array); select nvl(sum(a.xxx), 0)
into v_test_2
from 表2 a
where a.state = '00A'
and a.cust_id = 1102061249
and a.acct_type in (v_array);……不知道我描述得清楚不。
select acct_type into v_array from bonus_acct_type where is_use = '0'; select nvl(sum(a.xxx), 0)
into v_test_1
from 表1 a
where a.state = '00A'
and a.cust_id = 1102061249
and a.acct_type in (v_array); select nvl(sum(a.xxx), 0)
into v_test_2
from 表2 a
where a.state = '00A'
and a.cust_id = 1102061249
and a.acct_type in (v_array);……不知道我描述得清楚不。
我是楼主,我的意思说,下面这条语句,会返回多行结果,我怎么把这多行结果赋给变量,再把这变量作为in参数:
select acct_type into v_array from bonus_acct_type where is_use = '0';
select nvl(sum(a.xxx), 0)
into v_test_1
from 表1 a
where a.state = '00A'
and a.cust_id = 1102061249
and a.acct_type in (select acct_type from bonus_acct_type where is_use = '0';
);
你建一个临时表
将结果插入到临时表里,其他表关联时,从临时表关联,这样数据量小很多了
要用动态sql就要循环数组进行拼接,你自己考虑。
那你可以创建一个 table of varchar2 的类型
create or replace type type_name is table of varchar2(4000);
把 select acct_type from bonus_acct_type where is_use = '0' 的结果保存到 type_name 这个对象里最后使用 and a.acct_type in (select column_value from table(type_name ))
set serveroutput on;
declare
vs_t1 varchar2(10);
l_last_num number(4);
type v_array is table of bonus_acct_type.acct_type%TYPE INDEX BY BINARY_INTEGER;
g_array v_array;
cursor cur_test is select acct_type from bonus_acct_type;
begin
open cur_test;
fetch cur_test bulk collect into g_array;
l_last_num := g_array.last;
dbms_output.put_line(to_char(l_last_num));
for l_loop_cntr in 1..l_last_num loop
select .. into vs_t1 from 表1 a where t1 in g_array(l_loop_cntr) and rownum = 1;
dbms_output.put_line(g_array(l_loop_cntr));
end loop;
close cur_test;
end;
不知道条件是否允许,用C或者JAVA分多线程调用存储过程。
你可能会想:即使是多线程跑存储过程,操作的也是同一张表。。
但是:可以在C或JAVA调用存储过程的时候,把线程号做为参数传到存储过程中,每个线程处理1-10000或者10000-20000号的数据。
这样会加快速度。我们项目中有这样的例子。
opps_zhou和BenChiM888,谁回一下啊?
或者 采用多线程插入 百度一下:oracle parallel
那非常没必要每个ID对存储过程调用,应该合并到这里面来
业务逻辑不能改,实现方式可以换下用临时表,如果必须800万次,可以看下在一个800万循环内,你打算放到数组中的数据是否都一样?一样的话可以用全局临时表或固化表,在第一次执行存储过程前初始化