Why do I have hundreds of child cursors when cursor_sharing is set to similar in 10g
By Maria Colgan-Oracle on May 26, 2009
Recently we received several questions regarding an unusual situation where a SQL Statement has hundreds of child cursors. This is in fact the expected behavior when
- CURSOR_SHARING is set to similar
- Bind peeking is in use
- And a histogram is present on the column used in the where clause predicate of query
You must now be wondering why this is the expected behavior. In order to explain, let's step back and begin by explaining what CURSOR_SHARING actually does.
CURSOR_SHARING was introduced to help relieve pressure put on the shared pool, specifically the cursor cache, from applications that use literal values rather than bind variables in their SQL statements. It achieves this by replacing the literal values with system generated bind variables thus reducing the number of (parent) cursors in the cursor cache. However, there is also a caveat or additional requirement on CURSOR_SHARING, which is that the use of system generated bind should not negatively affect the performance of the application. CURSOR_SHARING has three possible values: EXACT, SIMILAR, and FORCE. The table below explains the impact of each setting with regards to the space used in the cursor cache and the query performance.
|CURSOR_SHARING VALUE||SPACE USED IN SHARED POOL||QUERY PERFORMANCE|
|EXACT (No literal replacement)||Worst possible case - each stmt issued has its own parent cursor||Best possible case as each stmt has its own plan generated for it based on the value of the literal value present in the stmt|
|FORCE||Best possible case as only one parent and child cursor for each distinct stmt||Potentially the worst case as only one plan will be used for each distinct stmt and all occurrences of that stmt will use that plan|
|SIMILAR without histogram present||Best possible case as only one parent and child cursor for each distinct stmt||Potentially the worst case as only one plan will be used for each distinct stmt and all occurrences of that stmt will use that plan|
|SIMILAR with histogram present||Not quite as much space used as with EXACT but close. Instead of each stmt having its own parent cursor they will have their own child cursor (which uses less space)||Best possible case as each stmt has its own plan generated for it based on the value of the literal value present in the stmt|
In this case the statement with hundreds of children falls into the last category in the above table, having CURSOR_SHARING set to SIMILAR and a histogram on the columns used in the where clause predicate of the statement. The presence of the histogram tells the optimizer that there is a data skew in that column. The data skew means that there could potentially be multiple execution plans for this statement depending on the literal value used. In order to ensure we don't impact the performance of the application, we will peek at the bind variable values and create a new child cursor for each distinct value. Thus ensuring each bind variable value will get the most optimal execution plan. It's probably easier to understand this issue by looking at an example. Let's assume there is an employees table with a histogram on the job column and CURSOR_SHARING has been set to similar. The following query is issued
select * from employees where job = 'Clerk';
The literal value 'Clerk' will be replaced by a system generated bind variable B1 and a parent cursor will be created as
select * from employees where job = :B1;
The optimizer will peek the bind variable B1 and use the literal value 'Clerk' to determine the execution plan. 'Clerk' is a popular value in the job column and so a full table scan plan is selected and child cursor C1 is created for this plan. The next time the query is executed the where clause predicate is job='VP' so B1 will be set to 'VP', this is not a very popular value in the job column so an index range scan is selected and child cursor C2 is created. The third time the query is executed the where clause predicate is job ='Engineer' so the value for B1 is set to 'Engineer'. Again this is a popular value in the job column and so a full table scan plan is selected and a new child cursor C3 is created. And so on until we have seen all of the distinct values for job column. If B1 is set to a previously seen value, say 'Clerk', then we would reuse child cursor C1.
|Value for B1||Plan Used||Cursor Number|
|Clerk||Full Table Scan||C1|
|VP||Index Range Scan||C2|
|Engineer||Full Table Scan||C3|
As each of these cursors is actually a child cursor and not a new parent cursor you will still be better off than with CURSOR_SHARING set to EXACT as a child cursor takes up less space in the cursor cache. A child cursor doesn't contain all of the information stored in a parent cursor, for example, the SQL text is only stored in the parent cursor and not in each child.
Now that you know the explanation for all of the child cursors you are seeing you need to decide if it is a problem for you and if so which aspect affects you most, space used in the SHARED_POOL or query performance. If your goal is to guarantee the application performance is not affected by setting CURSOR_SHARING to SIMILAR then keep the system settings unchanged. If your goal is to reduce the space used in the shared pool then you can use one of the following solutions with different scopes:
- Individual SQL statements - drop the histograms on the columns for each of the affected SQL statements
- System-wide - set CURSOR_SHARING to FORCE this will ensure only one child cursor per SQL statement
Both of these solutions require testing to ensure you get the desired effect on your system. Oracle Database 11g provides a much better solution using the Adaptive Cursor Sharing feature. In Oracle Database 11g, all you need to do is set CURSOR_SHARING to FORCE and keep the histograms. With Adaptive Cursor Sharing, the optimizer will create a cursor only when its plan is different from any of the plans used by other child cursors. So in the above example, you will get two child cursors (C1 and C2) instead of 3.