Materialized View
Materialized View
For datawarehousing purposes, mviews commonly created are aggregate views, single-table
aggregate views and join views.
In replication environments, mviews commonly created are primary key, rowid and
subquery materialized views.
Whenever you create a materialized view, regardless of it's type, always specify the schema
name of the table owner in the query for the materialized view.
Prerequisites:
To create mviews, the user should have any one of
CREATE MATERIALIZED VIEW or CREATE ANY MATERIALIZED VIEW privileges.
SQL> GRANT CREATE MATERIALIZED VIEW TO user-name;
And
SQL> GRANT QUERY REWRITE TO user-name;
And following init parameters should be set
query_rewrite_enabled = true (default)
query_rewrite_integrity = enforced|trusted|stale_tolerated
The background processes responsible for these materialized view refreshes are
the coordinated job queue (CJQ) processes.
job_queue_processes=n
Syntax:
CREATE MATERIALIZED VIEW mview-name
[partitioning-options]
[storage-parameters]
[TABLESPACE tablespace-name]
[OF object-type]
[FOR UPDATE]
[BUILD IMMEDIATE|BUILD DEFFERED|ON PREBUILT TABLE]
[REFRESH [FAST|COMPLETE|FORCE|NEVER]
[ON DEMAND|COMMIT]
[START WITH date]
[NEXT date]
[WITH PRIMARY KEY|ROWID]]
[DISABLE|ENABLE QUERY REWRITE]
AS select-query;
Refresh Types
Oracle can refresh a materialized view using either a fast, complete or force refresh.
The refresh option specifies:
a. Refresh method used by Oracle to refresh data in materialized view. FORCE is the default
option.
b. Whether the view is primary key based or row-id based. PRIMARY KEY is the default
option.
c. Time and interval at which the view is to be refreshed.
Complete Refresh
To perform COMPLETE refresh of a materialized view, the server that manages the
materialized view executes the materialized view's defining query, which essentially
recreates the materialized view. To refresh the materialized view, the result set of the query
replaces the existing materialized view data. Oracle can perform a complete refresh for any
materialized view. Depending on the amount of data that satisfies the defining query, a
complete refresh can take a substantially longer amount of time to perform than a fast
refresh.
Note: If a materialized view is complete refreshed, then set it's PCTFREE to 0 and PCTUSED
to 99 for maximum efficiency.
The complete refresh re-creates the entire materialized view. If we request a complete
refresh, Oracle performs a complete refresh even if a fast refresh is possible.
SQL> CREATE MATERIALIZED VIEW mv_emp
REFRESH COMPLETE
START WITH SYSDATE NEXT SYSDATE + 1
WITH PRIMARY KEY
AS SELECT * FROM emp@remote_db;
To refresh this mview,
SQL> EXEC DBMS_MVIEW.REFRESH('mv_emp', 'C');
From Oracle 10g, complete refresh of single materialized view can do delete instead of
truncate. To force the refresh to do truncate instead of delete, parameter ATOMIC_REFRESH
must be set to false.
ATOMIC_REFRESH = FALSE, mview will be truncated and whole data will be inserted. The
refresh will go faster, and no undo will be generated.
ATOMIC_REFRESH = TRUE (default), mview will be deleted and whole data will be inserted.
Undo will be generated. We will have access at all times even while it is being refreshed.
SQL> EXEC DBMS_MVIEW.REFRESH('mv_emp', 'C', atomic_refresh=>FALSE);
If we perform complete refresh of a master materialized view, then the next refresh
performed on any materialized views based on this master materialized view must be a
complete refresh. If a fast refresh is attempted for such a materialized view after it's master
materialized view has performed a complete refresh, then Oracle returns the following error:
ORA-12034 mview log is younger than last refresh
Fast Refresh
To perform FAST refresh, the master that manages the materialized view first identifies the
changes that occurred in the master since the most recent refresh of the materialized view
and then applies these changes to the materialized view. Fast refreshes are more efficient
than complete refreshes when there are few changes to the master because the
participating server and network replicate a smaller amount of data.
We can perform fast refreshes of materialized views only when the master table or master
materialized view has a materialized view log. Also, for fast refreshes to be faster than
complete refreshes, each join column in the CREATE MATERIALIZED VIEW statement must
have an index on it.
SQL> CREATE MATERIALIZED VIEW mv_emp
BUILD IMMEDIATE
REFRESH FAST
START WITH SYSDATE NEXT SYSDATE + 2
WITH PRIMARY KEY
ENABLE QUERY REWRITE
AS SELECT * FROM emp@remote_db;
master table's data so that a materialized view defined on the master table can be refreshed
incrementally.
We should create a materialized view log for the master tables if we specify the REFRESH
FAST clause.
SQL> CREATE MATERIALIZED VIEW LOG ON emp;
To refresh this mview,
SQL> EXEC DBMS_MVIEW.REFRESH('mv_emp', 'F');
After a direct path load on a master table or master materialized view using SQL*Loader, a
fast refresh does not apply the changes that occurred during the direct path load. Also, fast
refresh does not apply changes that result from other types of bulk load operations on
masters. Examples of these operations include some INSERT statements with
anAPPEND hint and some INSERT ... SELECT * FROM statements.
Note:
->> Fast refreshable materialized views can be created based on master tables and master
materialized views only.
->> Materialized views based on a synonym or a view must be complete refreshed.
->> Materialized views are not eligible for fast refresh if the defined subquery contains an
analytic function.
Force Refresh
To perform FORCE refresh of a materialized view, the server that manages the materialized
view attempts to perform a fast refresh. If fast refresh is not possible, then Oracle performs
complete refresh. Use the force setting when you want a materialized view to refresh if fast
refresh is not possible.
If you do not specify a refresh method, FORCE is the default.
SQL> CREATE MATERIALIZED VIEW mv_emp
REFRESH FORCE
START WITH SYSDATE NEXT SYSDATE + 3
WITH PRIMARY KEY
DISABLE QUERY REWRITE
AS SELECT * FROM emp@remote_db;
To refresh this mview,
SQL> EXEC DBMS_MVIEW.REFRESH(LIST =>'mv_emp', METHOD =>'?');
(or)
SQL> EXEC DBMS_MVIEW.REFRESH(LIST =>'mv_emp');
Enhanced Partition Change Tracking (EPCT) Refresh refers to PCT based refresh applied to
MVs containing columns that are partition-join dependent on the partitioning column of the
base table.
1. Read only
Cannot be updated and complex materialized views are supported.
2. Updateable
Can be updated even when disconnected from the master site.
3. Writeable
Created with the for update clause.
1.
Do not use column aliases when we are creating an updatable
materialized view. Column aliases cause an error when we attempt to add
the materialized view to a materialized view group using the
CREATE_MVIEW_REPOBJECT procedure.
2.
An updatable materialized view based on a master table or master
materialized view that has defined column default values does not
automatically use the master's default values.
3.
Updatable materialized views do not support the DELETE CASCADE
constraint.
The following types of materialized views cannot be masters for updatable
materialized views:
ROWID materialized views
We can use materialized views to achieve one or more of the following goals:
Less network loads
Both the master site and the materialized view site must have
compatibility level (COMPATIBLE initialization parameter) 9.0.1 or higher to
replicate user-defined types and any objects on which they are based.
We cannot create refresh-on-commit materialized views based on a
master with user-defined types. Refresh-on-commit materialized views are
those created using the ON COMMIT REFRESH clause in the CREATE
MATERIALIZED VIEW statement.
Advanced Replication does not support type inheritance.
Refresh Groups
Managing MVs is much easier in Oracle 10g with the introduction of the
powerful new tuning advisors that can tell us a lot about the design of the
MVs. Tuning recommendations that can generate a complete script that can
be implemented quickly, saving significant time and effort. The ability to
force rewriting or abort the query can be very helpful in decision-support
systems where resources must be conserved, and where a query that is not
rewritten should not be allowed to run amuck inside the database.
Related Views
DBA_MVIEWS
DBA_MVIEW_LOGS
DBA_MVIEW_KEYS
DBA_REGISTERED_MVIEWS
DBA_REGISTERED_MVIEW_GROUPS
DBA_MVIEW_REFRESH_TIMES
DBA_MVIEW_ANALYSIS
Related Package/Procedures
DBMS_MVIEW package
REFRESH
REFRESH_ALL
REFRESH_ALL_MVIEWS
REFRESH_DEPENDENT
REGISTER_MVIEW
UNREGISTER_MVIEW
PURGE_LOG
DBMS_REPCAT package
DBMS_REFRESH package
Row ID: The materialized view records changes to the master table or
master materialized view based on the ROWID of the affected rows.
Object ID: The materialized view records changes to the master object
table or master object materialized view based on the object identifier
(ID) of the affected row objects.
must be COMPLETE refreshed because the rowids of the master table have
changed.