The title is indeed strange, but I will try to explain.
I have been programming Oracle (PL-SQL) in version 11G for several years. I recently moved to a new workplace, and in the first task I was required to write a function that accepts 5 variables as input, and it is possible that not all variables will be sent - maybe only one.
As part of the process, there is a very large query between lots of very large tables, so the more values there are in the input, the more the JOINS between the tables are realized and the indexes are used efficiently. Like
create function get_customer(p_ssn number, p_phone number, p_email varchar2, p_address varchar2, p_unique_id number number) return <type>
is
begin
select...
from A, B, C, D, E, F, G
JOIN ..
where ssn = p_ssn
and phone = p_phone ...
end;
The first way I thought to solve the task is by using dynamic SQL, so that we check for a certain input whether it is not NULL, and if so we can chain its AND to the query and at the end EXECUTE IMMEDIATE for the entire query Like
v_sql varchar2(4000);
v_sql := 'select...
from A, B, C, D, E, F, G
JOIN ..
WHERE 1=1'
if p_phone is not null then
v_sql := v_sql || ' AND phone = p_phone'
end if;
etc..
But, my new team leader decided not to use dynamic SQL because it is complicated and can crash and other reasons. He suggested using NVL for each of the inputs, for example
select...
from A, B, C, D, E, F, G
JOIN ..
where ssn = nvl (p_ssn, a.ssn)
and phone = nvl(p_phone, b.phone) etc..
I was stunned. I thought a DBA team leader should understand a thing or two about efficiency and runtime. After I showed him the execution plan was really bad, and in particular that it took him a lot of time to finish his run, he told me to find another effective way without using dynamic SQL.
So, other suggestions How can the task be solved?
CREATE TABLEandINSERTstatements for your tables; an explanation of the logic that you are trying to implement; YOUR attempt(s) at a solution; and theEXPLAIN PLANfor your solutions. There is a very big difference between having a function where if you pass differing numbers of arguments then you select from different tables compared to a function where if you pass different numbers of arguments then you always select from the same tables but with different filters and it is unclear which you are trying to implement.BIND_VARIABLEin DBMS_SQL package