[20120327]Adaptive Cursor Sharing 的問題

lfree發表於2012-03-27
[20120327]Adaptive Cursor SharingG 的問題

11G的新特性裡面Adaptive Cursor Sharing採用新特性來解決PEEKED BIND的問題,但是必須要經過一次執行後,來改變執行計劃。
但是如果在一些工具裡面,它執行並不是提取全部的資訊,而是取一部分就會遭遇執行計劃不改變的問題。

如下站點:

按照講解是一個BUG,但是沒有給出例子,自己寫個例子測試如下:

1.建立測試環境:
SQL> select * from v$version ;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

SQL> create table t1 as select rownum id,'test' name from dual connect by level<=1000;
Table created.
SQL> insert into t1 select 1001 id,'book' name  from dual connect by level<=1000;
1000 rows created.
SQL> commit;
Commit complete.

SQL> create index i_t1_id on t1(id);
Index created.

SQL>exec SYS.DBMS_STATS.GATHER_TABLE_STATS (user,'T1',Method_Opt => 'FOR ALL COLUMNS SIZE 254 ');

SQL> column data_type format a20
SQL> select table_name,column_name,data_type,histogram from dba_tab_cols where wner='SCOTT' and table_name='T1';
TABLE_NAME                     COLUMN_NAME                    DATA_TYPE            HISTOGRAM
------------------------------ ------------------------------ -------------------- ---------------
T1                             ID                             NUMBER               HEIGHT BALANCED
T1                             NAME                           CHAR                 FREQUENCY

SQL> alter system flush shared_pool;


2.執行sql語句,為了更好測試,我在toad執行,:a :=1 :

select /*+ testme */ * from t1 where id = :a;

        ID NAME
---------- ----
         1 test

column is_bind_sensitive format a20
column is_bind_aware     format a20
column is_shareable      format a20
SELECT sql_id, sql_text, child_number, plan_hash_value, executions, is_bind_sensitive, is_bind_aware, is_shareable
FROM v$sql
WHERE sql_text LIKE '%testme%' AND sql_text NOT LIKE '%sql_text%'

SQL_ID        SQL_TEXT                       CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS IS_BIND_SENSITIVE    IS_BIND_AWARE        IS_SHAREABLE
------------- ------------------------------ ------------ --------------- ---------- -------------------- -------------------- --------------------
9rx9cq6x20guk select /*+ testme */ * from t1            0      1111474805          1 Y                    N                    Y
               where id = :a

--知道sql_id=9rx9cq6x20guk.

SQL> select * from table(dbms_xplan.display_cursor('9rx9cq6x20guk',NULL,'allstats last peeked_binds'));
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  9rx9cq6x20guk, child number 0
-------------------------------------
select /*+ testme */ * from t1 where id = :a
Plan hash value: 1111474805
--------------------------------------------------------
| Id  | Operation                   | Name    | E-Rows |
--------------------------------------------------------
|   0 | SELECT STATEMENT            |         |        |
|   1 |  TABLE ACCESS BY INDEX ROWID| T1      |      1 |
|*  2 |   INDEX RANGE SCAN          | I_T1_ID |      1 |
--------------------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
   1 - (NUMBER): 1
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("ID"=:A)

Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

30 rows selected.


3.在toad下執行相同的sql語句,但是帶入的引數是1001:

SQL_ID        SQL_TEXT                       CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS IS_BIND_SENSITIVE    IS_BIND_AWARE        IS_SHAREABLE
------------- ------------------------------ ------------ --------------- ---------- -------------------- -------------------- --------------------
9rx9cq6x20guk select /*+ testme */ * from t1            0      1111474805          2 Y                    N                    Y
               where id = :a

--執行多次,引數1001,可以發現執行計劃並沒有產生子游標。


再次執行在toad下執行相同的sql語句,但是帶入的引數是1001:
SQL_ID        SQL_TEXT                       CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS IS_BIND_SENSITIVE    IS_BIND_AWARE        IS_SHAREABLE
------------- ------------------------------ ------------ --------------- ---------- -------------------- -------------------- --------------------
9rx9cq6x20guk select /*+ testme */ * from t1            0      1111474805          4 Y                    N                    Y
               where id = :a


--可以發現並沒有產生新的子游標.換句話講Adaptive Cursor Sharing並沒有起作用。

現在toad開啟auto trace(具體操作是在sql編輯器裡面,點選右鍵選擇auto trace),主要目的是這樣可以提取全部查詢資訊。
再次執行以上語句。引數1001

SQL_ID        SQL_TEXT                       CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS IS_BIND_SENSITIVE    IS_BIND_AWARE        IS_SHAREABLE
------------- ------------------------------ ------------ --------------- ---------- -------------------- -------------------- --------------------
9rx9cq6x20guk select /*+ testme */ * from t1            0      1111474805          5 Y                    N                    Y
               where id = :a


再次執行以上語句。引數1001
SQL_ID        SQL_TEXT                       CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS IS_BIND_SENSITIVE    IS_BIND_AWARE        IS_SHAREABLE
------------- ------------------------------ ------------ --------------- ---------- -------------------- -------------------- --------------------
9rx9cq6x20guk select /*+ testme */ * from t1            0      1111474805          5 Y                    N                    Y
               where id = :a

9rx9cq6x20guk select /*+ testme */ * from t1            1      3617692013          1 Y                    Y                    Y
               where id = :a


--可以發現現在出現了新的子游標。

SQL> select * from table(dbms_xplan.display_cursor('9rx9cq6x20guk',NULL,'allstats last peeked_binds'));
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  9rx9cq6x20guk, child number 0
-------------------------------------
select /*+ testme */ * from t1 where id = :a
Plan hash value: 1111474805
--------------------------------------------------------
| Id  | Operation                   | Name    | E-Rows |
--------------------------------------------------------
|   0 | SELECT STATEMENT            |         |        |
|   1 |  TABLE ACCESS BY INDEX ROWID| T1      |      1 |
|*  2 |   INDEX RANGE SCAN          | I_T1_ID |      1 |
--------------------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
   1 - (NUMBER): 1
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("ID"=:A)
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

SQL_ID  9rx9cq6x20guk, child number 1
-------------------------------------
select /*+ testme */ * from t1 where id = :a
Plan hash value: 3617692013
-------------------------------------------
| Id  | Operation         | Name | E-Rows |
-------------------------------------------
|   0 | SELECT STATEMENT  |      |        |
|*  1 |  TABLE ACCESS FULL| T1   |   1012 |
-------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
   1 - (NUMBER): 1001
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("ID"=:A)
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level
59 rows selected.

4.再次執行以上語句。引數1

SELECT sql_id, sql_text, child_number, plan_hash_value, executions, is_bind_sensitive, is_bind_aware, is_shareable
FROM v$sql
WHERE sql_text LIKE '%testme%' AND sql_text NOT LIKE '%sql_text%';

SQL_ID        SQL_TEXT                       CHILD_NUMBER PLAN_HASH_VALUE EXECUTIONS IS_BIND_SENSITIVE    IS_BIND_AWARE        IS_SHAREABLE
------------- ------------------------------ ------------ --------------- ---------- -------------------- -------------------- --------------------
9rx9cq6x20guk select /*+ testme */ * from t1            0      1111474805          5 Y                    N                    N
               where id = :a

9rx9cq6x20guk select /*+ testme */ * from t1            1      3617692013          1 Y                    Y                    Y
               where id = :a

9rx9cq6x20guk select /*+ testme */ * from t1            2      1111474805          1 Y                    Y                    Y
               where id = :a

--可以發現child_number=0 的子游標IS_SHAREABLE='N'.

5.如果要避免這樣的問題,最好的方法是使用提示/*+ bind_aware */.


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-719598/,如需轉載,請註明出處,否則將追究法律責任。

相關文章