一般來說,你想要做的是在這種情況下是採取標量子查詢緩存advatage。
即認沽:
Select general.column1, general.column2, general.column1-general.column2
from (select (select package.function1('I take a long time') from dual) column1,
(select package.function2('I take even longer') from dual) column2,
normal_column
from bigtable) general;
delcaring的功能deterministic
太多幫助,如果它是確定的。
一個小例子:
SQL> create or replace function testfunc(i varchar2)
2 return varchar2
3 is
4 begin
5 dbms_application_info.set_client_info(userenv('client_info')+1);
6 return 'hi';
7 end;
8/
Function created.
現在可以測試調用函數就像你有:
SQL> exec dbms_application_info.set_client_info(0);
PL/SQL procedure successfully completed.
SQL> set autotrace traceonly
SQL> select *
2 from (select testfunc(owner) a
3 from all_objects);
57954 rows selected.
SQL> select userenv('client_info') from dual;
USERENV('CLIENT_INFO')
----------------------------------------------------------------
57954
函數被調用57954次(每行一次)。現在讓我們使用標量緩存:
SQL> exec dbms_application_info.set_client_info(0);
PL/SQL procedure successfully completed.
SQL> select *
2 from (select (select testfunc(owner) from dual) a
3 from all_objects);
57954 rows selected.
SQL> select userenv('client_info') from dual;
USERENV('CLIENT_INFO')
----------------------------------------------------------------
178
178調用而不是57k! 在你的案例中,你只顯示了你有一個字面值和每行不變的輸入(如果是這種情況,使用標量緩存後的調用次數應該是1)。
如果我們增加確定性:
SQL> create or replace function testfunc(i varchar2)
2 return varchar2 deterministic
3 is
4 begin
5 dbms_application_info.set_client_info(userenv('client_info')+1);
6 return 'hi';
7 end;
8/
Function created.
SQL> exec dbms_application_info.set_client_info(0);
PL/SQL procedure successfully completed.
SQL> select *
2 from (select (select testfunc(owner) from dual) a
3 from all_objects);
57954 rows selected.
SQL> select userenv('client_info') from dual;
USERENV('CLIENT_INFO')
----------------------------------------------------------------
55
現在下降到55在11g中,我們有result_cache
,我們可以到位的確定性,對subsequant運行到0調用,將減少電話。
謝謝,這正是我正在尋找的,並加快了我的SQL大約1/2的時間。 – Rolan