日期:2014-05-16  浏览次数:20555 次

Oracle使用实例证明实体化视图和视图的区别
使用实例证明实体化视图和视图的区别
scott@TICKET> show user;
USER 为 "SCOTT"
scott@TICKET> create table t( key int primary key, val varchar(25));
create table t( key int primary key, val varchar(25))
             *
第 1 行出现错误:
ORA-00955: 名称已由现有对象使用


scott@TICKET> drop table t;

表已删除。
创建基础表
scott@TICKET> create table t( key int primary key, val varchar(25));

表已创建。
插入基础数据
scott@TICKET> insert into t  values(1,'a');

已创建 1 行。

scott@TICKET> insert into t  values(2,'b');

已创建 1 行。

scott@TICKET> insert into t  values(3,'c');

已创建 1 行。

scott@TICKET> commit;

提交完成。

scott@TICKET> select * from t;

       KEY VAL
---------- -------------------------
         1 a
         2 b
         3 c
创建视图和物化视图
scott@TICKET> create view v as select * from t;
create view v as select * from t
            *
第 1 行出现错误:
ORA-01031: 权限不足


scott@TICKET> conn / as sysdba
已连接。

GLOBAL_NAME
--------------------------------------------
sys@TICKET
给scott创建视图和物化视图的授权
sys@TICKET> grant create any view ,create any materialized view to scott;

授权成功。

sys@TICKET> conn scott/tiger
已连接。

GLOBAL_NAME
--------------------------------------------
scott@TICKET

scott@TICKET> create view v as select * from t;

视图已创建。

scott@TICKET> select * from v;

       KEY VAL
---------- -------------------------
         1 a
         2 b
         3 c

scott@TICKET> select rowid,a.* from t a;

ROWID                     KEY VAL
------------------ ---------- -------------------------
AAASzoAAEAAABHlAAA          1 a
AAASzoAAEAAABHlAAB          2 b
AAASzoAAEAAABHlAAC          3 c

scott@TICKET> select rowid,a.* from v a;

ROWID                     KEY VAL
------------------ ---------- -------------------------
AAASzoAAEAAABHlAAA          1 a
AAASzoAAEAAABHlAAB          2 b
AAASzoAAEAAABHlAAC          3 c

scott@TICKET> create materialized view mv  as
  2  select * from t;

实体化视图已创建。

scott@TICKET> select rowid,a.* from mv a;

ROWID                     KEY VAL
------------------ ---------- -------------------------
AAASzrAAEAAABH0AAA          1 a
AAASzrAAEAAABH0AAB          2 b
AAASzrAAEAAABH0AAC          3 c

由上面:
 查询t,v,mv的信息可以看出mv的rowid和其他的不一样.
 
scott@TICKET> update t set val='aa' where key=1;

已更新 1 行。

scott@TICKET> commit;

提交完成。

scott@TICKET> select * from t;

       KEY VAL
---------- -------------------------
         1 aa
         2 b
         3 c

scott@TICKET> select * from v;

       KEY VAL
---------- -------------------------
         1 aa
         2 b
         3 c

scott@TICKET> select * from mv;

       KEY VAL
---------- -------------------------
         1 a
         2 b
         3 c
由上面可以查看当基表t变化,t和v的查询结果相应的发生变化.但是mv的数据不变化.

scott@TICKET> host

scott@TICKET> conn /as sysdba
已连接。

GLOBAL_NAME
--------------------------------------------
sys@TICKET

sys@TICKET> conn scott/tiger
已连接。

GLOBAL_NAME
--------------------------------------------
scott@TICKET
刷新物化视图的信息
scott@TICKET> exec dbms_mview.refresh('MV');

PL/SQL 过程已成功完成。

scott@TICKET> select * from mv;

       KEY VAL
---------- -------------------------
         1 aa
         2 b
         3 c

scott@TICKET> update t set val='aa' where key=2;

已更新 1 行。

scott@TICKET> select * from t;

       KEY VAL
---------- -------------------------
         1 aa
         2 aa
         3 c

scott@TICKET> select * from v;

       KEY VAL
---------- -------------------------
         1 aa
         2 aa
         3 c
由上面可以看出mv刷新之后,mv,t,v的数据一致.

scott@TICKET> select * from mv;

       KEY VAL
---------- -------------------------
         1 aa
         2 b
         3 c


在创建物化视图的时候指定为快速更新视图
scott@TICKET> create materialized view mv_t refresh fast as select * from t;
create materialized view mv_t refresh fast as select * from t
                                                            *
第 1 行出现错误:
ORA-23413: 表 "SCOTT"."T" 不带实体化视图日志


scott@TICKET> create materialized view log on t;

实体化视