I have a question regarding how queries executed through 'execute immediate' is treated in the library cache (We use Oracle 11).
Let's say I have a function like this:
FUNCTION get_meta_map_value (
getfield IN VARCHAR2,
searchfield IN VARCHAR2,
searchvalue IN VARCHAR2
) RETURN VARCHAR2 IS
v_outvalue VARCHAR2(32767);
sql_stmt VARCHAR2(2000) := 'SELECT '||getfield||' FROM field_mapping, metadata '||
'WHERE field_mapping.metadataid = metadata.metadataid AND rownum = 1 AND '||searchfield||' = :1';
BEGIN
EXECUTE IMMEDIATE sql_stmt INTO v_outvalue USING searchvalue;
...
The getfield and searchfield are in one installation always the same (but has other values in another installation, so that is why we use dynamic sql) So this leaves us with an sql that only differs in the searchvalue (which is a parameter). This function is called in a loop that executes x times, from inside another stored procedure. The stored procedure is executed y times during the connection life time, through ODBC connection. And there are z connections, but each of them uses the same database login.
Now let us also assume that the searchvalue changes b times during one loop.
Question 1: When calculating how many copies of the sql will be kept in the library cache, can we disregard the different values the searchvalue can have (b), as the value is sent as a parameter to execute immediate?
Question 2: Will the loop cause a hard parse of the query x times (query will be created in library cache x times), or can Oracle reuse the query? (We assume that the searchvalue is the same for all calls in this question here, for simplicity)
Question 3: Does the y (number of times the stored procedure is called from odbc during the lifetime of one connection) also multiply the amount of copies of the query that are kept in library cache?
Question 4: Does the z (number of simultaneous connections with same db login) multiply the amount of copies of the query that are kept in library cache?
Main question: What behaviour should I expect here? Is the behaviour configurable? The cause for this question is that we have had this code is production for 4 years, and now one of our customer gets back to us and says "This query fills our whole SGA, and Oracle says it's your fault".