验证:refresh on commit语句不能对任意的物化视图的每一种情况维护同步,
而是对于单一表的物化视图或没有任何聚集的连接则可以。
反例:是不是对连表的物化视图或者有聚集的连表的语句用refresh on commit语句创建的物化视图,
那么此物化视图是不会保持和主表保持同步的。
验证如下:
新建两张表
create table emp as select * from scott.emp;
create table dept as select * from scott.dept;
SQL> grant query rewrite to test;
Grant succeeded.
SQL> alter session set query_rewrite_enabled=true;
Session altered.
SQL> alter session set query_rewrite_integrity=enforced;
Session altered.
创建连表的物化视图,并且是具有refresh on commit语句
SQL> create materialized view emp_dept1
build immediate
2 3 refresh on commit
4 enable query rewrite
5 as
6 select dept.deptno, dept.dname, count (*) as count_num
7 from emp, dept
8 where emp.deptno = dept.deptno
9 group by dept.deptno, dept.dname;
Materialized view created.
SQL> select *
from emp_dept1 2
3 ;
DEPTNO DNAME COUNT_NUM
---------- -------------- ----------
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6
SQL> select sum(count_num)
from emp_dept1 2 ;
SUM(COUNT_NUM)
--------------
14
插入一条记录:
SQL> insert into emp(empno,ename,job,mgr,sal,hiredate,deptno)
values(2000,'daimin','DBA',2001,2000,sysdate,20); 2
1 row created.
SQL> commit;
Commit complete.
则:
SQL> select *
2 from emp_dept1
3 ;
DEPTNO DNAME COUNT_NUM
---------- -------------- ----------
10 ACCOUNTING 3
20 RESEARCH 6 --比原来的多了一条记录
30 SALES 6
SQL>
select sum(count_num)
SQL> 2 from emp_dept1
3 ;
SUM(COUNT_NUM)
--------------
15 --总数也多一条
做了实验发现并oralce9i并不是说上说的那样,对与连表的物化视图还是做了及时的更新操作
下面一段是从oracle10g中的Database SQL Reference文档中的CREATE MATERIALIZED VIEW中讲ON COMMIT Clause的:
ON COMMIT Clause
Specify ON COMMIT to indicate that a fast refresh is to occur whenever the database commits a transaction that
operates on a master table of the materialized view. This clause may increase the time taken to complete the commit,
because the database performs the refresh operation as part of the commit process.
Restrictions on Refreshing ON COMMIT
This clause is not supported for materialized views containing object types.
If you specify this clause, you cannot subsequently execute a distributed transaction on any master table of
this materialized view. For example, you cannot insert into the master by selecting from a remote table.
The ON DEMAND clause does not impose this restriction on subsequent distributed transactions on master tables
翻译:
ON COMMIT语句
定义ON COMMIT语句目的是告知数据库一旦有对所创建的物化视图的一个主表进行操作的事务并且提交该事务时,则数据库需要一个及时更新。
因为数据库将对物化视图的这个刷新操作作为提交事务的一部分,所以提交事务时需要消耗更多的时间。
提交时候的刷新操作的一些限制:
这个语句的功能是不支持的含有对象类型字段的物化视图。如果你定义了这个语句,你就不能够对物化试图的任何一个主表执行一个分布式事务。
比如:当查询一个远程的主表的是数据时,你不能向这个主表插入数据。ON COMMIT语句不会对主表的分布式事务强加这个限制。
按照文档的理解:
应该只要是本地的并且定义了ON COMMIT语句的物化视图,在当对物化视图的主表有DML操作并且提交事务的时候都会及时刷新物化视图,只不过
这个刷新会消耗时间。