Materialized Views: Snapshots
Materialized Views: Snapshots
A materialized view is a database object that stores the results of a query (possibly from a remote
database). Materialized views are sometimes referred to as snapshots
When creating a materialized view, you have the option of specifying whether the refresh occurs ON
DEMAND or ON COMMIT. In case of an ON COMMIT scenario, when a transaction commits, the
materialized view is refreshed and as a result, the data is always current in a materialized view. In case
of an ON DEMAND type, calling the dbms_mview package procedures refreshes the materialized view
Example
Force a refresh:
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;
/
exec DBMS_REFRESH.REFRESH('my_grp');
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
Monitoring
Here are some views that can be queried to obtain more information about materialized views:
You can use materialized views in data warehouses to increase the speed of queries on very large
databases. Queries to large databases often involve joins between tables, aggregations such as
SUM, or both. These operations are expensive in terms of time and processing power. The type of
materialized view you create determines how the materialized view is refreshed and used by
query rewrite.
You can use materialized views in a number of ways, and you can use almost identical syntax to
perform a number of roles. For example, a materialized view can replicate data, a process
formerly achieved by using the CREATE SNAPSHOT statement. Now CREATE MATERIALIZED VIEW
is a synonym for CREATE SNAPSHOT.
Materialized views improve query performance by precalculating expensive join and aggregation
operations on the database prior to execution and storing the results in the database. The query
optimizer automatically recognizes when an existing materialized view can and should be used
to satisfy a request. It then transparently rewrites the request to use the materialized view.
Queries go directly to the materialized view and not to the underlying detail tables. In general,
rewriting queries to use materialized views rather than detail tables improves response. Figure 8-
1 illustrates how query rewrite works.