以前没有function result cache,要实现类似效果,自定义函数比如是DETERMINISTIC并且建立函数索引,走索引才行,
当然,如果函数改变,但是索引未rebuild,则索引里存的函数值还是不变的,必须rebuild索引。
使用result cache,ORACLE会自动跟踪变化
11g R2后不需要 RESULT_CACHE RELIES_ON (t1) ,只要RESULT_CACHE,ORACLE自己会跟踪失效
函数结果集缓存,可以减少对相同参数的自定义函数执行,从而提高效率,主要是在SQL里使用自定义函数
create or replace function result_cache_test(v_owner in varchar2)
return number
RESULT_CACHE
is
v_object_id number;
begin
select max(object_id) into v_object_id from t where owner=v_owner;
return v_object_id;
end;
/
-- 可以看到 MAX(OBJECT_ID)只执行1次,因为前10行的owner是一样的
select owner from t where rownum<10;
OWNER
------------------------------
SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
SYS
select result_cache_test(owner) from t where rownum<10;
select result_cache_test(owner)
from
t where rownum<10
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 3 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 1126 0 9
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.00 0 1129 0 9
SELECT MAX(OBJECT_ID)
FROM
T WHERE OWNER=:B1
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.00 0.00 0 1098 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 3 0.00 0.00 0 1098 0 1
===== 如果去掉resutl_cache,则执行9次,不管参数是否一样
SELECT MAX(OBJECT_ID)
FROM
T WHERE OWNER=:B1
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 9 0.00 0.00 0 0 0 0
Fetch 9 0.03 0.02 1089 9882 0 9
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 19 0.03 0.03 1089 9882 0 9
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7655508/viewspace-3117894/ ,如需转载,请注明出处,否则将追究法律责任。