5.1.8.2 Sharable SQL
If an application makes use of literal (unshared) SQL then this can severely limit scalability and thr oughput. The cost of parsing a new SQL statement is expensive both in terms of CPU requirements and the number of times the library cache and shared pool latches may need to be acquired and released.
Eg: Even parsing a simple SQL statement may need to acquire a library cache latch 20 or 30 times.
The best approach to take is that all SQL should be sharable unless it is adhoc or infrequently used SQL where it is important to give CBO as much information as possible in order for it to produce a good execution plan. Reducing the load on the Shared Pool
5.1.9 Parse Once / Execute Many
By far the best approach to use in OLTP type applications is to parse a statement only once and hold the cursor open, executing it as required. This results in only the initial parse for each statement (either soft or hard). Obviously there will be some statements which are rarely executed and so maintaining an open cursor for them is a wasteful overhead.
Note that a session only has < Parameter: OPEN_CURSORS> cursors available and holding cursors open is likely to increase the total number of concurrently open cursors.
In precompilers the HOLD_CURSOR parameter controls whether cursors are held open or not while in OCI developers have direct control over cursors.
5.1.10 Eliminating Liter al SQL
If you have an existing application it is unlikely that you could eliminate all literal SQL but you should be prepared to eliminate some if it is causing problems. By looking at the V$SQLAREA view it is possible to see which literal statements are good candidates for converting to use bind variables. The following query that shows SQL in the SGA where there are a large number of similar statements:
/* Formatted on 2011/6/22 10:51:28 (QP5 v5.163.1008.3004) */
SELECT SUBSTR (sql_text, 1, 40) "SQL", COUNT (*), SUM (executions) "TotExecs"
FROM v$sqlarea
WHERE executions < 5
GROUP BY SUBSTR (sql_text, 1, 40)
HAVING COUNT (*) > 30
ORDER BY 2;
Note: If there is latch contention for the library cache latches the above Statement may cause yet further contention problems.
The values 40,5 and 30 are example values so this query is looking for different statements whose first 40 characters are the same which have only been executed a few times each and there are at least 30 different occurrences in the shared pool.
This query uses the idea it is common for literal statements to begin "SELECT col1, col2, col3 FROM table WHERE..." with the leading portion of each statement being the same.
Note: There is often some degree of resistance to converting literal SQL to use bind variables. Be assured that it has been proven time and time again that performing this conversion for the most frequently occurring statements can eliminate problems with the shared pool and improve scalability greatly.
5.1.11 Performance Effects
Oracle performance can be severely compromised by large volumes of literal SQL. Some of the symptoms that may be noticed are:
(1). System is CPU bound and exhibits an insatiable appetite for CPU.
(2). System appears to periodically “hang” after some period of normal operation.
(3). Latch contention on shared pool and library cache latches.
(4). Increasing the shared pool size delays the problem but it re-occurs more severely.
5.1.12 Identifying the Problem
An Oracle instance suffering from too much literal SQL will likely exhibit some of the symptoms above. There are several investigations the DBA can use to help confirm that this is indeed happening in the instance.
5.1.13 Library Cache Hit Ratio
The library cache hit ratio should be very high (98%) when SQL is being shared and will remain low regardless of shared pool sizing adjustments when SQL is chronically non-sharable. Use the following query to determine the hit ratios by namespace in the library cache.
/* Formatted on 2011/6/22 10:56:34 (QP5 v5.163.1008.3004) */
SELECT namespace, (100 * gethitratio) hit_ratio FROM v$librarycache;
The “SQL AREA” namespace will be the one affected by literal SQL.
5.1.14 SQL Parse-to- Execute Ratio
The following query displays the percentage of SQL executed that did not incur an expensive hard parse. Literal SQL will always be fully parsed, so a low percentage may indicate a literal SQL or other SQL sharing problem.
/* Formatted on 2011/6/22 10:58:35 (QP5 v5.163.1008.3004) */
SELECT 100 * (1 - A.hard_parses / B.executions) noparse_ratio
FROM (SELECT VALUE hard_parses
FROM v$sysstat
WHERE name = 'parse count (hard)') A,
(SELECT VALUE executions
FROM v$sysstat
WHERE name = 'execute count') B;
Again, when this ratio is high Oracle is sparing CPU cycles by avoiding expensive parsing and when low there may be a literal SQL problem.
5.1.15 Latch Free Waiters
A telltale sign that the instance is suffering library cache and shared pool problems is active latch contention with sessions waiting on the “latch free” wait event. The following query will select all current sessions waiting for either the shared pool or library cache latches.
/* Formatted on 2011/6/22 11:01:08 (QP5 v5.163.1008.3004) */
SELECT sid, event, name latch
FROM v$session_wait w, v$latch l
WHERE w.event = 'latch free' AND l.latch# = w.p2 AND l.name IN ('shared pool','library cache');
When this query selects more than 5-10% of total sessions there is likely very serious performance degradation taking place and literal SQL may be the culprit.
5.1.16 Finding Literal SQL
We can attempt to locate literal SQL in the V$SQL fixed view by grouping and counting statements that are identical up to a certain point based on the observation that most literal SQL becomes textually distinct toward the end of the statement (e.g. in the WHERE clause). The following query returns SQL statements having more than 10 statements that textually match on leading substring.
/* Formatted on 2011/6/22 11:04:55 (QP5 v5.163.1008.3004) */
SELECT S.sql_text
FROM v$sql S,
( SELECT SUBSTR (sql_text, 1, &&size) sqltext, COUNT (*)
FROM v$sql
GROUP BY SUBSTR (sql_text, 1, &&size)
HAVING COUNT (*) > 10) D
WHERE SUBSTR (S.sql_text, 1, &&size) = D.sqltext;
The SQL*Plus substitution variable &&size can be adjusted to vary the text length used to match statements, as can the value 10 used to filter by level of duplication. Note that this query is expensive and should not be executed frequently on production systems.
5.2 Memory Fragment ation
The primary problem that occurs is that free memory in the shared pool becomes fragmented into small pieces over time.
Any attempt to allocate a large piece of memory in the shared pool will cause large amount of objects in the library cache to be flushed out and may result in an ORA-4031 out of shared memory error.
Oracle ORA-04031 错误 说明
http://blog.csdn.net/tianlesoftware/archive/2011/06/15/6546191.aspx
5.2.1 DIAGNOSIS
(1)ORA-4031 ERROR
One way to diagnose that this is happening is to look for ORA-4031 errors being returned from applications. When an attempt is made to allocate a large contiguous piece of shared memory, and not enough contiguous memory can be created in the shared pool, the database will signal this error.
Before this error is signalled, all objects in the shared pool that are not currently pinned or in use will be flushed from the shared pool, and their memory will be freed and merged. This error only occurs when there is still not a large enough contiguous piece of free memory after this happens. There may be very large amounts of total free memory in the shared pool, but just not enough contiguous memory.
Note: The compiled code for a package was split into more than one-piece, each piece being only about 12K in size. So, the 64K restriction was lifted; however, packages larger 100K still may have problems compiling. Furthermore, with releases 7.2/2.3 of Oracle7/PLSQL, loading a library unit (package, function, and procedure) into the shared pool does NOT require one contiguous piece of memory in the shared pool. This means that chances of getting ORA-4031 is dramatically reduced.
(2)INIT.ORA PARAMETER
An init.ora parameter can be set so that whenever an ORA-4031 error is signalled a dump will occur into a trace file. By looking for these trace files, the DBA can determine that these errors are occurring. This is useful when applications do not always report errors signalled by oracle, or if users do not report the errors to the DBAs. The parameter is the following:
Event = "4031 trace name errorstack"
This will cause a dump of the oracle state objects to occur when this error is signalled. By looking in the dump for 'Load=X' and then looking up a few lines for 'name=' you can often tell whether an object was being loaded into the shared pool when this error occurred. If an object was being loaded then it is likely that this load is the cause of the problem and the Object should be 'kept' in the shared pool. The object being loaded is the object printed after the 'name='.
(3)X$KSMLRU
There is a fixed table called X$KSMLRU that tracks allocations in the shared pool that cause other objects in the shared pool to be aged out. This fixed table can be used to identify what is causing the large allocation.
The columns of this fixed table are the following: KSMLRCOM -allocation comment that describes the type of allocation.
If this comment is something like 'MPCODE' or 'PLSQL%' then there is a large PL/SQL object being loaded into the shared pool. This PL/SQL object will need to be 'kept' in the shared pool. If this comment is 'kgltbtab' then the allocation is for a dependency table in the library cache. This is only a problem when several hundred users are logged on using distinct user ids.
The solution in this case is to use fully qualified names for all table references. This problem will not occur in 7.1.3 or later.
If you are running MTS and the comment is something like 'Fixed UGA' then the problem is that the init.ora parameter 'open_cursors' is set too high.
KSMLRSIZ - amount of contiguous memory being allocated. Values over around 5K start to be a problem, values over 10K are a serious problem, and values over 20K are very serious problems. Anything less then 5K should not be a problem.
KSMLRNUM - number of objects that were flushed from the shared pool in order allocate the memory.
KSMLRHON - The name of the object being loaded into the shared pool if the object is a PL/SQL object or a cursor.
KSMLROHV - hash value of object being loaded
KSMLRSES - SADDR of the session that loaded the object.
The advantage of X$KSMLRU is that it allows you to identify problems with fragmentation that are effecting performance, but that are not bad enough to be causing ORA-4031 errors to be signalled. If a lot of objects are being periodically flushed from the shared pool then this will cause response time problems and will likely cause library cache latch contention Problems when the objects are reloaded into the shared pool.
One unusual thing about the X$KSMLRU fixed table is that the contents of the fixed table are erased whenever someone selects from the fixed table. This is done since the fixed table stores only the largest allocations that have occurred.
The values are reset after being selected so that subsequent large allocations can be noted even if they were not quite as large as others that occurred previously. Because of this resetting, the output of selecting from this table should be carefully noted Since it cannot be reselected if it is forgotten. Also you should take care that there are not multiple people on one database that select from this table because only one of them will select the real data.
To monitor this fixed table just runs the following:
select * from X$KSMLRU where KSMLRSIZ >5000;
