视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Oracle物化视图失效的几种情况及测试
2020-11-09 13:00:33 责编:小采
文档


物化视图(Materialized Views)是查询的结果集,所有原表和视图的变更都将导致物化视图的失效,最近就发现了一个物化视图经常失

说明:物化视图(Materialized Views)是查询的结果集,所有原表和视图的变更都将导致物化视图的失效,,最近就发现了一个物化视图经常失效的问题,以下是整理的文档。

一、物化视图状态查询:Oracle提供了一个视图用于查询物化视图的状态USER_MVIEWS,其中列STALENESS,用于显示当前物化视图的状态

Relationship between the contents of the materialized view and the contents of the materialized view's masters:
•FRESH - Materialized view is a read-consistent view of the current state of its masters(最新状态:当前物化视图的内容出于最新的状态)
•STALE - Materialized view is out of date because one or more of its masters has changed. If the materialized view was FRESH before it became STALE, then it is a read-consistent view of a former state of its masters.(陈旧状态:物化视图引用的主表已经更新,但是物化视图没有刷新,所以内容相对主表来说是旧的)

•NEEDS_COMPILE - Some object upon which the materialized view depends has changed. An ALTER MATERIALIZED VIEW...COMPILE statement is required to validate this materialized view and compute the staleness of the contents.(需要编译:物化视图引用的主表比如视图,进行了重建后相应的物化视图就需要编译,当处于这种状态的时候dba_objects视图显示的STATUS为INVALID)

需要运行语句:ALTER MATERIALIZED VIEW MV_NAME COMPILE;进行重新编译;

•UNUSABLE - Materialized view is not a read-consistent view of its masters from any point in time(物化视图引用的主表状态不确定)
•UNKNOWN - Oracle Database does not know whether the materialized view is in a read-consistent view of its masters from any point in time (this is the case for materialized views created on prebuilt tables)(未知:通过prebuilt创建的表)
•UNDEFINED - Materialized view has remote masters. The concept of staleness is not defined for such materialized views.(物化视图引用的表来自其他的数据库,一般通过dblink链接过来的)

二、实验测试:

2.1 创建物化视图

CREATE MATERIALIZED VIEW MV_TEST (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)

TABLESPACE USERS

PCTUSED 0

PCTFREE 10

INITRANS 2

MAXTRANS 255

STORAGE (

INITIAL K

NEXT 1M

MINEXTENTS 1

MAXEXTENTS UNLIMITED

PCTINCREASE 0

BUFFER_POOL DEFAULT

FLASH_CACHE DEFAULT

CELL_FLASH_CACHE DEFAULT

)

NOCACHE

LOGGING

NOCOMPRESS

NOPARALLEL

BUILD IMMEDIATE

USING INDEX

TABLESPACE USERS

PCTFREE 10

INITRANS 2

MAXTRANS 255

STORAGE (

INITIAL K

NEXT 1M

MINEXTENTS 1

MAXEXTENTS UNLIMITED

PCTINCREASE 0

BUFFER_POOL DEFAULT

FLASH_CACHE DEFAULT

CELL_FLASH_CACHE DEFAULT

)

REFRESH FORCE ON DEMAND

WITH PRIMARY KEY

AS

/* Formatted on 2014/12/30 16:36:55 (QP5 v5.215.120.387) */

SELECT "EMP"."EMPNO" "EMPNO",

"EMP"."ENAME" "ENAME",

"EMP"."JOB" "JOB",

"EMP"."MGR" "MGR",

"EMP"."HIREDATE" "HIREDATE",

"EMP"."SAL" "SAL",

"EMP"."COMM" "COMM",

"EMP"."DEPTNO" "DEPTNO"

FROM "SCOTT"."EMP" "EMP"

WHERE "EMP"."DEPTNO" = 20;

COMMENT ON MATERIALIZED VIEW MV_TEST IS 'snapshot table for snapshot SCOTT.MV_TEST';

CREATE UNIQUE INDEX PK_EMP1 ON MV_TEST

(EMPNO)

LOGGING

TABLESPACE USERS

PCTFREE 10

INITRANS 2

MAXTRANS 255

STORAGE (

INITIAL K

NEXT 1M

MINEXTENTS 1

MAXEXTENTS UNLIMITED

PCTINCREASE 0

BUFFER_POOL DEFAULT

FLASH_CACHE DEFAULT

CELL_FLASH_CACHE DEFAULT

)

NOPARALLEL;

2.2 查询当前物化视图的状态

SQL> SELECT mview_name,staleness FROM DBA_MVIEWS WHERE MVIEW_NAME='MV_TEST';

MVIEW_NAME STALENESS

------------------------------ -------------------

MV_TEST FRESH

2.3 查询数据库物化视图的状态

column OBJECT_NAME format a20;

column STATUS format a20; OBJECT_TYPE

column OBJECT_TYPE format a20;

SQL> select OBJECT_NAME,STATUS,OBJECT_TYPE from dba_objects where OBJECT_NAME='MV_TEST' ;

OBJECT_NAME STATUS OBJECT_TYPE

-------------------- -------------------- --------------------

MV_TEST VALID TABLE

MV_TEST VALID MATERIALIZED VIEW

2.4 修改源表的数据

SQL> SELECT mview_name,staleness FROM DBA_MVIEWS WHERE MVIEW_NAME='MV_TEST';

MVIEW_NAME STALENESS

------------------------------ -------------------

MV_TEST NEEDS_COMPILE

显示状态需要编译

SQL> ALTER MATERIALIZED VIEW SCOTT.MV_TEST COMPILE;

进行相应的编译

SQL> SELECT mview_name,staleness FROM DBA_MVIEWS WHERE MVIEW_NAME='MV_TEST';

MVIEW_NAME STALENESS

------------------------------ -------------------

MV_TEST STALE

编译完成后,状态变成STALE

2.5 修改源表的表结构测试

SQL> ALTER TABLE SCOTT.EMP RENAME COLUMN COMM TO COMMS; //修改源表的结构

SQL> SELECT mview_name,staleness FROM DBA_MVIEWS WHERE MVIEW_NAME='MV_TEST';//查看物化视图的状态

MVIEW_NAME STALENESS

------------------------------ -------------------

MV_TEST NEEDS_COMPILE

SQL> ALTER MATERIALIZED VIEW SCOTT.MV_TEST COMPILE; //重新编译

SQL> SELECT mview_name,staleness FROM DBA_MVIEWS WHERE MVIEW_NAME='MV_TEST'; //重新编译状态没变;

MVIEW_NAME STALENESS

------------------------------ -------------------

MV_TEST NEEDS_COMPILE

SQL> select OBJECT_NAME,STATUS,OBJECT_TYPE from dba_objects where OBJECT_NAME='MV_TEST' ;

OBJECT_NAME STATUS OBJECT_TYPE

-------------------- -------------------- --------------------

MV_TEST VALID TABLE

MV_TEST INVALID MATERIALIZED VIEW

显示物化视图的状态INVALID

2.5 修改源表的结构跟物化视图一致

SQL> ALTER TABLE SCOTT.EMP RENAME COLUMN COMMS TO COMM; //修改源表的结构

SQL> SELECT mview_name,staleness FROM DBA_MVIEWS WHERE MVIEW_NAME='MV_TEST'; //需要编译

MVIEW_NAME STALENESS

------------------------------ -------------------

MV_TEST NEEDS_COMPILE

SQL> ALTER MATERIALIZED VIEW SCOTT.MV_TEST COMPILE; //进行重新编译

Materialized view altered.

SQL> SELECT mview_name,staleness FROM DBA_MVIEWS WHERE MVIEW_NAME='MV_TEST'; //状态变成STALE

MVIEW_NAME STALENESS

------------------------------ -------------------

MV_TEST STALE

SQL> select OBJECT_NAME,STATUS,OBJECT_TYPE from dba_objects where OBJECT_NAME='MV_TEST' ; //状态变成VALID

OBJECT_NAME STATUS OBJECT_TYPE

-------------------- -------------------- --------------------

MV_TEST VALID TABLE

MV_TEST VALID MATERIALIZED VIEW

总结:当物化视图的源表重新编译了,如果重建后的表结构没有发现变化,那么运行脚本ALTER MATERIALIZED VIEW MV_NAME COMPILE后物化视图的状态就会刷新成有效的;

但是如果表的结构发生了变化,那么需要重新修改物化视图的脚本,相应的物化视图才能有效,dba_objects显示出来的状态才是VALID的状态;

物化视图定义不当引发Oracle性能问题

Oracle物化视图测试

Oracle【定期刷新】的【基于连接】的物化视图

Oracle物化视图创建报ORA-00942错误解决

Oracle 存储过程刷新物化视图

Oracle物化视图的使用

本文永久更新链接地址:

下载本文
显示全文
专题