To save on these costs, the Oracle instance maintains a system-wide LRU cache (aka the shared cursor cache) - exposed via v$sqlarea - of previously encountered SQL statements and appropriate derived information so that when the next SQL statement is submitted for parsing it is checked for match against the cached ones. (The definition of the criterea for a match is beyond the scope of this tutorial. Roughly speaking, the current statement must be both textually identical to its match candidate, famously to the extent of whitespace and upper/lower case identity, and the types of the bind variables must match.) When the current statement is matched, the stored derived information (parse tree, execution plan, etc) is reused and computational cost is saved.
Moreover, the above processing has to be done in the context of a cursor which itself has to be opened and associated with the SQL statement in question, again at some cost.
A given session might have one or several concurrently open cursors. Information on these, including the foreign key reference to v$sqlarea is exposed via v$open_cursor.
Advanced programmers using the "difficult" interfaces to cursor manipulation - eg OCI in a C programming environment or the Dbms_Sql package in a PL/SQL programming environment - typically code explicit, relatively elaborate approaches to minimize cursor costs. This cost-saving paradigm is described below.
Programmers whose requirements can be satisfied by static SQL in a PL/SQL programming environment enjoy the benefits of an implicit implementation of this cost-saving paradigm while writing simple, easy-to-maintain, code.
However, it is possible to subvert these implicit benefits by careless programing. This tutorial presents the conceptual background for understanding this and some guidelines for avoiding such mistakes.
Moreover using Dbms_Sql develops your mental model in this critical area of functionality, so it's good at least to study it and practice with it a little.
There are some use cases (for example when you don't know the number of bind variables until runtime) which are not supported using native dynamic SQL, and in such cases Dbms_Sql is the only viable approach.
Thus Oracle recommends that you use native dynamic SQL when its syntax
supports your requirements, and that you use Dbms_Sql only when
your functional requirements cannot be satisfied by native dynamic
SQL. The theoretical performance advantage of Dbms_Sql over native
dynamic SQL is in fact balanced by the performance advantages of the latter's
tighter integration into the PL/SQL language, so you should not need to
choose Dbms_Sql for performance reasons when other approaches
are viable.
This is achieved because the PL/SQL runtime system does not actually
close your cursor (in the sense of the actions impemented by Dbms_Sql.Close_Cursor,
aka a hard-close) when the PL/SQL close statement is executed,
or when an implicit cursor statement completes. Rather, it just soft-closes
the cursor you think you've closed, ie it marks it as a candidate
for later hard-close in the PL/SQL cursor cache, a LRU cache of potentially
re-usable open cursors exposed via v$open_cursor.
Bryn Llewellyn, PL/SQL Product Manager, Oracle Corp
last updated 4-Jun-2003