Oracle 11g 的PL/SQL函數結果緩衝

來源:互聯網
上載者:User

標籤:style   color   使用   strong   io   資料   for   2014   

    模仿Oracle效能診斷藝術中的例子做了兩個實驗,書上說如果不用RELIES_ON,則函數依賴的對象發生的變更操作就不會導致結果緩衝的失效操作(result_cache RELIES_ON(test1,test2)),實驗證明不對,函數f1()並沒有使用RELIES_ON,但表上的變化影響到了函數。

C:\Documents and Settings\guogang>sqlplus gg_test/[email protected]_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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.