Saturday , December 21 2024

Materialized Views In Oracle

In today’s article, we will be explaining the concept of Materialized Views in Oracle, also known as Snapshots.

It is a structure that stores the data returned by a query in a table and updates this data with the main table at certain intervals.

The user who will create Mview must have “CREATE MATERIALIZED VIEW” and “CREATE TABLE” system permissions.

Some important terms related to Materialized Views.

Materialized Views: It is a database object that replicates data and improves performance.

Master Table: The table whose data is queried.

Complete Refresh: It is the deletion of the MView table and complete renewal of the main table.

Fast Refresh: Only DML changes in the main table are applied to Mview.

Refresh Method: It determines the renewal method. It can be Complete or Fast.

Materialized View Log: It is created when Fast Refresh is used. It is the database object that tracks DML changes in the main table.

Build Mode: It determines the Mview mode. It can be Immediate or Deferred.

Refresh Mode: It determines the renewal mode. It can be Demand, on Commit or Never.

Query Rewrite: It allows the Optimizer to consider the Mview SQL work plan instead of the main table.

Refresh Group: It is the group where the MVs to be refreshed at the same time are defined.

“Complete Refresh” Materialized Views that will completely retrieve the data from the main table each time are created as follows.

If the view is queried after it is created, it will return results immediately. Because we made the BUILD parameter in the view IMMEDIATE.

Even if we perform insert operations as below after the view is created, the result will be the same since we do not refresh it.

Mviews are renewed with the DBMS_MVIEW package as follows.

The DBMS_MVIEW package has 2 parameters. The first is the MV name and the other is the Mview renewal method. The value “C” is used for Complete renewal and “F” is used for Fast renewal.

Fast Refresh Mview is created as follows.

1. Mview Log is created. Mview Log is a database object that tracks DML changes in the main table. In order to create an Mview Log, there must be a primary key in our main table.

If we cannot re-create the primary key, Mview Log is created according to rowid.

2. Then Fast Refresh Mview is created.

Mview Fast is refreshed as follows.

When Mview is created, we may not want it to be refreshed with data immediately.

In this case, we create the BUILD mode as DEFERRED.

In some cases, when a data change occurs in our main table, we may want the change to be reflected in Mview immediately.

In this case, we use the phrase “ON COMMIT”.

Finally, materialized views can be created in a way that never refreshes. And these views cannot be refreshed.

Afterwards, when the insertion process is performed and the query is made again, it is seen that the number has not changed.

Finally, when you try to refresh the view, you get an error.

A view that has been made Never Refresh can be made refreshable again.

Afterwards, when the view is executed, it is observed that the number has changed.

Loading

About Onur ARDAHANLI

Leave a Reply

Your email address will not be published. Required fields are marked *