物化视图"无效"添加约束时

时间:2014-10-23 20:38:35

标签: sql oracle materialized-views

我在Oracle 10.2.0.50数据库中有一个物化视图,如下所示:

CREATE MATERIALIZED VIEW mv_cis
   NOCACHE
   NOLOGGING
   NOCOMPRESS
   NOPARALLEL
   BUILD IMMEDIATE
   REFRESH FORCE
           ON DEMAND
           WITH PRIMARY KEY AS
   SELECT component_id, ctn, visible_tag, facility,
          SYSTEM, elev, parent, room_number,
          remarks, safety_class, seismic, quality_level
     FROM v_cis;

很直接的东西。创建此物化视图后,它将生效并填充我期望的数据。然后我在其表中添加一个约束,如下所示:

ALTER TABLE mv_cis
   MODIFY ctn CONSTRAINT chk_cis_ctn_null NOT NULL ENABLE VALIDATE;

这可以按预期工作,表格获得一个新的约束,并且世界上一切都很好。

然而,我回顾一下物化视图和(使用Toad for Oracle 12)它显示无效。查看user_mviews表格会显示其中COMPILE_STATENEED_COMPILESTALENESSUNDEFINED。所以我跑:

ALTER MATERIALIZED VIEW mv_cis COMPILE;

没有变化。奇怪的是,刷新视图仍然有效,但它让我发疯,它显示无效,并使我的测试计划失败,即使一切看起来都井然有序。那么,我错过了什么?

1 个答案:

答案 0 :(得分:3)

我要说bug,你应该检查Metalink和/或提交SR。

这是11.2场景。当然这很简单。如果您愿意,我可以使用您的实际DDL进行尝试。

SQL> create table base(id integer primary key, name varchar2(100) not null,
  2  description varchar2(400));

Table created.

SQL> desc base
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                        NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(100)
 DESCRIPTION                                        VARCHAR2(400)

SQL> create materialized view mv_base build immediate refresh force on demand
  2  with primary key as
  3  select id, name, description from base
  4  ;

Materialized view created.

SQL> desc mv_base
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                        NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(100)
 DESCRIPTION                                        VARCHAR2(400)

SQL> alter table mv_base modify description constraint chk_not_null not null 
  2  enable validate;

Table altered.

SQL> desc mv_base
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------

 ID                                        NOT NULL NUMBER(38)
 NAME                                      NOT NULL VARCHAR2(100)
 DESCRIPTION                               NOT NULL VARCHAR2(400)

现在检查是否需要编译。

SQL> select mview_name, compile_state, staleness from user_mviews;

MVIEW_NAME                     COMPILE_STATE       STALENESS
------------------------------ ------------------- -------------------
MV_BASE                        NEEDS_COMPILE       NEEDS_COMPILE

SQL> alter materialized view mv_base compile;

Materialized view altered.

SQL> select mview_name, compile_state, staleness from user_mviews;

MVIEW_NAME                     COMPILE_STATE       STALENESS
------------------------------ ------------------- -------------------
MV_BASE                        VALID               FRESH