0% found this document useful (0 votes)
11 views4 pages

Materialized View

A materialized view is a database object that stores precomputed query results, offering better performance than normal views due to its indexed table storage. It is commonly used in data warehouses to maintain local copies of remote data, reduce network traffic, and improve performance for complex queries, with options for automatic refresh based on set frequencies. There are different types of materialized views, including normal and pre-built, with various refresh methods such as complete, fast, and force, each serving different use cases depending on the data update requirements.

Uploaded by

shalih786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Materialized View

A materialized view is a database object that stores precomputed query results, offering better performance than normal views due to its indexed table storage. It is commonly used in data warehouses to maintain local copies of remote data, reduce network traffic, and improve performance for complex queries, with options for automatic refresh based on set frequencies. There are different types of materialized views, including normal and pre-built, with various refresh methods such as complete, fast, and force, each serving different use cases depending on the data update requirements.

Uploaded by

shalih786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Materialized View :

Materialized view is a data base object that stores query result or precomputed
result.

When we see the performance of Materialized view it is better than normal View
because the
data of materialized view will stored in table and table may be indexed so faster.

The Materialized view created for reasons such as:


---------------------------------------------

To maintain the local copy of the remote database object.

To Reduce the network traffic especially during working hours.

To improve the performance of complex group by or joins, complex aggregate


functions.

So the Materialized view widely used in data wharehouses.

Another advantange is refresh option. The refresh is happen automatically based on


the frequency we will set.

Drop -> Drop Materialized view mv1;

Alter -> Alter Materialized View mv1 Refresh Fast;

Types of Materialized View:


---------------------------

1. Normal
2. Pre-Built

Normal Materialized View Syntax:


-------------------------------

CREATE MATERIALIZED VIEW view-name


BUILD [IMMEDIATE | DEFERRED]
REFRESH [FAST | COMPLETE | FORCE ]
ON [COMMIT | DEMAND ]
[[ENABLE | DISABLE] QUERY REWRITE]
AS
SELECT ...;

Pre-Built Materialized View Syntax:


----------------------------------

CREATE MATERIALIZED VIEW view-name


ON PREBUILT TABLE
REFRESH [FAST | COMPLETE | FORCE ]
ON [COMMIT | DEMAND ]
[[ENABLE | DISABLE] QUERY REWRITE]
AS
SELECT ...;

Refreshing Types:
----------------

1. Manual -> EXEC DBMC_MVIEW.REFRESH('MV_NAME','REFRESH_TYPE');

2. Automatic -> It will be instructed on the time of Materialized view creation.

Refreshing Methods of Materialized View : (Default - Complete)


--------------------------------------------------------------

1. Complete
2. Fast - Primary Key Must in the Based table for the creation of MV.
3. Force

Complete Refresh :
-----------------

Complete Refresh -> It will Trunc the Data stored on the materialized view and
then load the fresh
data to the materialized based on the select query in
materialized view.

It can be used in longer time interval refresh.

It take time is much higher time then the Fast Refresh.

Example:
-------
CREATE MATERIALIZED VIEW MV1
BUILD IMMEDIATE
REFRESH COMPLETE
START WITH SYSDATE
NEXT SYSDATE+3/1440
AS
SELECT * FROM products;

Fast Refresh :
------------
Primary Key Must in the Based table for the creation of MV.

It is a Incremental Refresh method means only the incremental changes will be


traced from the materialized view log on the each
table as per the select query in the MV only that changes load to the materialized
view.

In Fast Refresh Materialized View Log Must for all table as per the select
statement in MV otherwise throw error.

It can be used in Shorter time interval refresh.

It take time is much lesser time then the Complete Refresh.

Materialized View Log :


----------------------
It is a incremental Changes tracing file from the last refresh. Once the Fast
Refresh is proceed then the Materialized view log file automatically cleared.

Primary Key Must in the base table to the materialized view log
Example:
--------

CREATE MATERIALIZED VIEW MV1


BUILD IMMEDIATE
REFRESH FAST
START WITH SYSDATE
NEXT SYSDATE+3/1440
AS
SELECT * FROM products;

FAST REFRESH:
------------
In Complete Refresh Method give the first preference to the Fast Refresh at the
time

Materialized view log Found. Else it will automatically proceed the complete
refresh method.

Example :
-------

CREATE MATERIALIZED VIEW MV1


BUILD IMMEDIATE
REFRESH FORCE
START WITH SYSDATE
NEXT SYSDATE+3/1440
AS
SELECT * FROM products;

Refresh Events :
--------------
1. On Demand -> Is the Manual Refresh Method (DBMC_MVIEW.REFRESH)
2. On Commit -> Refresh action will take place on the time of On Commit.

Building Option: (Default Immediate)


----------------
1. Immediate -> It do not wait for first Refresh. Record load immediate on the time
of MV
Created.
2. Deferred -> It must waiting for the first Refresh. Record not load immediate on
the time
of MV Created.

Query Rewrite Option: (Disable)


------------------------------
1. Enable -> Aggregate Function of the query pre-calculated before the start of
Refresh.
2. Disable -> Aggregate Function of the calculated on the time Refresh.

On Prebuilt Table Method:


-------------------------
 The "materialized view on prebuilt table" is used when you have already created a
replicated
table using "create table as select" (CTAS) and now want that table to accept query
rewrite.

 In this case, you can convert an existing table to a materialized view by using
the ON PREBUILT
TABLE clause.

 In name and structure of view must be similar like a replicate table.

Example :
--------

--- Main table creation---


CREATE TABLE t1_mv
( emp_id NUMBER, M_Salary NUMBER );

ALTER TABLE t1_mv ADD CONSTRAINT t1_mv_pk PRIMARY KEY(emp_id);

---Replicated table creation---


CREATE TABLE t2_mv
AS SELECT * FROM t1_mv;
ALTER TABLE t2_mv ADD CONSTRAINT t2_mv_pk PRIMARY KEY(emp_id);

--Prebuilt materialized view creation---


CREATE MATERIALIZED VIEW t2_mv
ON PREBUILT TABLE
REFRESH COMPLETE
AS
SELECT SUM(emp_id) emp_id, SUM(M_Salary) M_Salary
FROM t1_mv
GROUP BY emp_id;

You might also like