Share pool 9: bind variables to session_cached_cursors

Source: Internet
Author: User
The parsing is divided into hard parsing, soft parsing, and soft parsing. the first time an SQL statement is parsed, hard Parsing is required. The result set cache is not parsed. For details, see: the difference between hard parsing and soft Parsing is that hard parsing requires the generation of execution plans. Soft parsing does not require the generation of execution plans. There are two types of SQL statements in Oracle, the first type is DD.

The parsing is divided into hard parsing, soft parsing, and soft parsing. the first time an SQL statement is parsed, hard Parsing is required. The result set cache is not parsed. For details, see: the difference between hard parsing and soft parsing in the result set cache is: Hard parsing = the execution plan needs to be generated; soft parsing = There is no need to generate an execution plan. There are two types of SQL statements in Oracle, the first type is DD.

The parsing is divided into hard parsing, soft parsing, and soft parsing. The SQL statement must be hard parsed for the first time.

There is also a result set cache-no resolution. For details, see result set cache.

The difference between hard parsing and soft Parsing is as follows:

Hard resolution = Execution Plan generation required soft resolution = no execution plan generation required

There are two types of SQL statements in Oracle: DDL statements, which are not shared, that is, hard Parsing is required for each execution. There is also a DML statement that performs hard or soft parsing.

Hard parsing becomes soft parsing: bind variables

Soft resolution becomes soft resolution: Set session_cached_cursors. For details, see the end of this article.

Soft parsing parameter session_cached_cursors reference value
Select owner, name from v $ db_object_cachewherechild_latch = 1; displays the objects cached in the library cache and their sizes.

Session_cached_cursors memory: the memory occupied by the session will be released when the session exits, or when the maximum value is reached, the earliest and rarely used memory will be released.

Session_cached_cursors parameter settings: -- from Zhou Liang -- oracle dba practices.

In SYS @ bys3> show parameter session_cached 11G, the default value is 50.

NAME TYPE VALUE

-----------------------------------------------------------------------------

Session_cached_cursors integer 50

SYS @ bys3> select. name, B. value fromv $ statnamea, v $ sesstat B where. statistic # = B. statistic # and. name in ('session cursorcache hits ', 'session cursor cache count', 'parse count (total)') andb. sid = (select c. sid from v $ mystat c where rownum = 1 );

NAME VALUE

--------------------------------------------------------------------------

Session cursor cachehits 32

Session cursor cachecount 4

Parse count (total) 43

Session cursor cache count indicates the number of cursors in the session cache. The session_cached_cursors parameter indicates the maximum number of cursors that can be cached in each session.

The session cursor cache count is smaller than session_cached_cursors. You do not need to increase the session_cached_cursors size. If they are equal, they may need to be added.

Session cursor cache hits indicates the number of hits from UGA-soft resolution times.

Parse count (total) specifies the total number of sessions parsed.

If session cursor cache hits is close to parsecount (total), you do not need to adjust session_cached_cursors.

If the session cursor cache hits is much smaller than parsecount (total), you may need to adjust session_cached_cursors.

Session_cached_cursors takes effect for all sessions. If the proportion of sessions to be optimized is small, the adjustment is of little significance.

Bind Variable
Oracle can reuse the execution plan by binding variables.

The essence of variable binding is the substitution variable used to replace constants in SQL statements. Variable binding makes the SQL statements submitted each time identical.

Variable binding only acts as a placeholder. Variable binding with the same name does not mean that they are the same. During transmission, you must consider the order of the passed values and the bound variables, instead of the variable name.

Variable binding can usually improve efficiency. Unusual situations are as follows:

When a field (including a field set) has an index and the set of the field (SET) has a very large potential (that is, there is a particularly large proportion in the field, using Bound variables may cause query plan errors, which may result in very low query efficiency. In this case, it is best not to bind variables.

However, not all variables need to be bound in any situation. The following are two exceptions:

1. for SQL statements that are executed once after a considerable period of time, the benefits of binding variables will be offset by the inability to effectively use the optimizer.

2. Data Warehouse.

Variable binding cannot be used as an embedded string. It can only be used as a variable in a statement. You cannot bind a variable to replace the table name, process name, and field name.

From the efficiency point of view, because oracle10G fully introduces CBO, the efficiency of using variable binding in 10g is more obvious than 9i.

Bind Variable Peeking: Bind Peeking-favorable for even Field Distribution

When Bind Peeking is used to Bind a variable in the WHERE condition, CBO generates an execution plan based on the actual variable value used for the first time. Throughout the Life Cycle of this cursor, CBO no longer hardparse the same SQL statement.

Advantage: if the value of the index field is evenly distributed, hardparse reduces and improves performance.

Disadvantage: if the field distribution is uneven and the value used for the first time is not universal, the execution plan will be very bad.

Oracle11g provides a new feature, AdpativeCursorSharing, or Extended Cursor Sharing. His core idea is that when the histogram of a field provides uneven data information, CBO will try to generate a more appropriate execution plan when actually using different values.

The default bind peeking behavior of the database is adjusted by implicit parameters: _ OPTIM_PEEK_USER_BINDS.

Disable Bind Variable Peeking. You can set this parameter to False-to restart the database.

SQL> alter sessionset "_ optim_peek_user_binds" = false

Bind Variable Classification-bind graduation and bind-mismatch cause high version Problems

Bind_mismatch is generally because bind value length is different, resulting in the bindbuffer cannot be reused, and eventually the cursor cannot be reused.

The root cause is that the Oracle Database Engine applies the classification of bind variables, that is, it is divided into four levels according to the length of bind variables, such as varchar2 (32) and varchar2 (33) belong to different levels.

There are four levels:
0 ~ 32
32 ~ 128
129 ~ 2000
2001 ~

When there are hundreds of varchar2 columns in a table, bind-mismatch caused by bind graduation is more common, that is, multiple sub-cursors that cannot be shared are generated.

Too many sub-cursors may affect SQL parse.

You can bind a fixed length to a string variable, for example, to_char (4000 ), to avoid excessive child cursor issues due to bind graduation.

For example, when binding a variable for a character-type field, the BUFFER of 32 bytes is used for the first time. If the value is smaller than 32 bytes, the SQL statement is executed for the second time, if the value is smaller than 32 bytes, the CURSOR can be shared. If the value is greater than 32 bytes, it cannot be shared because BIND_MISMATCH is used. A sub-CURSOR is generated, and a 128-byte bind buffer is allocated, and so on.
Select count (*) from v $ SQL _shared_cursor where SQL _id = '9rwd4wkwm4bsy 'andBIND_MISMATCH = 'y ';
COUNT (*)
----------
120

You can view the value of each variable bound in the v $ SQL _bind_capture View:
Select position, LAST_CAPTURED, datatype_string, value_string fromv $ SQL _bind_capture where SQL _id = '9rwd4wkwm4bsy 'and rownum <50;

Normally, such a large number of sub-CURSOR will not be generated. However, some bugs may cause problems.

If there is no patch, a temporary solution, set a large BUFFER
SQL> ALTER SESSION SET EVENTS '10503 trace name context level , Forever ';

You can view the specific content in the view. View the bind_metadata of the above shared parent cursor
Selects. child_number, m. position, m. max_length,
Decode (m. datatype, 1, 'varchar2', 2, 'number', m. datatype) as datatype
From v $ SQL s, v $ SQL _bind_metadata m
Where s. SQL _id = '94ka9mv232yxb'
And s. child_address = m. address;
CHILD_NUMBER POSITION MAX_LENGTH DATATYPE
--------------------------------------------------------------------
0 3 32 VARCHAR2
0 2 32 VARCHAR2
0 1 22 NUMBER
1 3 32 VARCHAR2
1 2 128 VARCHAR2
1 1 22 NUMBER

From the perspective of variable binding, a maximum of four sub-cursors can be generated. But why can the Version Count of SQL ordered by version count in AWR reports be dozens or even hundreds? The reason for not sharing is that bind_mismatch.

About: cursor_sharing

According to oracle's official recommendation, cursor_sharing = SIMILAR is not recommended in 11g. In fact, it is not recommended in all versions. setting this value can easily lead to high-version questions. in addition, this value may be confusing and cannot be explained. in addition, the SIMILAR value will be removed from the 12c version to be released according to relevant oracle documents. this problem occurs in the client database because many SQL statements are not bound with parameters. To reduce hard parsing, we recommend that you set cursor_sharing = FORCE when the business is low and refresh the sharedpool.

If the cursor_sharing parameter is set to similar, the predicate value in SQL is automatically replaced by a variable. This will increase the number of cursor instances.

To reduce the usage of the library cache by cursor, change the cursor_shring parameter to the default exact mode.

In this way, version_count reduces a lot, but the number of hard parsing times increases, and the Library Cache Latch wait may increase.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.