纪念2013年展望2014年新的突破,新的进步!
建立物化视图的选项很多,如果在建立物化视图的时候由于缺少权限而报错,ORACLE给出的错误信息仅仅是ORA-01031: 权限不足,而没有清楚的给出是由于缺少哪种权限造成的。
SQL> create user test identified by test;
User created.
SQL> GRANT CREATE SESSION TO TEST;
Grant succeeded.
SQL> SQL> GRANT UNLIMITED TABLESPACE TO TEST;
Grant succeeded.
SQL> grant create table to test;
Grant succeeded.
第一种权限不足,在自己的属主下创建普通物化视图
SQL> conn test/test
Connected.
SQL> CREATE TABLE T (ID NUMBER PRIMARY KEY, NAME VARCHAR2(30));
Table created.
SQL> CREATE MATERIALIZED VIEW MV_T AS SELECT * FROM T;
CREATE MATERIALIZED VIEW MV_T AS SELECT * FROM T
*
ERROR at line 1:
ORA-01031: insufficient privileges
SQL> show user
USER is "SYS"
SQL> grant CREATE MATERIALIZED VIEW to test;
Grant succeeded.
SQL> show user
USER is "TEST"
SQL> CREATE MATERIALIZED VIEW MV_T AS SELECT * FROM T;
Materialized view created.
第二种权限不足,
(1)在自己的属主下创建快速刷新的物化视图
SQL> show user;
USER is "TEST"
SQL> CREATE MATERIALIZED VIEW LOG ON T;
Materialized view log created.
SQL> CREATE MATERIALIZED VIEW MV_T REFRESH FAST AS SELECT * FROM T;
Materialized view created.
(2)创建其他的属主下创建快速刷新的物化视图
SQL> conn xionglang/xionglang
Connected.
SQL> create table xl (ID NUMBER PRIMARY KEY, NAME VARCHAR2(30));
Table created.
SQL> CREATE MATERIALIZED VIEW LOG ON xl;
Materialized view log created.
SQL> grant select on xl to test;
Grant succeeded.
SQL> conn test;
Enter password:
Connected.
SQL> CREATE MATERIALIZED VIEW xionglang_xl REFRESH FAST AS SELECT * FROM xionglang.xl;
CREATE MATERIALIZED VIEW xionglang_xl REFRESH FAST AS SELECT * FROM xionglang.xl
*
ERROR at line 1:
ORA-12018: following error encountered during code generation for
"TEST"."XIONGLANG_XL"
ORA-00942: table or view does not exist
SQL> CREATE MATERIALIZED VIEW xionglang_xl AS SELECT * FROM xionglang.xl;
Materialized view created.
有些奇怪吧!为啥不能创建快速刷新的视图呢?
快速刷新的物化视图和完全刷新的物化视图的主要差别在于,是否需要访问物化视图日志,将用户基表的物化视图日志的查询权限授予用户。
所以这个还需要物化视图日志的权限,才能创建!
SQL> conn xionglang/xionglang
Connected.
SQL> grant select on mlog$_xl to test;
Grant succeeded.
SQL> conn test/test
Connected.
SQL> CREATE MATERIALIZED VIEW xionglang_xl_fast REFRESH FAST AS SELECT * FROM xionglang.xl;
Materialized view created.
第三种权限不足,
创建其他属主下快速刷新,on commit的物化视图
在自己属主下创建没有任何问题。
SQL> CREATE MATERIALIZED VIEW test_t REFRESH FAST on commit as SELECT * FROM t;
Materialized view created.
SQL> CREATE MATERIALIZED VIEW xionglang_xl_fast_commit REFRESH FAST on commit as SELECT * FROM xionglang.xl;
CREATE MATERIALIZED VIEW xionglang_xl_fast_commit REFRESH FAST on commit as SELECT * FROM xionglang.xl
*
ERROR at line 1:
ORA-01031: insufficient privileges
解决办法有两种:
1,授予该表的查询ON COMMIT REFRESH权限
SQL> conn xionglang/xionglang
Connected.
SQL> grant ON COMMIT REFRESH on xl to test;
Grant succeeded.
SQL> conn test/test;
Connected.
SQL> CREATE MATERIALIZED VIEW xionglang_xl_fast_commit REFRESH FAST on commit as SELECT * FROM xionglang.xl;
Materialized view created.
2,授予系统的ON COMMIT REFRESH权限
SQL> conn xionglang/xionglang
Connected.
SQL> revoke ON COMMIT REFRESH on xl from test;
Revoke succeeded.
SQL> conn /as sysdba
Connected.
SQL> grant ON COMMIT REFRESH to test;
Grant succeeded.
SQL> conn test
Enter password:
Connected.
SQL> drop MATERIALIZED VIEW xionglang_xl_fast_commit;
Materialized view dropped.
SQL> CREATE MATERIALIZED VIEW xionglang_xl_fast_commit REFRESH FAST on commit as SELECT * FROM xionglang.xl;
Materialized view created.
同理其他属主下创建query rewrite的物化视图
1,授予该表的查询QUERY REWRITE权限
2,授予系统的GLOBAL QUERY REWRITE权限