I've heard that using bind variables is (can be) more efficient, because for subsequent calls with a different bind value, the query itself is still the same, so it doesn't need to be parsed anymore.
I understand why this is the case for fixed values. In the cursor below, the value is fixed on 1. If I have a different cursor that is the same, except the 1 becomes 2, it is a diffent query. Clear so far.
declare
cursor C_CURSOR is
select * from TESTTABLE pt where pt.ID = 1;
But I wondered if this is also the case when using PL/SQL variables inside the cursor. Are they expanded as if it's a fixed value, or are they interpreted as bind variables.
I've searched far and wide, but everywhere I find examples about literals, as in the case above, but no clear explanation about the use of PL/SQL variables.
In other words, of the two snippets below, is the second one potentially more efficient, or are they essentially the same?
With a PL/SQL variable directly in the cursor:
declare
V_TEST integer := 1;
cursor C_CURSOR is
select *
from
TESTTABLE pt
where
pt.ID = V_TEST;
begin
for r in C_CURSOR loop
null;
end loop;
end;
With a bind variable:
declare
V_TEST int := 1;
cursor C_CURSOR(B_TEST int) is
select *
from
TESTTABLE pt
where
pt.ID = B_TEST;
begin
for r in C_CURSOR(V_TEST) loop
null;
end loop;
end;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…