Materialized Views in Oracle
A materialized view, or snapshot as they were previously known, is a table segment whose contents are periodically refreshed based on a query, either against a local or remote table. Using materialized views against remote tables is the simplest way to achieve replication of data between sites. The example code in this article assumes DB1 is the master instance and DB2 is the materialized view site.
- Check Privileges
- Create Materialized View
- Create Materialized View Logs
- Cleaning Up
- Aggregations and Transformations
- Considerations
Check Privileges
Check the user who will own the materialized views has the correct privileges.
CONNECT sys@db2 GRANT CREATE DATABASE LINK TO scott; GRANT CREATE MATERIALIZED VIEW TO scott;
Create Materialized View
Connect to the materialized view owner and create the database link and the materialized view itself.
CONNECT scott/tiger@db2
CREATE DATABASE LINK DB1.WORLD CONNECT TO scott IDENTIFIED BY tiger USING 'DB1.WORLD';
CREATE MATERIALIZED VIEW emp_mv
BUILD IMMEDIATE
REFRESH FORCE
ON DEMAND
AS
SELECT * FROM emp@db1.world;
BEGIN
DBMS_STATS.gather_table_stats(
ownname => 'SCOTT',
tabname => 'EMP_MV');
END;
/
The FORCE parameter means the materialized view will attempt a fast refresh. If this cannot be performed a complete refresh will occur.
Create a refresh group, defined to refresh every minute and assign our materialized view to it.
BEGIN
DBMS_REFRESH.make(
name => 'SCOTT.MINUTE_REFRESH',
list => '',
next_date => SYSDATE,
interval => '/*1:Mins*/ SYSDATE + 1/(60*24)',
implicit_destroy => FALSE,
lax => FALSE,
job => 0,
rollback_seg => NULL,
push_deferred_rpc => TRUE,
refresh_after_errors => TRUE,
purge_option => NULL,
parallelism => NULL,
heap_size => NULL);
END;
/
BEGIN
DBMS_REFRESH.add(
name => 'SCOTT.MINUTE_REFRESH',
list => 'SCOTT.EMP_MV',
lax => TRUE);
END;
/
At this point the materialized view is created and will refresh every minute.
Create Materialized View Logs
Since a complete refresh involves truncating the materialized view segment and re-populating it using the related query, it can be quite time consuming and involve a considerable amount of network traffic when performed against a remote table. To reduce the replication costs, materialized view logs can be created to capture all changes to the base table since the last refresh. This information allows a fast refresh, which only needs to apply the changes rather than a complete refresh of the materialized view.
To take advantage of the of the fast refresh, connect to the master instance and create the materialized view log.
CONNECT scott/tiger@db1 CREATE MATERIALIZED VIEW LOG ON scott.emp TABLESPACE users WITH PRIMARY KEY INCLUDING NEW VALUES;
Cleaning Up
To clean up we must remove all objects.
CONNECT scott/tiger@db2 DROP MATERIALIZED VIEW emp_mv; DROP DATABASE LINK DB1.WORLD; BEGIN DBMS_REFRESH.destroy(name => 'SCOTT.MINUTE_REFRESH'); END; / CONNECT scott/tiger@db1 DROP MATERIALIZED VIEW LOG ON scott.emp;
Aggregations and Transformations
Materialized views can be used to improve the performance of a variety of queries, including those performing aggregations and transformations of the data. This allows the work to be done once and used repeatedly by multiple sessions, reducing the total load on the server.
The following query does an aggregation of the data in the EMP table.
CONN scott/tiger SET AUTOTRACE TRACE EXPLAIN SELECT deptno, SUM(sal) FROM emp GROUP BY deptno; --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3 | 21 | 4 (25)| 00:00:01 | | 1 | HASH GROUP BY | | 3 | 21 | 4 (25)| 00:00:01 | | 2 | TABLE ACCESS FULL| EMP | 14 | 98 | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------
Create a materialized view to perform the aggregation in advance, making sure you specify the ENABLE QUERY REWRITE clause.
CREATE MATERIALIZED VIEW emp_aggr_mv BUILD IMMEDIATE REFRESH FORCE ON DEMAND ENABLE QUERY REWRITE AS SELECT deptno, SUM(sal) AS sal_by_dept FROM emp GROUP BY deptno; EXEC DBMS_STATS.gather_table_stats(USER, 'EMP_AGGR_MV');
The same query is now rewritten to take advantage of the pre-aggregated data in the materialized view, instead of the session doing the work for itself.
--ALTER SESSION SET QUERY_REWRITE_INTEGRITY = TRUSTED; --ALTER SESSION SET QUERY_REWRITE_ENABLED = TRUE; SET AUTOTRACE TRACE EXPLAIN SELECT deptno, SUM(sal) FROM emp GROUP BY deptno; -------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3 | 21 | 3 (0)| 00:00:01 | | 1 | MAT_VIEW REWRITE ACCESS FULL| EMP_AGGR_MV | 3 | 21 | 3 (0)| 00:00:01 | --------------------------------------------------------------------------------------------
Considerations
Before using materialized views and materialized view logs, consider the following:
- Populating a materialized view adds load to both servers involved. The source server is queried to capture the data, which is inserted into the destination server. Be sure the additional load does not adversely affect your primary system.
- Although materialized view logs improve the performance of materialized view refreshes, they do increase the work needed to perform DDL on the base table. Check the additional work does nos adversely affect performance on the primary system.
- If regular refreshes are not performed, materialized view logs can grow very large, potentially reducing the performance of their maintenance and blowing tablespace limits.
- Depending on the Oracle version and the complexity of the associated query, fast refreshes may not be possible.
- When using materialized views to improve performance of transformations and aggregations, the
QUERY_REWRITE_INTEGRITYandQUERY_REWRITE_ENABLEDparameters must be set or the server will not be able to automatically take advantages of query rewrites. These parameters may be set in the pfile or spfile file if they are needed permanently. Later releases have them enabled by default.
For more information see:
Hope this helps. Regards Tim...
![]() |

