Note: This blog entry is my summary of the references at the bottom and to be honest I wrote this long entry for myself for not to get lost between those references again :)

Shared pool is used to cache different types of data such as textual and executable forms of PL/SQL blocks and SQL statements, dictionary cache data, and other data. If you use shared pool effectively you can reduce resource consumption in at least four ways

Parse overhead is avoided if the SQL statement is already in the shared pool. This saves CPU resources on the host and elapsed time for the end user.

Shared pool memory requirements are reduced, because all applications use the same pool of SQL statements and dictionary resources.

I/O resources are saved, because dictionary elements that are in the shared pool do not require disk access.

Main components of shared pool are library cache (executable forms of SQL cursors, PL/SQL programs, and Java classes.) and the dictionary cache (usernames, segment information, profile data, tablespace information, and sequence numbers. ).The library cache stores the executable (parsed or compiled) form of recently referenced SQL and PL/SQL code. The dictionary cache stores data referenced from the data dictionary. This caches are managed by LRU algorithm to “age out” memory structures that have not been reused over time. Allocation of memory from the shared pool is performed in chunks. This allows large objects (over 5k) to be loaded into the cache without requiring a single contiguous area, hence reducing the possibility of running out of enough contiguous memory due to fragmentation. Starting with 9i The Shared Pool divide its shared memory areas into subpools. Each subpool will have Free List Buckets (containing pointers to memory chunks within the subpool ) and , memory structure entries, and LRU list. This architecture is designed to to increase the throughput of shared pool in that now each subpool is protected by a Pool child latch. This means there is no longer contention in the Shared Pool for a single latch as in earlier versions.

Infrequently, Java, PL/SQL, or SQL cursors may make allocations out of the shared pool that are larger than 5k then Oracle must search for and free enough memory to satisfy this request. This operation could conceivably hold the latch resource for detectable periods of time, causing minor disruption to other concurrent attempts at memory allocation. To allow these allocations to occur most efficiently, Oracle segregates a small amount of the shared pool. This memory is used if the shared pool does not have enough space. The segregated area of the shared pool is called the reserved poolwhich is also divided into subpools. Smaller objects will not fragment the reserved list, helping to ensure the reserved list will have large contiguous chunks of memory. Once the memory allocated from the reserved list is freed, it returns to the reserved list.

By using automatic shared memory management (ASMM) option available with 10G, which is activated by setting SGA_TARGET parameter with a value greater than 0, shared pool size is started to be managed by Oracle, under the limits of SGA_TARGET and other SGA components.

After these explanations lets start to explain how to manage shared pool with ASMM.

1- Using Shared Pool Effectively

avoid hard parsing by

using bind variables instead of literal values in your queries The script below can be used to find sqls which use literals

Because they are stored, Consider using stored procedures whenever possible

Avoiding users from change the optimization approach and goal for their individual sessions.

Reducing the number of entries in the dictionary cache by explicitly qualifying the segment owner, rather than using public synonyms or try to connect to the database through a single user ID, rather than individual user IDs because Reducing the number of distinct userIDs also reduces the load on the dictionary cache.SELECT employee_id FROM hr.employees WHERE department_id = :dept_id;

Using PL/SQL packages when your system has thousands of users, each with individual user sign-on and public synonyms because a package is executed as the owner, rather than the caller, which reduces the dictionary cache load considerably.

Avoid performing DDLs in peak hours because ddl operations invalidate the dependent SQLs and cause hard parsing when the statement called again.

Cache the sequence numbers by using CACHE keyword of CREATE/ALTER SEQUENCE clause to reduce the frequency of dictionary cache locks,

Try to avoid closing of rapidly executed cursors

Check for hash values which maps different literals. The query below should return no rows otherwise there is possibility for a bug

SELECT hash_value, count(*)
FROM v$sqlarea
GROUP BY hash_value
HAVING count(*) > 5
;

This should show if there are similar literal statements, or multiple versions of a statements which account for a large portion of the memory in the shared pool.

4- Identify which allocations causing shared pool to be aged out

spool ageout.txt
SELECT *FROM x$ksmlru
WHERE ksmlrnum>0;
spool off

This select returns no more than 10 rows and then erases the contents of the X$KSMLRU table so be sure to SPOOL the output. The X$KSMLRU table shows which memory allocations have caused the MOST memory chunks to be thrown out of the shared pool since it was last queried. This is sometimes useful to help identify sessions or statements which are continually causing space to be requested.

5- Why are there multiple child cursors.

V$SQL_SHARED_CURSOR explains why a particular child cursor is not shared with existing child cursors. Each column identifies a specific reason why the cursor cannot be shared.

SELECT SA.SQL_TEXT,SA.VERSION_COUNT,SS.*
FROM V$SQLAREA SA,V$SQL_SHARED_CURSOR SS
WHERE SA.ADDRESS=SS.ADDRESS
AND SA.VERSION_COUNT > 5
ORDER BY SA.VERSION_COUNT

6- Monitor Shared Pool sizing operations

You can see the shrinking and growing operations from V$SGA_RESIZE_OPS dynamic view and you can guess why there is need for this operations by focusing the sql at the sizing operation times.

You can use the sizing advices from the view v$shared_pool_advice. This view displays information about estimated parse time in the shared pool for different pool sizes and the sizes range from %10 to %200 of current shared pool size. This can give you idea for sizing SGA and obliquely shared pool by the help of ASMM.

The X$KSMSP view shows the breakdown of memory in the SGA. You can run this query to build trend information on memory usage in the SGA. Remember, the ‘free’ class in this query is not specific to the Shared Pool, but is across the SGA. Dont use the script below when db is under load. Check out Jonathan Lewis’s experiences on this view from here

a) if ‘free’ memory is low (less than 5mb or so) you may need to increase the shared_pool_size and shared_pool_reserved_size. You should expect ‘free’ memory to increase and decrease over time. Seeing trends where ‘free’ memory decreases consistently is not necessarily a problem, but seeing consistent spikes up and down could be a problem.
b) if ‘freeable’ or ‘perm’ memory continually grows then it is possible you are seeing a memory bug.
c) if ‘freeabl’ and ‘recr’ memory classes are always huge, this indicates that you have a lot of cursor info stored that is not releasing.
d) if ‘free’ memory is huge but you are still getting 4031 errors, the problem is likely reloads and invalids in the library cache causing fragmentation.
!!!!!!!!!! Note says that this query can hang database on HP platforms

High updates with low pct_succ_gets can be a clue of performance problems when accessing that dictionary object. For frequently accessed dictionary caches, the ratio of total GETMISSES to total GETS should be less than 10% or 15%, depending on the application. If this ratio is higher and every previous control is OK then you should consider to increase the shared pool size

Having objects pinned will reduce fragmentation and changes of encountering the ORA-04031 error. Objects causing a large number of other objects been flushed out from the shared pool are candidates to be pinned into the shared pool using dbms_shared_pool.keep procedure. You can check the x$ksmlru fixed table to see the candidates. This table keeps track of the objects and the corresponding number of objects flushed out of the shared pool to allocate space for the load. These objects are stored and flushed out based on the Least Recently Used (LRU) algorithm. Because this is a fixed table, once you query the table, Oracle will automatically reset the table so first insert the contents to temporary table like below,

You should also use hidden and unsupported parameter “_shared_pool_reserved_pct” to control reserved pool. This parameter controls the allocated percentage of shared pool for reserved pool. By default it is %5 of the shared pool and if you use ASMM for memory management you can set this value higher like 10 to allocate reserved pool dynamically. When you set the parameter you will see the shared_pool_reserved_size parameter will be adjusted to the new setting.

The parameter can not be modified when instance is started. You can use the query below to see the current value

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-04031 out of shared memory error. But how to understand the fragmentation ?

Occurrence of ORA-04031 error. Before this error signalled, memory is freed from unnecessary objects and merged. This error only occurs when there is still not a large enough contiguous piece of free memory after this cleaning process. There may be very large amounts of total free memory in the shared pool, but just not enough contiguous memory.

Using X$KSMLRU internal fixed table. We told about this view before about its usage for tracking age out operations, it also can be used to identify what is causing the large allocations. KSMLRSIZ column of this table shows the 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. Again be careful to save spool the result when you query this table

After finding the result you should do the followings to correct fragmentation

Keep object by pinning them as we discussed above

Use bind variables as we discussed before

Eliminate large anonymous PL/SQL block. Large anonymous PL/SQL blocks should be turned into small anonymous PL/SQL blocks that call packaged functions. The packages should be ‘kept’ in memory. To view candidates

Free memory in shared pool prevents fragmentation. This is not true because Free memory is more properly thought of as ‘wasted memory’. You would rather see this value be low than very high. In fact, a high value of free memory is sometimes a symptom that a lot of objects have been aged out of the shared pool and therefore the system is experiencing fragmentation problems.

Flushing shared pool frequently solves fragmentation and improves performance. This is also incorrect because Executing this statement causes a big spike in performance and does nothing to improve fragmentation. You lost your cached cursors when you flush and they will hard parsed next time with high CPU consumption.

13- Using related database parameters

CURSOR_SHARING: Setting this parameter to smilar can solve your hard parse problems caused by using literals but can have side effects mostly on DSS environments and systems which uses stored outlines.

CURSOR_SPACE_FOR_TIME: This parameter specifies whether a cursor can be deallocated from the library cache to make room for a new SQL statement. CURSOR_SPACE_FOR_TIME has the following values meanings:

If CURSOR_SPACE_FOR_TIME is set to false (the default), then a cursor can be deallocated from the library cache regardless of whether application cursors associated with its SQL statement are open. In this case, Oracle must verify that the cursor containing the SQL statement is in the library cache.

If CURSOR_SPACE_FOR_TIME is set to true, then a cursor can be deallocated only when all application cursors associated with its statement are closed. In this case, Oracle need not verify that a cursor is in the cache, because it cannot be deallocated while an application cursor associated with it is open.

You must be sure that the shared pool is large enough for the work load otherwise performance will be badly affected and ORA-4031 eventually signalled.

OPEN_CURSORS: This parameter sets the upper bound for the number of cursor that a session can have open and if you size it correctly, cached cursors can be stay opened and won’t have to be closed to let new cursor open

PROCESSES / SESSIONS: You can review the high water mark for Sessions and Processes in the V$RESOURCE_LIMIT view. If the hard-coded values for these parameters are much higher than the high water mark information, consider decreasing the parameter settings to free up some memory in the Shared Pool for other uses.

SESSION_CACHED_CURSORS: When a cursor is closed, Oracle divorces all association between the session and the library cache state. If no other session has the same cursor opened, the library cache object and its heaps are unpinned and available for an LRU operation. The parameter SESSION_CACHED_CURSORS controls the number of cursors “soft” closed, much like the cached PL/SQL cursors. Oracle checks the library cache to determine whether more than three parse requests have been issued on a given statement. If so, then Oracle assumes that the session cursor associated with the statement should be cached and moves the cursor into the session cursor cache. Subsequent requests to parse that SQL statement by the same session then find the cursor in the session cursor cache.To determine whether the session cursor cache is sufficiently large for your instance, you can examine the session statistic sessioncursorcachehits in the V$SYSSTAT view. This statistic counts the number of times a parse call found a cursor in the session cursor cache. If this statistic is a relatively low percentage of the total parse call count for the session, then consider setting SESSION_CACHED_CURSORS to a larger value. Steve Adams also wrote usefully queries to find the usage and the maximum cacheable cursors.session_cursor_cache.sql

This was a long article and if you see anything wrong or suspicious please feel free to comment for correction

Like this:

Related

Hi sir,
I have only one word to say,Excellent!I guess I wont have the requirement to read manual again for shared pool.I realy liked your approach for trainings.I am also in trainings but much much junior to you both in experience and knowledge but I am trying to learn database.If there would be some doubts than may I ask them on this blog?
I am going to add your blog on my website and bloglines account.
With best regards,
Aman….

I have been working on Oracle over 10 years. Your web site gave me a very impression. I still can learn something and update my knowledge from here. Also you looks like an artist and make this web so beautiful.

thank you very much for your comments they are very encouraging to keep what I am doing.

To be honest I have lost my concentration to blogging after changing , country that I live , language I speak, marital status from single to married in the same year, but today I started writing again. I hope this time there wont be a long break period like this.

I really feel the passion of oracle you have ,excellent would be the small word,
Think its best to fix the problems of shared pool.

Query:

My prod DB is facing with Ora-07445 error,to my knoweldge or research it searching for a CHILD CURSORS which is not been in shared pool,can you please advice me,if needed i will share my alert & user trace file as well

Thank you for you comment but the right place for 7445 errors is not here It is metalink support , as you see it on error definition :(

ORA-07445:
exception encountered: core dump [string] [string] [string] [string] [string] [string]
Cause: An OS exception occurred which should result in the creation of a core file. This is an internal error.
Action: Contact your customer support representative.

[…] reason that makes this book unique. I already wrote my own compilation about shared pool in a blog post but this chapter thought me many more things like lathes and mutexes in shared pool , In memory undo […]

Had a question. You have used v$sql to “Identify which sqls are using lots of shared memory”

The sum(sharable_mem) from v$sql and v$sqlarea for a sqlid is different. Which sharable_mem should be considered for the actual space utilized by a sql_id in shared_pool? Also what is the difference between v$sql and v$sqlarea?

thats also what I observe on 11.2.0.2, looks like it is a bug on 11.2.0.3 this problem disappears. I can see on 11.2.0.2 same sql has got x number of childs but v$sqlarea claims it is less then x number. If you have issues I suggest open an SR with ORacle

[…] Shared Pool Oracle na web: pinning objects in shared pool – Oracle— Question: What is the Oracle shared_pool_size parameter and what is contained in this shared pool? Answer: the Shared Pool is a RAM area within the RAM heap that is created at … http://www.dba-oracle.com/concepts/shared_pool_concepts.htm – ver What I learned about shared pool— could some one please tell me the steps needed to pin packages in the shared pool . thanks dbaforums.org/oracle/index.php?showtopic=3854 – ver DBMS SHARED POOL – Oracle FAQ 14-09-2007 · Update on 07/06/11: Please also watch this video tutorial from Tanel Poder after you read this article. Nowadays, I am working on performance tuning guide of 10gR2 … coskan.wordpress.com/2007/09/14/what-i-learned-about-shared-pool-management – ver […]

Halo,
I have a question. Our production was upgraded from oracle 10g to 11g. Since then there has been concurrency issue in the form of Library cache mutex x waits and shared pool contention which slows down the DB performance. This has been there for almost 4 months. We have tried the basic trouble shooting methods to resolve mutex waits and shared pool contention but still the problem exists. We have also raised an SR in metalink. Would be great if you could help us with your suggestions. Appreciate your time.

Excellent information. I was able to find some areas for improvement. For the high invalidations and reloads which I see I am trying to fix them. Your queries help us figure out the issues it would be great if we could have the solutions to fix those issues like which particular parameter needs to be changed. The 11th point to check the reserved pool sizehad a query to check for the particular value and if it is >0 increase the pool size. That point helped me fix it quickly. I am in a situation where the queries cannot be tuned as it is a package but have to try improving the complete daily load by tuning the database as much as I can.
NAMESPACE PINS PINHITS RELOADS INVALIDATIONS

Excellent document and very useful information in a nut shell.
I’ve a query, Is it possible to join v$sqlarea and v$db_object_cache?
Basically my requirement is to gather the information that when a package/procedure/function is last executed which I put in KEEP shared pool.
I’m getting the package name and number of executions in v$db_object_cache, but not getting information about last executed, so can I use last_load_date of v$sqlarea for the same purpose (is v$sqlarea records the plsql package entries also?)?

I drop a leave a response each time I like a post on a site or I
have something to valuable to contribute to the conversation. It’s a result of the sincerness displayed
in the article I looked at. And after this article What I learned about shared pool management | Coskan’s Approach to
Oracle. I was actually excited enough to drop
a thought ;-) I actually do have a few questions for you if you do not mind.

Could it be just me or do some of the remarks appear like they are
coming from brain dead visitors? :-P And, if you are
writing on additional places, I would like to keep up with everything fresh you have to post.
Could you make a list all of your public sites like your linkedin profile, Facebook page or twitter feed?