cursor_sharing為force時帶來的一個執行計劃的假象

jolly10發表於2009-04-13
有時候可能存在評估的執行計劃和真實的執行計劃不一樣,檢視執行計劃的方法如下:
a.explain plan ,可透過dbms_xplan.display來檢視執行計劃
b.set autot
c.v$sql_plan
d.dbms_xplan.display_cursor

a,b的執行計劃都是根據explain plan得到的,因此都不是真實的(基表是sql_plan),而3,4的執行計劃都是真實的(基表是 V$SQL_PLAN),cursor_sharing為force+直方圖的時候可能存在一個假象,就是set autot trace給你顯示的執行計劃很完美,而真實的執行計劃是當作繫結變數處理的,第一次查詢的時候可能存在bind peeking的影響,而之後的如果沒有新的cursor產生,那麼執行計劃都是重用第一次的,[@more@]

做了個測試如下:


SQL> conn l5m/l5m @dev107_c203
Connected.
SQL> show parameter cursor

NAME TYPE
------------------------------------ ---------------------------------
VALUE
------------------------------
cursor_sharing string
EXACT
cursor_space_for_time boolean
FALSE
open_cursors integer
500
session_cached_cursors integer
20
SQL> alter session set cursor_sharing=force;

Session altered.

SQL> create table kk1(id int);

Table created.

SQL> insert into kk1 select 1 from dual;

1 row created.

SQL> insert into kk1 select 2 from dba_objects;

63413 rows created.

SQL> create index i_kk1_1 on kk1(id);

Index created.

SQL> exec dbms_stats.gather_table_stats(user,'KK1',method_opt=>'for all columns size 254');

PL/SQL procedure successfully completed.

SQL> set autot trace
SQL> select count(*) from kk1 where id=1;


Execution Plan
----------------------------------------------------------
Plan hash value: 1026585023

-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX RANGE SCAN| I_KK1_1 | 1 | 3 | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("ID"=1)


Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
335 bytes sent via SQL*Net to client
377 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed


SQL> /


Execution Plan
----------------------------------------------------------
Plan hash value: 1026585023

-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX RANGE SCAN| I_KK1_1 | 1 | 3 | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("ID"=1)


Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
335 bytes sent via SQL*Net to client
377 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed


SQL> select count(*) from kk1 where id=2;
//這裡顯示的執行計劃是假的,真正的執行計劃是index range scan


Execution Plan
----------------------------------------------------------
Plan hash value: 1408337260

---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 26 (8)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| KK1 | 62313 | 182K| 26 (8)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - filter("ID"=2)


Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
125 consistent gets
0 physical reads
0 redo size
337 bytes sent via SQL*Net to client
377 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed

下面的真正的執行計劃
SQL> set autot off
SQL> select sql_text,sql_id,child_number from v$sql
2 where sql_text like 'select count(*) from kk1%';

SQL_TEXT
--------------------------------------------------------------------------------
SQL_ID CHILD_NUMBER
--------------------------------------- ------------
select count(*) from kk1 where id=:"SYS_B_0"
1fnfa7pqtz9h6 0


SQL> select * from table(dbms_xplan.display_cursor('1fnfa7pqtz9h6','0'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 1fnfa7pqtz9h6, child number 0
-------------------------------------
select count(*) from kk1 where id=:"SYS_B_0"

Plan hash value: 1026585023

-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|* 2 | INDEX RANGE SCAN| I_KK1_1 | 1 | 3 | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("ID"=:SYS_B_0)


19 rows selected.



SQL> set autot trace

可以發現下面的執行計劃是假的,和加index(t)的提示的consistent gets是一致的
SQL> select count(*) from kk1 where id=2; //走索引邏輯讀125


Execution Plan
----------------------------------------------------------
Plan hash value: 1408337260

---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 26 (8)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| KK1 | 62313 | 182K| 26 (8)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - filter("ID"=2)


Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
125 consistent gets
0 physical reads
0 redo size
337 bytes sent via SQL*Net to client
377 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed



SQL> select /*+ index(t) */ count(*) from kk1 t where id=2;


Execution Plan
----------------------------------------------------------
Plan hash value: 1026585023

-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 126 (2)| 00:00:02 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX RANGE SCAN| I_KK1_1 | 62313 | 182K| 126 (2)| 00:00:02 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("ID"=2)


Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
125 consistent gets
0 physical reads
0 redo size
337 bytes sent via SQL*Net to client
377 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed



SQL> select /*+ full(t) */ count(*) from kk1 t where id=2;
//全表邏輯讀107


Execution Plan
----------------------------------------------------------
Plan hash value: 1408337260

---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 26 (8)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| KK1 | 62313 | 182K| 26 (8)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - filter("ID"=2)


Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
107 consistent gets
0 physical reads
0 redo size
337 bytes sent via SQL*Net to client
377 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed



下面看看dbms_xplan.display與dbms_xplan.display_cursor兩個包的結果是否一致

SQL> explain plan set statement_id='NO' for
2 select count(*) from kk1 where id=2;

SQL> select plan_table_output
2 from table(dbms_xplan.display('PLAN_TABLE','NO','ALL'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1408337260

---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 26 (8)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| KK1 | 62313 | 182K| 26 (8)| 00:00:01 |
---------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
-------------------------------------------------------------

1 - SEL$1
2 - SEL$1 / KK1@SEL$1

Predicate Information (identified by operation id):
---------------------------------------------------

2 - filter("ID"=2)

Column Projection Information (identified by operation id):

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
-----------------------------------------------------------

1 - (#keys=0) COUNT(*)[22]

25 rows selected.




SQL> select sql_text,sql_id,child_number from v$sql where sql_text like 'select count(*) from kk1%';


SQL_TEXT
--------------------------------------------------------------------------------
SQL_ID CHILD_NUMBER
--------------------------------------- ------------
select count(*) from kk1 where id=:"SYS_B_0"
1fnfa7pqtz9h6 0


SQL> select * from table(dbms_xplan.display_cursor('1fnfa7pqtz9h6','0'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 1fnfa7pqtz9h6, child number 0
-------------------------------------
select count(*) from kk1 where id=:"SYS_B_0"

Plan hash value: 1026585023

-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|* 2 | INDEX RANGE SCAN| I_KK1_1 | 1 | 3 | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("ID"=:SYS_B_0)


19 rows selected.


由於dbms_xplan.display表的基表是plan_table,而dbms_xplan.display_cursor的基表是v$sql_plan,看出結果是不一致的。

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

相關文章