I have a sql query that pulls about 26000 rows in about 3 min. I have added another column that calls a pretty simple function, but it changes the efficiency to about 100 rows/2 minutes. I really need to add three columns using the same function, but at this rate, it would run all day. The function is as follows
create or replace function customers_x(v_master_id number,
p_product_id varchar2,
n number)
return varchar2
is
v_no_of_records number := 0;
v_current_rec number := 0;
cursor x is
select m.product_id
from miccom1.ensembles_prod_rec e, acntv.master_sku m
where m.master_id = e.master_id2
and e.master_id1 = v_master_id
order by e.affinity_score desc,master_id2 asc;
begin
for rec_index in x loop
v_no_of_records := v_no_of_records + 1;
end loop;
if v_no_of_records < n then
return 'NULL';
else
for rec_index in x loop
v_current_rec := v_current_rec + 1;
if v_current_rec = n then
return rec_index.product_id;
end if;
end loop;
end if;
end;
basically, master_id is indexed in the table that is passing that variable, product_type is not used(will be in future function), and n is the number of the column I added. (ie would pass 1 to get first record, 2 to get second record for second additional column ...etc) The select statement itself pulls incredibly fast when providing a master_id. Each master_id could have 0-10 product_ids. Everything works fine, it is just a complete resource hog. Is there anything I can do to help speed this up.
Thank you in advance for your help.
create or replace function customers_x(v_master_id number,
p_product_id varchar2,
n number)
return varchar2
is
v_no_of_records number := 0;
v_current_rec number := 0;
cursor x is
select m.product_id
from miccom1.ensembles_prod_rec e, acntv.master_sku m
where m.master_id = e.master_id2
and e.master_id1 = v_master_id
order by e.affinity_score desc,master_id2 asc;
begin
for rec_index in x loop
v_no_of_records := v_no_of_records + 1;
end loop;
if v_no_of_records < n then
return 'NULL';
else
for rec_index in x loop
v_current_rec := v_current_rec + 1;
if v_current_rec = n then
return rec_index.product_id;
end if;
end loop;
end if;
end;
basically, master_id is indexed in the table that is passing that variable, product_type is not used(will be in future function), and n is the number of the column I added. (ie would pass 1 to get first record, 2 to get second record for second additional column ...etc) The select statement itself pulls incredibly fast when providing a master_id. Each master_id could have 0-10 product_ids. Everything works fine, it is just a complete resource hog. Is there anything I can do to help speed this up.
Thank you in advance for your help.