SQL and PL/SQL boundary
Arun Gupta, December 02, 2003 - 8:56 am UTC
Tom,
Is it right to say that the moment I code something like declare...begin...exception...end, I am using PL/SQL and all the code in packages/stored procedures/functions is PL/SQL, though it might contain SQL statements?
If this is correct, and all the code is in packages in the database, does this imply that I should try to minimize the use of dynamic SQL in packages?
Thanks...
December 02, 2003 - 9:26 am UTC
if it starts with declare or begin -- it is plsql.
yes, minimize the use of
o execute immediate
o dbms_sql
maximize the use of STATIC SQL
Arun Gupta, December 03, 2003 - 12:16 pm UTC
Tom,
I want to know more about why is dynamic sql less efficient when called from within PL/SQL. The context switching between SQL and PL/SQL will still take place even when I use static SQL.
Thanks...
December 03, 2003 - 5:02 pm UTC
do you have my book "Effective Oracle by Design"?
I go into this in detail but to summarize:
procedure p1
is
l_x number;
begin
execute immediate 'select count(*) from t' into l_x;
end;
procedure p2
is
l_x number;
select count(*) into l_x from t;
end;
Ok -- say we execute p1 and p2 100 times. Answer the following:
a) how many times will select count(*) from t be parsed by p1? 100. by p2? 1
parsing per execute instead of one parse per session. big reason number 1.
b) what procedures access T? I want to modify it. What code will be affected. Well P1 will -- but you HAVE NO CLUE that it will.
c) will p1 run successfully each time? no idea, what of the dynamic sql is bad. compile time static sql will execute. runtime dynamic sql -- who knows? (hard to code, harder to debug, really truly hard to maintain)
Go ahead and benchmark it. run p1 and p2 lots -- see which is "faster"
Arun Gupta, December 04, 2003 - 12:04 pm UTC
I did run the tests for i in 1..1000 loop. Posting the tkprof output would just be a waste of space. The dynamic sql was parsed 1000 times, the static, just once.
a) Why does Oracle parse a dynamic SQL statement every time if nothing has changed? From tkprof, both the statements look the same to Oracle: select count(*) from t
b) Is it a hard parse or a soft parse?
I am also trying to understand how your statement fits in:
In short, all SQL in Oracle is dynamic sql under the covers. Therefore, it is a
"language thing" in reality (static vs dynamic).
Thanks for your patience...
December 04, 2003 - 12:30 pm UTC
a) because each time it COULD be different, hence it is not cached.
b) soft
my point was -- in Oracle -- ALL SQL is dynamic (we hard and soft parse all sql, we do not "precompile" access plans in the manner DB2 on the mainframe does -- which is a GOOD thing (that we don't))
It is the language environment that will mandate whether static or dynamic sql is to be used
a) VB -- all about dynamic sql, no such thing as static
b) java -- pick between SQLJ (static) and JDBC (all about dynamic)
c) OCI -- all about dynamic
d) pro*c -- use static until you are forced to use dynamic
e) plsql -- DEFINITELY use static until you are forced at gunpoint to do dynamic
and so on.
Arun Gupta, December 04, 2003 - 1:25 pm UTC
Thanks...it clears the doubts. In addition to everything that you said about dependence and maintainability, the developers coded such convoluted logic using dynamic SQL that there were several (more than 20) possible SQLs generated from one stored procedure. Looking at the SQL from statspack report, it was impossible to tell where they came from. I regretted the day I taught the developers to use dynamic SQL.