|
原帖由 hponiang 于 2010-9-24 14:03 发表 ![]()
在这里和大家说一下,这种方法不行,因大数据量使用全表扫描的时候sys_context与直接变量绑定的差异很大,因为sys_context其实也相当于一个函数,从一个内存表中取数据
类似于
SELECT * from dba_objects WHERE object_name = (SELECT object_name
from dba_objects WHERE object_name = 'UNDO$');
这句SQL的执行的时间并不是简单的子查询的用时加上外层查询用时,上面耗时0.2,下面的耗时0.015
SELECT *
from dba_objects WHERE object_name = 'UNDO$';
我也不是很明白为什么会产生的这样的问题,如果有人明白请告诉我 一下
既然已经全表扫描,那么对于一个表,是不大会有什么区别的
SUNDOG315>create table t (id number,text varchar2(4000));
表已创建。
SUNDOG315>insert into t select 1,object_name from dba_objects;
已创建55087行。
SUNDOG315>insert into t select 2,'aaa' from dual;
已创建 1 行。
SUNDOG315>commit;
提交完成。
SUNDOG315>exec dbms_stats.gather_table_stats(user,'T',null,null,method_opt=>'for
all columns',cascade=>true);
PL/SQL 过程已成功完成。
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: 2966233522
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 102 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| T | 1 | 3 | 102 (0)| 00:00:02 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("ID"=2)
已选择19行。
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: 2966233522
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 103 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
|* 2 | TABLE ACCESS FULL| T | 27544 | 82632 | 103 (1)| 00:00:02 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("ID"=TO_NUMBER(SYS_CONTEXT('test','id')))
已选择19行。
SUNDOG315>set autot trace stat
SUNDOG315>select count(*) from t where id=2;
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
300 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
300 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 |
|