[20121101]物化檢視與表(Materialized Views and Tables).txt
[20121101]物化檢視與表(Materialized Views and Tables).txt
1.建立測試環境:
SQL> select * from v$version where rownum<=1;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
SQL> grant dba to test identified by xxxxxx;
Grant succeeded.
SQL> create table deptx as select * from scott.dept ;
Table created.
2.測試建立物化檢視1:
connect test/xxxxxx;
create materialized view mv_deptx as select count(*) from deptx;
SQL> create materialized view mv_deptx as select count(*) from deptx;
Materialized view created.
SQL> desc mv_deptx
Name Null? Type
--------- -------- -------
COUNT(*) NUMBER
--欄位名使用count(*)不好.
SQL> drop materialized view mv_deptx;
Materialized view dropped.
SQL> create materialized view mv_deptx as select count(*) dept_count from deptx;
Materialized view created.
SQL> select * from user_objects where object_type in ('TABLE','MATERIALIZED VIEW');
OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS T G S NAMESPACE
-------------------- -------------------- --------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ----------
MV_DEPTX 117700 MATERIALIZED VIEW 2012-11-01 17:10:28 2012-11-01 17:10:28 2012-11-01:17:10:28 VALID N N N 19
DEPTX 117696 117696 TABLE 2012-11-01 17:07:07 2012-11-01 17:07:07 2012-11-01:17:07:07 VALID N N N 1
MV_DEPTX 117699 117699 TABLE 2012-11-01 17:10:28 2012-11-01 17:10:28 2012-11-01:17:10:28 VALID N N N 1
--可以發現建立物化檢視的同時,會建立相同名字的表.
SQL> drop materialized view mv_deptx;
Materialized view dropped.
SQL> select * from user_objects where object_type in ('TABLE','MATERIALIZED VIEW');
OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS T G S NAMESPACE
-------------------- -------------------- --------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ----------
DEPTX 117696 117696 TABLE 2012-11-01 17:07:07 2012-11-01 17:07:07 2012-11-01:17:07:07 VALID N N N 1
--如果刪除物化檢視,對應的表也同時刪除.
3.測試建立物化檢視2:
SQL> create materialized view mv_deptx on prebuilt table as select count(*) dept_count from deptx;
create materialized view mv_deptx on prebuilt table as select count(*) dept_count from deptx
*
ERROR at line 1:
ORA-12059: prebuilt table "TEST"."MV_DEPTX" does not exist
--如果採用on prebuilt table模式,必須先建立表.
SQL> create table mv_deptx(a number);
Table created.
SQL> create materialized view mv_deptx as select count(*) dept_count from deptx;
create materialized view mv_deptx as select count(*) dept_count from deptx
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
--如果存在對應的表,建立時必須加入 on prebuilt table 引數.
SQL> create materialized view mv_deptx on prebuilt table as select count(*) dept_count from deptx;
create materialized view mv_deptx on prebuilt table as select count(*) dept_count from deptx
*
ERROR at line 1:
ORA-12060: shape of prebuilt table does not match definition query
--可以發現欄位命令無匹配,無法建立.
SQL> ALTER TABLE TEST.MV_DEPTX RENAME COLUMN A TO dept_count;
Table altered.
SQL> create materialized view mv_deptx on prebuilt table as select count(*) dept_count from deptx;
Materialized view created.
SQL> select * from user_objects where object_type in ('TABLE','MATERIALIZED VIEW');
OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS T G S NAMESPACE
-------------------- -------------------- --------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ----------
MV_DEPTX 117701 117701 TABLE 2012-11-01 17:18:41 2012-11-01 17:20:32 2012-11-01:17:20:32 VALID N N N 1
DEPTX 117696 117696 TABLE 2012-11-01 17:07:07 2012-11-01 17:07:07 2012-11-01:17:07:07 VALID N N N 1
MV_DEPTX 117702 MATERIALIZED VIEW 2012-11-01 17:20:51 2012-11-01 17:20:51 2012-11-01:17:20:51 VALID N N N 19
--看看這個時候是否可以刪除表?
SQL> drop table mv_deptx;
drop table mv_deptx
*
ERROR at line 1:
ORA-12083: must use DROP MATERIALIZED VIEW to drop "TEST"."MV_DEPTX"
--可以發現無法刪除.
SQL> drop materialized view mv_deptx;
Materialized view dropped.
SQL> select * from user_objects where object_type in ('TABLE','MATERIALIZED VIEW');
OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS T G S NAMESPACE
-------------------- -------------------- --------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ----------
MV_DEPTX 117701 117701 TABLE 2012-11-01 17:18:41 2012-11-01 17:20:32 2012-11-01:17:20:32 VALID N N N 1
DEPTX 117696 117696 TABLE 2012-11-01 17:07:07 2012-11-01 17:07:07 2012-11-01:17:07:07 VALID N N N 1
--可以發現刪除物化檢視,表依舊存在.
總結:
第1種方式建立物化檢視,建立時同時建立表和檢視.刪除物化檢視,同時消失.
第2種方式建立物化檢視(on prebuilt table),建立物化檢視前,先要建立表,才能物化檢視時. 如果刪除物化檢視,表保持存在.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-748153/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- materialized view (物化檢視)ZedView
- oracle10g partition分割槽表與物化檢視materialized viewOracleZedView
- 建立物化檢視MV ( Materialized View )ZedView
- Validating Tables, Indexes, Clusters, and Materialized ViewsIndexZedView
- 轉:物化檢視(Materialized View)介紹ZedView
- oracle10g materialized view物化檢視示例OracleZedView
- ogg 同步 物化檢視建立限制 Materialized ViewZedView
- 物化檢視妙用__表同步使用物化檢視方法
- oracle10g nested materialized view巢狀物化檢視示例OracleZedView巢狀
- zt_yangtinkung_ITPUB知識索引貼——物化檢視materialized view索引ZedView
- 物化檢視(Materialized View)的重新整理回滾約束ZedView
- 物化檢視(Materialized View)的重新整理回滾測試ZedView
- 物化檢視日誌表被DROP後建立物化檢視報錯
- 物化檢視日誌(materialized view log)引起大量Dfs Lock Handle等待ZedView
- Overview of Materialized Views (184)ViewZed
- Refresh Materialized Views (189)ZedView
- Views and Base Tables (243)View
- 物化檢視的快速重新整理測試與物化檢視日誌
- [DW]An article about Materialized Views(zz)ZedView
- Extents in Materialized Views and Their Logs (18)ZedView
- 【物化檢視】幾種物化檢視日誌分析
- [20230225]12c Real-time materialized view 實時物化檢視的應用.txtZedView
- 建立遠端基表的物化檢視
- trigger和物化檢視同步表
- 【物化檢視】根據物化檢視日誌快速重新整理物化檢視的過程
- oracle物化檢視Oracle
- Materialized Views in data warehouse environment (185)ZedView
- [20120808]學習物化檢視.txt
- Materialized Views in data distributed environment (186)ZedView
- 物化檢視詳解
- oracle 建立物化檢視Oracle
- Oracle 物化檢視建立Oracle
- 物化檢視 on commitMIT
- DB2資料庫物化檢視:MQT物化查詢表的使用DB2資料庫MQQT
- [20131001]ctas與檢視user_tables,user_objects.txtObject
- 物化檢視日誌與增量重新整理
- oracle筆記整理10——效能調優之臨時表與物化檢視Oracle筆記
- Oracle如何根據物化檢視日誌快速重新整理物化檢視Oracle