手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle 11g 的PL/SQL函数结果缓存

[日期:2014-08-05] 来源:Linux社区  作者:stevendbaguo [字体:大 中 小]

模仿Oracle性能诊断艺术中的例子做了两个试验,书上说如果不用RELIES_ON,则函数依赖的对象发生的变更操作就不会导致结果缓存的失效操作(result_cache RELIES_ON(test1,test2)),试验证明不对,函数f1()并没有使用RELIES_ON,但表上的变化影响到了函数。

rlwrap - 解决Linux下SQLPLUS退格、上翻键乱码问题 http://www.linuxidc.com/Linux/2013-07/87890.htm

SQLPLUS spool 到动态日志文件名 http://www.linuxidc.com/Linux/2013-03/80988.htm

Oracle SQLPLUS提示符设置 http://www.linuxidc.com/Linux/2012-10/73326.htm

通过设置SQLPLUS ARRAYSIZE(行预取)加快SQL返回速度 http://www.linuxidc.com/Linux/2011-07/38711.htm

C:\Documents and Settings\guogang>sqlplus gg_test/gg_test@10.10.15.25_gg

SQL*Plus: Release 10.2.0.1.0 - Production on 星期一 8月 4 19:46:44 2014
Copyright (c) 1982, 2005, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
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> drop table test1 purge;
SQL> drop table test2 purge;
SQL> create table test1 as select * from dba_objects;
SQL> create table test2 as select * from all_objects;
SQL> select count(*) from test1;
COUNT(*)
----------
74144
SQL> select count(*) from test2;
COUNT(*)
----------
73248


SQL> create or replace function f1
return number
is
l_ret number;
begin
select count(*) into l_ret
from test1,test2
where test1.object_type = test2.object_type
and test1.object_type in ('TABLE SUBPARTITION','VIEW','INDEX','TABLE');
return l_ret;
end;
/
函数已创建。


SQL> set timing on
SQL> select f1() from dual;
F1()
----------
60681409

已用时间: 00: 00: 07.29

--禁用结果缓存

SQL> execute dbms_result_cache.Bypass(bypass_mode=>true,session=>true);
SQL> select f1() from dual;
F1()
----------
60681409

已用时间: 00: 00: 03.60

--启用结果缓存

SQL> execute dbms_result_cache.Bypass(bypass_mode=>false,session=>true);
SQL> select f1() from dual;
F1()
----------
60681409
已用时间: 00: 00: 00.00


SQL> delete from test1 where object_type = 'VIEW' and rownum <100;
SQL> delete from test2 where object_type = 'VIEW' and rownum <100;
SQL> commit;
SQL> select f1() from dual;
F1()
----------
59788330

已用时间: 00: 00: 07.09 --可以看到数据发生变化,即使不使用RELIES_ON,结果集也是正确的。

 

SQL> select count(*)
from test1, test2
where test1.object_type = test2.object_type
and test1.object_type in ('TABLE SUBPARTITION','VIEW','INDEX','TABLE');
COUNT(*)
----------
59788330
已用时间: 00: 00: 03.56



SQL> create or replace function f2
return number
result_cache RELIES_ON(test1,test2)
is
l_ret number;
begin
select count(*) into l_ret
from test1,test2
where test1.object_type = test2.object_type
and test1.object_type in ('TABLE SUBPARTITION','VIEW','INDEX','TABLE');
return l_ret;
end;
/
函数已创建。


SQL> select f2() from dual;
F2()
----------
59788330
已用时间: 00: 00: 03.54
SQL> select f2() from dual;
F2()
----------
59788330
已用时间: 00: 00: 00.00

SQL> delete from test1 where object_type = 'VIEW' and rownum <100;
SQL> delete from test2 where object_type = 'VIEW' and rownum <100;
SQL> commit;
SQL> select f2() from dual;
F2()
----------
58914853

已用时间: 00: 00: 03.50

 

SQL> select count(*)
from test1, test2
where test1.object_type = test2.object_type
and test1.object_type in ('TABLE SUBPARTITION','VIEW','INDEX','TABLE');
COUNT(*)
----------
58914853
已用时间: 00: 00: 03.50

更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/105131.htm

linux
相关资讯       Oracle函数  PL/SQL 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款