文档包括general restrictions on materialised view 和materialised view with aggregates。
虽然处理这些列表很有启发性,但您可以通过检查来自the dbms_mview.explain_mview procedure 的结果来了解现有或潜在的物化视图是否可以快速刷新:
set serveroutput on
declare
msg_array SYS.ExplainMVArrayType;
begin
dbms_mview.explain_mview (q'[
SELECT SUBJECTID ,count(*)as totalcount ,avg(price)as avgprice,sum(price) as totalprice
FROM your_table
WHERE SUBJECTID='xxxxx'
GROUP by SUBJECTID
]',
msg_array);
for i in msg_array.first..msg_array.last loop
dbms_output.put_line(rpad(msg_array(i).capability_name, 30)
||' '|| msg_array(i).possible
||' '|| msg_array(i).msgtxt);
end loop;
end;
/
...
REFRESH_FAST F
...
REFRESH_FAST_AFTER_INSERT F agg(expr) requires correspondng COUNT(expr) function
REFRESH_FAST_AFTER_ONETAB_DML F SUM(expr) without COUNT(expr)
REFRESH_FAST_AFTER_ONETAB_DML F see the reason why REFRESH_FAST_AFTER_INSERT is disabled
REFRESH_FAST_AFTER_ANY_DML F see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled
REFRESH_FAST_PCT F PCT is not possible on any of the detail tables in the materialized view
...
正如@vercelli 提到的,以及REFRESH_FAST_AFTER_INSERT 消息所暗示的,您需要将count(*) 更改为count(price)。但这还不是全部。如果你只是改变,你会看到:
REFRESH_FAST F
...
REFRESH_FAST_AFTER_INSERT F mv log does not have all necessary columns
REFRESH_FAST_AFTER_ONETAB_DML F see the reason why REFRESH_FAST_AFTER_INSERT is disabled
REFRESH_FAST_AFTER_ONETAB_DML F COUNT(*) is not present in the select list
REFRESH_FAST_AFTER_ANY_DML F see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled
您的物化视图日志必须包含您正在聚合的列:
CREATE MATERIALIZED VIEW LOG ON your_table WITH SEQUENCE, ROWID
(SUBJECTID,PRICE)
INCLUDING NEW VALUES;
Materialized view LOG created.
CREATE MATERIALIZED VIEW ih_data_aggregated_view
PARALLEL
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS
SELECT SUBJECTID ,count(price)as totalcount ,avg(price)as avgprice,sum(price) as totalprice
FROM your_table
WHERE SUBJECTID='xxxxx'
GROUP by SUBJECTID;
Materialized view IH_DATA_AGGREGATED_VIEW created.
仍会报告缺少的count(*),但由于这是针对单个表的,因此不会阻止快速刷新。值得注意的是,如果您的price 列可以为空,那么count(price) 和count(*) 可能会给出不同的结果;如果是这种情况,您可能希望将这两个计数都作为列在您的 MV 中。