cursor_sharing=force会带来一个执行计划的假象
有时候可能存在评估的执行计划和真实的执行计划不一样,查看执行计划的方法如下:
a.explain plan
b.set autot
c.v$sql_plan
d.dbms_xplan.display_cursor
a,b的执行计划都是根据explain plan得到的,因此都不是真实的,而3,4的执行计划都是真实的,cursor_sharing为force+直方图的时候可能存在一个假象,就是set autot trace给你显示的执行计划是你所希望的,而真实的执行计划是当作绑定变量处理的;第一次执行某个SQL,由于存在bind peeking的影响,同时如果第二次要执行的SQL没有新的cursor产生,那么执行计划都是重用第一次的,做了个测试如下:
SQL> alter session set cursor_sharing=force;
会话已更改。
SQL> create table kk1(id int);
表已创建。
SQL> insert into kk1 select 1 from dual;
已创建 1 行。
SQL> insert into kk1 select 2 from dba_objects;
已创建51923行。
SQL> create index i_kk1_1 on kk1(id);
索引已创建。
SQL> exec dbms_stats.gather_table_stats(user,'KK1',method_opt=>'for all columns size 254');
PL/SQL 过程已成功完成。
SQL> set autot trace
SQL> select count(*) from kk1 where id=1;
执行计划
----------------------------------------------------------
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)
统计信息
----------------------------------------------------------
126 recursive calls
0 db block gets
19 consistent gets
0 physical reads
0 redo size
408 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> /
执行计划
----------------------------------------------------------
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)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
408 bytes sent via SQL*Net to client
385 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
执行计划
----------------------------------------------------------
Plan hash value: 1408337260
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 21 (10)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| KK1 | 52240 | 153K| 21 (10)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("ID"=2)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
103 consistent gets
0 physical reads
0 redo size
410 bytes sent via SQL*Net to client
385 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行。
SQL> select /*+ index(t) */ count(*) from kk1 t where id=2;//走索引逻辑读为103
执行计划
----------------------------------------------------------
Plan hash value: 1026585023
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 104 (2)| 00:00:02 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX RANGE SCAN| I_KK1_1 | 52240 | 153K| 104 (2)| 00:00:02 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=2)
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
103 consistent gets
0 physical reads
0 redo size
410 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> /
执行计划
----------------------------------------------------------
Plan hash value: 1026585023
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 104 (2)| 00:00:02 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX RANGE SCAN| I_KK1_1 | 52240 | 153K| 104 (2)| 00:00:02 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=2)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
103 consistent gets
0 physical reads
0 redo size
410 bytes sent via SQL*Net to client
385 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; //全表逻辑读82
执行计划
----------------------------------------------------------
Plan hash value: 1408337260
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 21 (10)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| KK1 | 52240 | 153K| 21 (10)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("ID"=2)
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
82 consistent gets
0 physical reads
0 redo size
410 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> /
执行计划
----------------------------------------------------------
Plan hash value: 1408337260
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 | 21 (10)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| KK1 | 52240 | 153K| 21 (10)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("ID"=2)
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
82 consistent gets
0 physical reads
0 redo size
410 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
从以上例子可以看出当强制使用cursor_sharing时会有可能造成bind peeking,对性能会有所影响,因此尽量不要使用强制cursor_sharing。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13129975/viewspace-588722/ ,如需转载,请注明出处,否则将追究法律责任。