Professional Documents
Culture Documents
Materialized View
Materialized View
MATERIALIZED VIEWS
A Materialized View is effectively a database table that contains the results of a query. The power of materialized views comes from the fact that, once created, Oracle can automatically synchronize a materialized view's data with its source information as required with little or no programming effort. Materialized views can be used for many purposes, including:
Views vs Materialized Views Like its predecessor the view, materialized views allow you to store the definition of a query in the database.
select * from MV ;
KEY VAL ---------- ----1a 2b 3c 4 KEY VAL ---------- ----1a 2b 3c 4
ROWID
Table select rowed from T order by rowid ; ROWID -----------------AAAgY9AAEAAAAVfAAA AAAgY9AAEAAAAVfAAB AAAgY9AAEAAAAVfAAC AAAgY9AAEAAAAVfAAD View select rowid from V order by rowid ; ROWID -----------------AAAgY9AAEAAAAVfAAA AAAgY9AAEAAAAVfAAB AAAgY9AAEAAAAVfAAC AAAgY9AAEAAAAVfAAD Materialized View select rowid from MV order by rowid ; ROWID -----------------AAAgZFAAEAAADyEAAA AAAgZFAAEAAADyEAAB AAAgZFAAEAAADyEAAC AAAgZFAAEAAADyEAAD
DIFFERENCE
Table update t set val = upper(val); select * from T ; View Materialized View
'MV' );
Materialized View
select * from T ;
select * from V ;
select * from MV ;
REFRESH COMPLETE
There are various ways to refresh the data in a materialized view, the simplest way being a complete refresh. When a complete refresh occurs the materialized view's defining query is executed and the entire result set replaces the data currently residing in the materialized view. The REFRESH COMPLETE clause tells Oracle to perform complete refreshes by default when a materialized view is refreshed.
REFRESH
REFRESH COMPLETE
Let's see a complete refresh in action now. We will use the DBMS_MVIEW.REFRESH procedure to initiate it. The "list" parameter accepts a list of materialized views to refresh (in our case we only have one) and the "method" parameter accepts a "C", for Complete refresh.
REFRESH COMPLETE
execute DBMS_MVIEW.REFRESH( LIST => 'MV', METHOD => 'C' );
select key, val, rowid from mv ;
KEY VAL ROWID ---------- ----- -----------------1 a AAAWgHAAEAAAAIEAAE 2 b AAAWgHAAEAAAAIEAAF 3 c AAAWgHAAEAAAAIEAAG 4 AAAWgHAAEAAAAIEAAH
Note how the rowids in the second query differ from those of the first, even though the data in table T was unchanged throughout. This is because complete refreshes create a whole new set of data, even when the new result set is identical to the old one.
Cleanup
describe T
Name
-------------------------------------------KEY VAL
Null?
--------
Type
------------------------------
describe MLOG$_T
Name KEY SNAPTIME$$ DMLTYPE$$ OLD_NEW$$ CHANGE_VECTOR$$ Null? Type NUMBER DATE VARCHAR2(1) VARCHAR2(1) RAW(255) -------------------------------------------- -------- ------------------------------
The MLOG$_T.KEY column mirrors the base table's primary key column T.KEY. The other MLOG$ columns are system generated.
UPDATE t set val = upper( val ) where KEY = 1 ; INSERT into t ( KEY, val ) values ( 5, 'e' ); column dmltype$$ format a10 select key, dmltype$$ from MLOG$_T ;
KEY DMLTYPE$$ ---------- ---------1U 5I If the changes affecting T are rolled back, so are the changes to MLOG$_T.
create materialized view log on t WITH PRIMARY KEY ; desc mlog$_t Name Null? Type -------------------------------------------- -------- -----------------------------KEY NUMBER SNAPTIME$$ DATE DMLTYPE$$ VARCHAR2(1) OLD_NEW$$ VARCHAR2(1) CHANGE_VECTOR$$ RAW(255) Note how MLOG$_T contains T's primary key column, T.KEY. This materialized view log is equivalent to the one created earlier in this topic, which did not have a WITH clause, because WITH PRIMARY KEY is the default option when no WITH clause is specified.
ROLLBACK EFFECT
rollback ; Rollback complete. select key, dmltype$$ from MLOG$_T ;
no rows selected
desc mlog$_t Name KEY M_ROW$$ SNAPTIME$$ DMLTYPE$$ OLD_NEW$$ CHANGE_VECTOR$$ Null? Type NUMBER VARCHAR2(255) DATE VARCHAR2(1) VARCHAR2(1) RAW(255)
In this case both KEY and M_ROW$$ appear in the log table.
UPDATE T set val = upper(val) where key = 5 ; UPDATE T2 set amt = 333 where key = 60 ;
commit;
Since mixed DML is a common occurrence SEQUENCE will be specified in most materialized view logs. In fact, Oracle recommends it. "Oracle recommends that the keyword SEQUENCE be included in your materialized view log statement unless you are sure that you will never perform a mixed DML operation (a combination of INSERT, UPDATE, or DELETE operations on multiple tables)."
desc mlog$_t
Name -------------------------------------------KEY VAL SNAPTIME$$ DMLTYPE$$ OLD_NEW$$ CHANGE_VECTOR$$ Null? -------Type -----------------------------NUMBER VARCHAR2(5) DATE VARCHAR2(1) VARCHAR2(1) RAW(255)
Note how both the old and the new values are stored in the same column, VAL. The OLD_NEW$$ column identifies the value as either an old or a new value.
REFRESH FAST
Now that we know how materialized view logs track changes to base tables we can use them to perform fast materialized view refreshes, i.e. refreshes where only the individual materialized view rows affected by base table changes are updated. This is also called "incremental" refreshing. Earlier in this tutorial we saw how the rowids for each row in a materialized view changed after a complete refresh. Now let's see what happens to a materialized view's rowids after a fast refresh. First we use the REFRESH FAST clause to specify that the default refresh method should be fast.
create materialized view log on t with sequence ; create materialized view mv REFRESH FAST as select * from t;
REFRESH FAST
select key, val, rowid from mv ; KEY VAL ROWID ---------- ----- -----------------1 a AAAWm+AAEAAAAaMAAA 2 b AAAWm+AAEAAAAaMAAB 3 c AAAWm+AAEAAAAaMAAC 4 AAAWm+AAEAAAAaMAAD Now we refresh the materialized view. The "F" value for the "method" parameter ensures the refresh will be a Fast one. execute dbms_mview.refresh( list => 'MV', method => 'F' ); select key, val, rowid from mv ;
REFRESH FAST
KEY VAL ROWID ---------- ----- -----------------1 a AAAWm+AAEAAAAaMAAA 2 b AAAWm+AAEAAAAaMAAB 3 c AAAWm+AAEAAAAaMAAC 4 AAAWm+AAEAAAAaMAAD The rowids did not change. Thus, with a fast refresh the materialized view data is not touched when no changes have been made to the base table, unlike a complete refresh where each row would have been created a new.
REFRESH FAST
Now let's update a row in the base table.
---------- -----