I need to optimize a PL/SQL function that is currently like that:
CREATE OR REPLACE FUNCTION tkt_get_underlying(n_input number)
RETURN t_table_of_number
IS
ret t_table_of_number;
CURSOR c IS SELECT n_number FROM t_table WHERE n_prop_1=n_input OR n_prop_2=n_input OR n_prop_3=n_input;
BEGIN
ret := t_table_of_number();
OPEN c;
FETCH c BULK COLLECT INTO ret;
CLOSE c;
RETURN ret;
END;
I want to be able to give an array as argument, however, I don’t know how to build my cursor to take to array. I think I could use the IN statement, but could you help me settle this down please ?
EDIT:
According to solution provided by Justin Cave, it would become:
CREATE OR REPLACE FUNCTION tkt_get_underlying(n_inputs t_table_of_number)
RETURN t_table_of_number
IS
ret t_table_of_number;
CURSOR c IS SELECT n_number FROM t_table WHERE n_prop_1 IN (SELECT column_value FROM TABLE(n_inputs))
OR n_prop_2 IN (SELECT column_value FROM TABLE(n_inputs))
OR n_prop_3 IN (SELECT column_value FROM TABLE(n_inputs));
BEGIN
ret := t_table_of_number();
OPEN c;
FETCH c BULK COLLECT INTO ret;
CLOSE c;
RETURN ret;
END;
However, the multiple SELECT column_value FROM TABLE(n_inputs) slow the entire function. How can I improve that ?
If you want to pass in a collection of
n_inputvalues and return the samet_table_of_numbercollection (i.e. you don’t need to know which element of the output array was associated with which element of the input array)This assumes that the number of elements that is going to potentially be inserted into the
retcollection is still reasonable to hold in PGA memory simultaneously. Depending on the situation, you may want to transform this into a pipelined table function in order to limit the amount of PGA memory required.