Skip to Main Content
  • Questions
  • What causes a materialized view to get invalidated

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Livio.

Asked: December 23, 2015 - 1:32 pm UTC

Last updated: November 20, 2017 - 1:50 am UTC

Version: 11.2.0.4.0

Viewed 50K+ times! This question is

You Asked

Hello,

I have a materialized view whose definition looks like this:

CREATE MATERIALIZED VIEW <owner>.<materialized view name> (<column list>)
TABLESPACE <tablespace name>
PCTUSED 0
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
NOCACHE
LOGGING
NOCOMPRESS
NOPARALLEL
BUILD IMMEDIATE
REFRESH FORCE ON DEMAND
WITH PRIMARY KEY
AS
<materialized view query>;

The <materialized view query> is quite a complicated query involving a few tables, not any view, and is refreshed nightly via a job.
What is going on is that, during the day, periodically, the materialized view, and the ones based on it, are set to an INVALID state,
as can be seen by inspecting the user_objects view. A successful recompilation makes the problem disappear only temporarily and that's quite annoying.
Could you shed some light on the reasons why the mv gets invalidated and how to avoid it?

Now, I realize this one is by no means a great question and it may lack a lot of information.
But I was not able to sort it out in any other way and browsing the net did not prove helpful.

Thanks in advance for any help you will provide.

Warm regards

Livio


and Connor said...

Structural change to an underlying table is a common cause

SQL> create table T as select * from dba_Objects where object_id is not null;

Table created.

SQL>
SQL> alter table T add primary key ( object_id);

Table altered.

SQL>
SQL> drop MATERIALIZED VIEW MV;

Materialized view dropped.

SQL>
SQL> CREATE MATERIALIZED VIEW MV
  2  BUILD IMMEDIATE
  3  REFRESH FORCE ON DEMAND
  4  WITH PRIMARY KEY
  5  AS
  6  select owner, count(object_id)
  7  from t
  8  group by owner;

Materialized view created.

SQL>
SQL> select object_type, status from user_Objects where object_name = 'MV';

OBJECT_TYPE             STATUS
----------------------- -------
MATERIALIZED VIEW       VALID
TABLE                   VALID

SQL>
SQL> alter table T add x int;

Table altered.

SQL>
SQL> select object_type, status from user_Objects where object_name = 'MV';

OBJECT_TYPE             STATUS
----------------------- -------
MATERIALIZED VIEW       INVALID
TABLE                   VALID


Check the LAST_DDL_TIME on objects in your query.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

PMO

Rajeshwaran, Jeyabal, December 24, 2015 - 6:16 am UTC

Other option would be partition maintenance operation.

rajesh@ORA11G>
rajesh@ORA11G> alter table t truncate partition p2;

Table truncated.

rajesh@ORA11G>
rajesh@ORA11G> select object_name,object_type,status
  2  from user_objects
  3  where object_name ='T_MV';

OBJECT_NAME                    OBJECT_TYPE         STATUS
------------------------------ ------------------- -------
T_MV                           TABLE               VALID
T_MV                           MATERIALIZED VIEW   INVALID

2 rows selected.

rajesh@ORA11G> 
rajesh@ORA11G> alter materialized view t_mv compile;

Materialized view altered.

rajesh@ORA11G> select object_name,object_type,status
  2  from user_objects
  3  where object_name ='T_MV';

OBJECT_NAME                    OBJECT_TYPE         STATUS
------------------------------ ------------------- -------
T_MV                           TABLE               VALID
T_MV                           MATERIALIZED VIEW   VALID

2 rows selected.

rajesh@ORA11G> alter table t add partition p3;

Table altered.

rajesh@ORA11G> select object_name,object_type,status
  2  from user_objects
  3  where object_name ='T_MV';

OBJECT_NAME                    OBJECT_TYPE         STATUS
------------------------------ ------------------- -------
T_MV                           TABLE               VALID
T_MV                           MATERIALIZED VIEW   INVALID

2 rows selected.

rajesh@ORA11G>

MV imp from DMP doesnt copy the Timed refresh criteria

Udit Vardhan, November 16, 2017 - 6:47 pm UTC

we implemented the timed refresh on one of or MVs. when we create DMP from this env to refresh lower environments, expdp process somehow leaves refresh criteria. resulting in MV that doesn't refresh itself and we end up modifying view to alter with the refresh condition to refresh in every five minutes.

any suggestions ??
using 11gR2, refresh condition as below that is not copied in DMP imports-

REFRESH FORCE ON DEMAND START WITH sysdate + 0 NEXT SYSDATE + 1/288
Connor McDonald
November 20, 2017 - 1:50 am UTC

We'd need to see a full test case.