http://www.itpub.net/thread-1350771-1-1.html
在上面的帖子里,楼主提出使用dbms_session来处理绑定变量。但是,这个方法必须进行有效的测试,防止出现错误的执行计划。
由于sys_context导致bind_peeking无法正确执行,在某些情况下,会生成错误的执行计划,见下例:
[@more@]SQL*Plus: Release 11.2.0.1.0 Production on 星期三 9月 29 08:43:56 2010
Copyright (c) 1982, 2010, Oracle. All rights reserved.
SUNDOG315>conn test/test
已连接。
SUNDOG315>create table t (id number,text varchar2(4000));
表已创建。
SUNDOG315>insert into t select 1,object_name from dba_objects;
已创建55085行。
SUNDOG315>insert into t select 2,'aaa' from dual;
已创建 1 行。
SUNDOG315>commit;
提交完成。
SUNDOG315>create index t_idx on t(id);
索引已创建。
SUNDOG315>exec dbms_stats.gather_table_stats(user,'T',null,null,method_opt=>'for
all columns',cascade=>true);
PL/SQL 过程已成功完成。
SUNDOG315>select id,count(*) from t group by id;
ID COUNT(*)
---------- ----------
1 55085
2 1
SUNDOG315>select count(*) from t where id=2;
COUNT(*)
----------
1
SUNDOG315>select * from table(dbms_xplan.display_cursor());
SQL_ID 8tfp2xac71yd6, child number 0
-------------------------------------
select count(*) from t where id=2
Plan hash value: 293504097
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX RANGE SCAN| T_IDX | 1 | 3 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=2)
已选择19行。
--选择了正确的索引范围扫描
SUNDOG315>var fid number;
SUNDOG315>exec :fid := 2;
PL/SQL 过程已成功完成。
SUNDOG315>select count(*) from t where id=:fid;
COUNT(*)
----------
1
SUNDOG315>select * from table(dbms_xplan.display_cursor());
SQL_ID 3u1847dua6k75, child number 0
-------------------------------------
select count(*) from t where id=:fid
Plan hash value: 293504097
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX RANGE SCAN| T_IDX | 1 | 3 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=:FID)
已选择19行。
--由于bind_peeking的存在,依然选择了正确的索引范围扫描
SUNDOG315>create context test using test_p;
上下文已创建。
SUNDOG315>create or replace procedure test_p is
2 begin
3 dbms_session.set_context('test','id',2);
4 end;
5 /
过程已创建。
SUNDOG315>exec test_p;
PL/SQL 过程已成功完成。
SUNDOG315>select sys_context('TEST','ID') from dual;
SYS_CONTEXT('TEST','ID')
--------------------------------------------------------------------------------
2
SUNDOG315>select count(*) from t where id=sys_context('test','id');
COUNT(*)
----------
1
SUNDOG315>select * from table(dbms_xplan.display_cursor());
SQL_ID 69x691fhtckm0, child number 0
-------------------------------------
select count(*) from t where id=sys_context('test','id')
Plan hash value: 1058879072
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 32 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | INDEX FAST FULL SCAN| T_IDX | 27543 | 82629 | 32 (4)| 00:00:01 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("ID"=TO_NUMBER(SYS_CONTEXT('test','id')))
已选择19行。
--TO_NUMBER(SYS_CONTEXT('test','id'))无法peeking到正确的值,因此,选择了错误的执行计划
SUNDOG315>set autot trace stat
SUNDOG315>select count(*) from t where id=2;
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
423 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SUNDOG315>select count(*) from t where id=:fid;
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
423 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SUNDOG315>select count(*) from t where id=sys_context('test','id');
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
116 consistent gets --逻辑读增大
0 physical reads
0 redo size
423 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
目前,估计用dbms_session的人还是比较少,像这种问题,Oracle其实完全可以处理掉的。估计如果使用的人多,就会进行处理了。呵呵
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/19423/viewspace-1039108/ ,如需转载,请注明出处,否则将追究法律责任。