admin
2010-08-20
<p><strong>Query using Bind Variables is suddenly slow</strong></p>
<h2>Applies to: </h2>
Oracle Server - Enterprise Edition - Version: 9.0.1 to 10.2<br>
This problem can occur on any platform.<br>
<h2><a name="SYMPTOM" rel="nofollow"></a>Symptoms</h2>
<p>You are running on a database at 9.x or above, and have observed that sometimes, for no apparent <br>
reason, some SQL which has been running fine suddenly runs very poorly. You have made no changes <br>
to the data, the SQL, or the statistics for the objects involved. <br>
<br>
On further examination of the SQL it can be seen that it is using bind variables. </p>
<h2><a name="CAUSE" rel="nofollow"></a>Cause</h2>
<p>One reason for this behaviour may be explained by the use of a feature introduced in 9.x called bind <br>
peeking. <br>
<br>
With this feature the query optimizer peeks at the values of user-defined bind variables on the first <br>
invocation of a cursor. This feature lets the optimizer determine the selectivity of any WHERE <br>
clause condition, based on this value just as if a literal had been used instead of a bind variable. <br>
On subsequent invocations of the cursor, no peeking takes place, and the cursor is shared, based on <br>
the standard cursor-sharing criteria, even if subsequent invocations use different bind values. <br>
Thus if the first set of bind values that happen to be used when the cursor is first hard-parsed are not <br>
representative then the plan may be inappropriate for subsequent executions. <br>
<br>
The Oracle 10.2 Database Performance Tuning Guide manual Chapter 13 "The Query Optimizer" says the <br>
following about peeking bind variables :- <br>
<br>
"When bind variables are used in a statement, it is assumed that cursor sharing is intended and that <br>
different invocations are supposed to use the same execution plan. If different invocations of the <br>
cursor would significantly benefit from different execution plans, then bind variables may have been <br>
used inappropriately in the SQL statement." <br>
<br>
Bind peeking has been known to cause a different execution plan to be used on different nodes of a RAC <br>
cluster, because each node has its own Shared Pool, and despite the same SQL, data, and statistics the <br>
first time a cursor was hard parsed on each node a different set of bind values was presented to the <br>
optimizer, and it thus chose a different plan on each node. <br>
<br>
There is a hidden parameter which controls this feature's behaviour, whose default value is TRUE. <br>
Although a stable plan can be achieved by setting the parameter off it must be realized that this stable <br>
plan is not necessarily the optimum plan for all bind values. Consider the following simple example where <br>
tablex has 10,000 rows and col1 has an index. <br>
<br>
SELECT <whatever> <br>
FROM tablex <br>
WHERE col1 BETWEEN :bind1 AND :bind2; <br>
<br>
If this SQL is used, for example, with values 123 and 124 in order to pick out 2 rows from the <br>
10,000 rows in the table then using an index would be the obvious choice. <br>
However, if the same SQL is used with bind values 123 and 9999 then we would be getting the vast majority <br>
of the rows and a full table scan would be more appropriate. But the optimizer cannot now know this, <br>
and does not change the plan accordingly. <br>
</p>
<h2><a name="FIX" rel="nofollow"></a>Solution</h2>
<p>In this sort of situation it would perhaps be a good idea to modify the application and have two separate <br>
modules/sections each with the above SQL, but with a variation (perhaps modified with a hint) that will result <br>
in the desired plan. The appropriate module could then be invoked programmatically. An example might be <br>
a situation where you use essentially the same SQL to query the pay for one employee or all 10,000 employees. <br>
The query for one employee should use indexes, and the query for all employees should do a full table scan. <br>
<br>
N.B. The cursor will be moved out of the Shared Pool and therefore require a hard parse on subsequent <br>
invocation in a number of circumstances, such as :- <br>
<br>
1) database shutdown/restart </p>
<p>2) cursor not in use by any session, and aged out by LRU algorithm</p>
<p>3) change to the stats associated with any referenced object (eg following a gather stats job)</p>
<p>4) change to the structure of any referenced object (eg alter table) <br>
<br>
5) Granting/revoking privileges on a referenced object <br>
</p>
<p>It will NOT get moved out by flushing the Shared Pool if it is pinned (ie in use) <br>
<br>
CONCLUSION <br>
========== <br>
<br>
It is desirable to share cursors, particularly in an OLTP environment, for all the good reasons outlined in <br>
<br>
<a href="https://support.oracle.com/CSP/main/article?cmd=show&type=NOT&id=62143.1" rel="nofollow">Note 62143.1</a> Understanding/Tuning the Shared Pool in Oracle7, 8, 8i <br>
<br>
Thus coding bind variables, or perhaps using CURSOR_SHARING values of SIMILAR or FORCE, is an appropriate <br>
path to follow, but it must be realized that having bind peeking may result in unpredictable execution <br>
plans dependent on the first set of bind values presented to the optimizer on hard parse. <br>
Tuning SQL with hints and coding your application to allow the use of the appropriate "version" of the <br>
SQL, or using literal values, is the preferred method of dealing with SQL having changing <br>
execution plans due to bind peeking, but if necessary this feature can also be disabled. <br>
<br>
To set this feature off for the whole database :- <br>
<br>
a) set _OPTIM_PEEK_USER_BINDS=FALSE in the spfile/init.ora <br>
<br>
or just for the session :- <br>
<br>
b) use alter session set "_OPTIM_PEEK_USER_BINDS"=FALSE; <br>
</p>
<p>For a good case study where this was a factor please see <a href="https://support.oracle.com/CSP/main/article?cmd=show&type=NOT&id=369427.1" rel="nofollow">Note 369427.1</a> "Case Study: The Mysterious Performance Drop"</p>
<p>N.B. Please also be aware of the following <br>
</p>
<p>i) (unpublished) Bug:5082178 (fixed in 10.2.0.4 and 11.x). <br>
</p>
<p>Details: <br>
In some situations bind peeking can occur when it should not eg: Bind peeking can occur for user binds even if "_optim_peek_user_binds" is set to FALSE.<br>
This can cause binds to be marked "unsafe" leading to cursors not being shared when they should be.<br>
This fix is notable as plan changes could occur if statements suffering this problem execute in a release with this fix as the CBO will no longer have peeked data to use when determining an execution plan.</p>
ii) (unpublished) Bug: 4567767 Abstract: UNEXPLAINED PLAN CHANGES CAN OCCUR WITHOUT STATS REGATHER (Fixed in 10.2.0.4 and 11.x)<br>
<br>
Details: <br>
It is possible for queries' execution plans to change without any modification in statistics or optimizer environment. Usually it is interpreted as the plans changed "out of the blue". The reason for the change is that density is being reevaluated as 1/ndv instead of taking the statistic stored in the data dictionary when the table is reloaded to the row cache for whatever reason, like a shared pool flush.<br>
It is not easy to catch in the act but can be seen on a 10053 trace file when the query is hardparsed before and after the table is reloaded to the row cache.<br>
Before:<br>
Column: ISOCODE Col#: 7 Table: PL_X_NP Alias: X<br>
NDV: 1344 NULLS: 0 DENS: 1.5152e-02 <------ From Dict.<br>
NO HISTOGRAM: #BKT: 1 #VAL: 2<br>
After:<br>
Column: ISOCODE Col#: 7 Table: PL_X_NP Alias: X <br>
NDV: 1344 NULLS: 0 DENS: 7.4405e-04 <------ 1 / 1344<br>
NO HISTOGRAM: #BKT: 1 #VAL: 2<br>
<br>
To turn this fix off (in 11g and 10gR2):<br>
Set "_fix_control"='4567767:off'<br>
Workaround <br>
Set event 10139 :-<br>
<p> alter session set events '10139 trace name context forever';<br>
or<br>
event="10139 trace name context forever" <br>
</p>
<p>This bug is described in <a href="https://support.oracle.com/CSP/main/article?cmd=show&type=NOT&id=338113.1" rel="nofollow">Note:338113.1</a> "Plans can change despite no stats being regathered"</p>
<p>iii) <a href="https://support.oracle.com/CSP/main/article?cmd=show&type=BUG&id=5364143" rel="nofollow">Bug: 5364143</a> Abstract: UNPREDICTABLE CHANGE IN QUERY OPTIMIZER PLAN (Fixed in 10.2.0.4 and 11.x)</p>
<p>Details: <br>
It is possible for queries' execution plans to change without any modification in statistics or optimizer environment. Usually its interpreted like the plans changed "out of the blue". The reason for the change is that the cursor was taken out of the library cache for whatever reason (flush, Memory Pressure, DDLs,etc) and upon reload sometimes bind peeking is skipped for the cursor.<br>
Note: Disabling Bind Peeking DOES NOT workaround the issue.<br>
</p>
<p>SUMMARY <br>
======= <br>
<br>
In summary, the bind peeking feature can give the optimizer better information and allow a more appropriate <br>
execution plan if the bind values presented on hard parsing the cursor are representative. However, if there <br>
is a possibility they are NOT representative then a plan which is sub-optimal for subsequent invocations <br>
may result. Under these circumstances one of the above strategies should be considered. <br>
Ultimately, in order to make the most appropriate decision, a good knowledge of both the application <br>
and the data is required. <br>
<br>
<br>
FOOTNOTE <br>
======== <br>
<br>
Once a good plan is in operation for a key SQL statement it always good practice to do the following :- <br>
<br>
a) for a 9.2 database capture the statistics for the objects involved using DBMS_STATS.EXPORT_TABLE_STATS. <br>
(See Metalink <a href="https://support.oracle.com/CSP/main/article?cmd=show&type=NOT&id=117203.1" rel="nofollow">Note 117203.1</a> "How to Use DBMS_STATS to Move Statistics to a Different Database" for <br>
more information on how to do this). <br>
These statistics could then be imported in an "emergency" to restore use of a good plan while a rogue plan <br>
is investigated. (NB take a copy of the "bad" stats before importing, of course). <br>
<br>
In 10g whenever optimizer statistics are modified using the DBMS_STATS package, old versions of the statistics <br>
are saved automatically for future restoration, so the above is not necessary. <br>
See <a href="https://support.oracle.com/CSP/main/article?cmd=show&type=NOT&id=281793.1" rel="nofollow">Note 281793.1</a> "Oracle Database 10g stats history using dbms_stats.restore_table_stats". <br>
<br>
<br>
b) capture the good execution plan so it can be used as a baseline reference in the event that an <br>
undesired change occurs. For 9.2 use <a href="https://support.oracle.com/CSP/main/article?cmd=show&type=NOT&id=260942.1" rel="nofollow">Note 260942.1</a>: "Display Execution plans from Statements in V$SQL_PLAN". <br>
<br>
In 10g the view DBA_HIST_SQL_PLAN has historical information with respect to execution plans. Use <br>
<br>
select * from table(dbms_xplan.display_awr('&sql_id')) <br>
<br>
...as documented in <a href="https://support.oracle.com/CSP/main/article?cmd=show&type=NOT&id=362887.1" rel="nofollow">Note 362887.1</a> "A 10g Equivalant Process To The 9i Statspack Level 6 Execution Plan Output" </p>
<p> </p>