物理视图涉及UNION ALL操作符在FROM和ON COMMIT中

时间:2013-07-11 17:56:41

标签: oracle materialized-views union-all

我有两张桌子'调查'& 'survey_processed'和两者基本相似。我有一个工作查询,它​​使用两个表之间的UNION ALL运算符&我有些重要。当我尝试将其转换为物化视图时,我得到与ON COMMIT相关的错误。查看MV DDL&错误如下。


    CREATE MATERIALIZED VIEW vwm_survey_records_count
    REFRESH FAST ON COMMIT
    AS
    SELECT
        survey_combined.survey_header_id,
        COUNT(*) AS count_total,
        COUNT(CASE WHEN survey_combined.processed_flag = 'Y' THEN 1 ELSE NULL END) AS count_a,
        COUNT(CASE WHEN survey_combined.approved_flag IS NULL THEN 1 ELSE NULL END) AS count_b,
        COUNT(CASE WHEN survey_combined.processed_flag = 'N' AND survey_combined.approved_flag = 'Y' THEN 1 ELSE NULL END) AS count_c,
        COUNT(CASE WHEN survey_combined.approved_flag = 'N' THEN 1 ELSE NULL END) AS count_d
    FROM 
    (
    SELECT survey_header_id, 'N' AS processed_flag, approved_flag FROM survey
    UNION ALL
    SELECT survey_header_id, 'Y' AS processed_flag, approved_flag FROM survey_processed) survey_combined
    INNER JOIN survey_header ON survey_combined.survey_header_id = survey_header.id
    GROUP BY survey_combined.survey_header_id;

如果我运行上述命令,我会得到错误:
'SQL错误:ORA-12054:无法为实例化视图设置ON COMMIT刷新属性'。

但是,如果我使用'REFRESH COMPLETE ON DEMAND'就行了。我知道显然打破了对ON COMMIT属性的一些限制,但无法弄清楚哪个。有人能让我知道上面的查询有什么问题吗?此外,有没有更好的方法来查询,以使其高效和在创建MV时使用'REFRESH FAST ON COMMIT'。

注意:我在所选列上使用rowid为两个表创建了MV Log。
如果有人有任何问题,请告诉我。

提前致谢。

这是'jonearles'

所要求的MV Log的DDL

    CREATE MATERIALIZED VIEW LOG ON survey WITH SEQUENCE,ROWID (id, survey_header_id, approved_flag, processed_flag) INCLUDING NEW VALUES;
    CREATE MATERIALIZED VIEW LOG ON survey_processed WITH SEQUENCE,ROWID (id, survey_header_id, approved_flag) INCLUDING NEW VALUES;
    CREATE MATERIALIZED VIEW LOG ON survey_header WITH SEQUENCE,ROWID (id) INCLUDING NEW VALUES;

注意:“调查”表中的“processed_flag”列稍后将被删除。从技术上讲,最近根据'processed_flag'列的值拆分了两个表。因此,“调查”表包含所有未处理的记录(processed_flag ='N')& 'survey_processed'具有已处理的记录(processed_flag ='Y')。拆分后,该列无关紧要。

1 个答案:

答案 0 :(得分:2)

我觉得你在这里运气不好。来自Restrictions on Fast Refresh on Materialized Views with UNION ALL

  

定义查询必须在顶层具有UNION ALL运算符。

用外连接替换UNION ALL也不起作用。聚合和外连接似乎不能一起工作。下面的代码与您的代码不同,但它表明即使是简化版本的查询也无效:

CREATE MATERIALIZED VIEW vwm_survey_records_count
REFRESH COMPLETE ON DEMAND AS
SELECT survey_header.id AS survey_header_id, COUNT(*) AS count_total
FROM survey_header, survey, survey_processed
WHERE survey_header.id = survey.survey_header_id(+)
GROUP BY survey_header.id;


delete from MV_CAPABILITIES_TABLE;
begin
    DBMS_MVIEW.EXPLAIN_MVIEW ('VWM_SURVEY_RECORDS_COUNT');
end;
/
select possible, msgno, msgtxt
from MV_CAPABILITIES_TABLE
where capability_name = 'REFRESH_FAST_AFTER_INSERT';

POSSIBLE    MSGNO   MSGTXT
--------    -----   ------
N           2048    outer join in mv