Lecture 08 Materialized View
Department of Computer Science Kardan University
Materialized View
Lec-08
Prepared by
Bilal Khan
Lecture 08 Materialized View
2
 A materialized view (MV) is a database object that
stores the results of a query at a single point in time.
Unlike a view, materialized view is not virtual.
 Materialized views are sometimes referred to as
snapshots.
 Materialized views may be stored locally or remotely in
other site.
Lecture 08 Materialized View
3
 Increase query performance since it contains results of a query.
 Remote materialized views are an efficient way to replicate
data at different sites compared to fully consistent distributed
data.
◦ Do not require dedicated network connection and network load is
reduced.
◦ Efficiently support remote users
Lecture 08 Materialized View
4
 Materialized View Management
◦ Define materialized view
◦ Refresh materialized view
 Data of a materialized view may be out-of-date and require to be
refreshed.
◦ Drop materialized view
Lecture 08 Materialized View
5
 Basic Syntax
CREATE MATERIALIZED VIEW view-name
BUILD [IMMEDIATE | DEFERRED]
REFRESH [FAST | COMPLETE | FORCE ]
ON [COMMIT | DEMAND ]
[ON PREBUILT TABLE]
AS
SELECT ...;
Lecture 08 Materialized View
6
 Types of Materialized Views
◦ Read-Only Materialized Views
◦ Updatable Materialized Views
Lecture 08 Materialized View
7
 Types of Materialized Views
◦ Read-Only Materialized Views
◦ Records in the MV cannot be changed by users.
◦ Eliminates the possibility of a materialized view introducing
data conflicts with the master (base tables).
Lecture 08 Materialized View
8
 Types of Materialized Views
◦ Updatable Materialized Views
◦ Users can make changes to the data at the materialized view
site.
◦ Changes made to an updatable materialized view are pushed
back to the master during refresh.
Lecture 08 Materialized View
9
 Refresh Method of Materialized Views
◦ Complete Refresh
◦ Fast Refresh
 Differential Refresh
 On Comitt
Lecture 08 Materialized View
10
 Materialized Views Log
◦ A materialized view log is required on a master if you want to perform a
fast refresh on materialized views based on the master. The log is used
to record changes to the master.
◦ For Example
CREATE MATERIALIZED VIEW LOG ON customer;
Lecture 08 Materialized View
11
 Materialized Views Log
◦ When DML changes are made to master table data, Oracle
Database stores rows describing those changes in the
materialized view log and then uses the materialized view
log to refresh materialized views based on the master table.
◦ This process is called incremental or fast refresh.
Lecture 08 Materialized View
12
 Materialized Views Log
◦ Behaviors of Materialized view by using:
 mlog$_tablename
 SELECT COUNT(*) FROM mlog$_tablename
 SELECT * FROM mlog$_tablename
Lecture 08 Materialized View
13
 Materialized Views Creation
◦ CREATE MATERIALIZED VIEW view_name
REFRESH FAST ON COMMIT AS
SELECT * FROM table_name;
Lecture 08 Materialized View
14
 Materialized Views Creation
desc user_snapshots
SELECT name, table_name, refresh_method
FROM user_snapshots;
Lecture 08 Materialized View
15
 Materialized Views Creation ON DEMAND
◦ CREATE MATERIALIZED VIEW view_name
REFRESH FAST ON DEMAND
AS SELECT * FROM table_name;
Lecture 08 Materialized View
16
 Materialized Views Creation ON DEMAND
◦ To reflect the changes of master table in
materialized view we have to execute following
code.
execute DBMS_MVIEW.REFRESH( ‘name_of_view' );
Lecture 08 Materialized View
17
 Materialized Views Creation using Refresh COMPLETE
◦ CREATE MATERIALIZED VIEW view_name
REFRESH COMPLETE
START WITH SYSDATE
NEXT SYSDATE + 1
AS SELECT * FROM table_name;
Lecture 08 Materialized View
18
 Materialized Views Creation Updatable
◦ CREATE MATERIALIZED VIEW view_name
for UPDATE
AS SELECT * FROM table_name;
Lecture 08 Materialized View
19
Have a Nice Day

Lect 08 materialized view

  • 1.
    Lecture 08 MaterializedView Department of Computer Science Kardan University Materialized View Lec-08 Prepared by Bilal Khan
  • 2.
    Lecture 08 MaterializedView 2  A materialized view (MV) is a database object that stores the results of a query at a single point in time. Unlike a view, materialized view is not virtual.  Materialized views are sometimes referred to as snapshots.  Materialized views may be stored locally or remotely in other site.
  • 3.
    Lecture 08 MaterializedView 3  Increase query performance since it contains results of a query.  Remote materialized views are an efficient way to replicate data at different sites compared to fully consistent distributed data. ◦ Do not require dedicated network connection and network load is reduced. ◦ Efficiently support remote users
  • 4.
    Lecture 08 MaterializedView 4  Materialized View Management ◦ Define materialized view ◦ Refresh materialized view  Data of a materialized view may be out-of-date and require to be refreshed. ◦ Drop materialized view
  • 5.
    Lecture 08 MaterializedView 5  Basic Syntax CREATE MATERIALIZED VIEW view-name BUILD [IMMEDIATE | DEFERRED] REFRESH [FAST | COMPLETE | FORCE ] ON [COMMIT | DEMAND ] [ON PREBUILT TABLE] AS SELECT ...;
  • 6.
    Lecture 08 MaterializedView 6  Types of Materialized Views ◦ Read-Only Materialized Views ◦ Updatable Materialized Views
  • 7.
    Lecture 08 MaterializedView 7  Types of Materialized Views ◦ Read-Only Materialized Views ◦ Records in the MV cannot be changed by users. ◦ Eliminates the possibility of a materialized view introducing data conflicts with the master (base tables).
  • 8.
    Lecture 08 MaterializedView 8  Types of Materialized Views ◦ Updatable Materialized Views ◦ Users can make changes to the data at the materialized view site. ◦ Changes made to an updatable materialized view are pushed back to the master during refresh.
  • 9.
    Lecture 08 MaterializedView 9  Refresh Method of Materialized Views ◦ Complete Refresh ◦ Fast Refresh  Differential Refresh  On Comitt
  • 10.
    Lecture 08 MaterializedView 10  Materialized Views Log ◦ A materialized view log is required on a master if you want to perform a fast refresh on materialized views based on the master. The log is used to record changes to the master. ◦ For Example CREATE MATERIALIZED VIEW LOG ON customer;
  • 11.
    Lecture 08 MaterializedView 11  Materialized Views Log ◦ When DML changes are made to master table data, Oracle Database stores rows describing those changes in the materialized view log and then uses the materialized view log to refresh materialized views based on the master table. ◦ This process is called incremental or fast refresh.
  • 12.
    Lecture 08 MaterializedView 12  Materialized Views Log ◦ Behaviors of Materialized view by using:  mlog$_tablename  SELECT COUNT(*) FROM mlog$_tablename  SELECT * FROM mlog$_tablename
  • 13.
    Lecture 08 MaterializedView 13  Materialized Views Creation ◦ CREATE MATERIALIZED VIEW view_name REFRESH FAST ON COMMIT AS SELECT * FROM table_name;
  • 14.
    Lecture 08 MaterializedView 14  Materialized Views Creation desc user_snapshots SELECT name, table_name, refresh_method FROM user_snapshots;
  • 15.
    Lecture 08 MaterializedView 15  Materialized Views Creation ON DEMAND ◦ CREATE MATERIALIZED VIEW view_name REFRESH FAST ON DEMAND AS SELECT * FROM table_name;
  • 16.
    Lecture 08 MaterializedView 16  Materialized Views Creation ON DEMAND ◦ To reflect the changes of master table in materialized view we have to execute following code. execute DBMS_MVIEW.REFRESH( ‘name_of_view' );
  • 17.
    Lecture 08 MaterializedView 17  Materialized Views Creation using Refresh COMPLETE ◦ CREATE MATERIALIZED VIEW view_name REFRESH COMPLETE START WITH SYSDATE NEXT SYSDATE + 1 AS SELECT * FROM table_name;
  • 18.
    Lecture 08 MaterializedView 18  Materialized Views Creation Updatable ◦ CREATE MATERIALIZED VIEW view_name for UPDATE AS SELECT * FROM table_name;
  • 19.
    Lecture 08 MaterializedView 19 Have a Nice Day

Editor's Notes

  • #6 BUILD clause options are shown below. IMMEDIATE : The materialized view is populated immediately. DEFERRED : The materialized view is populated on the first requested refresh. The following refresh types are available. FAST : A fast refresh is attempted. If materialized view logs are not present against the source tables in advance, the creation fails. COMPLETE : The table segment supporting the materialized view is truncated and repopulated completely using the associated query. FORCE : A fast refresh is attempted. If one is not possible a complete refresh is performed. A refresh can be triggered in one of two ways. ON COMMIT : The refresh is triggered by a committed data change in one of the dependent tables. ON DEMAND : The refresh is initiated by a manual request or a scheduled task. The ON PREBUILT TABLE clause tells the database to use an existing table segment, which must have the same name as the materialized view and support the same column structure as the query.