Materialized views in SQL :
In this article,I will give the basic information about What exactly the Materialized views in SQL.view are nothing but the logical structure of table which will be created from one table or one or more tables.There are 2 types of views in SQL-
Materialized view in SQL is also a logical structure which is stored physically on the disc.Like a view in Materialized views in SQL we are using simple select statement to create it.You should have create materialized views privileges to create a Materialized views.Definition of Materialized views(called as MV) has been stored in databases.Materialized views are useful in Data-warehousing concepts.When you create a Materialized view, Oracle Database creates one internal table and at least one index, and may create one view, all in the schema of the materialized views. Oracle Database uses these objects to maintain the materialized views in SQL data. You must have the privileges necessary to create these objects.
MVs are used in data-warehouse like aggregate materialized views,materialized views with joins etc.
Materialized view in SQL Syntax and Examples:
“Materialized views are also know as snapshots..”
Snapshots acts like a physical table because data from snapshots are storing in to physical memory.Snapshot retrieves data very fast.So for performance tuning Snapshots are used.Following is the synax of materialized view:Create materialized view View_Name Build [Immediate/Deffered] Refresh [Fast/Complete/Force] on [Commit/Demand] as Select ..........;
Using above syntax you can create materialized views.The Syntax includes some different optional fields:
1.Build Immediate: Means materialized views(mv) created immediately.
2.Build Deffered:Means materialized views(mv) created after one refresh.
3.Refresh on commit:
This option committed the data in materialized views in SQL immediately after data inserted and committed in table.This option is known as incremental refresh option.View is not fully refreshed with this option
4.Refresh on Demand:
Using this option you can add the condition for refreshing data in materialized views.
You can refresh the data using fast (incremental approach),Complete,Force options.
CREATE MATERIALIZED VIEW MV_Employee BUILD immediate
on commit SELECT * FROM Employee;
Difference Between Materialized View And View :
|1.View is nothing but the logical structure of the table which will retrieve data from 1 or more table.||1.Materialized views(Snapshots) are also logical structure but data is physically stored in database.|
|2.You need to have Create view privileges to create simple or complex view||2.You need to have create materialized view ‘s privileges to create Materialized views|
|3.Data access is not as fast as materialized views||3.Data retrieval is fast as compare to simple view because data is accessed from directly physical location|
|4.There are 2 types of views:
|4.There are following types of Materialized views:
1.Refresh on Auto
2.Refresh on demand
|5.In Application level views are used to restrict data from database||5.Materialized Views are used in Data Warehousing.|
Materialized View Real Life Example:
Materialized Views are basically used in the scenarios where actual performance tuning for query is needed.Materialized views are used mostly in reports where user wants to fetch the records very fast.I will try to explain the real life scenario where exactly materialized view is useful.When user creates materialized view then one table structure is created and user directly fetches that data from that table structure.
Suppose there are 2 tables named Employee and Department. The Employee table contains 1 million records and department table contains 20 records.We need to fetch the Employees associated with that department.
To Perform above scenario we basically create view:
Create View V_Employee
from Employee E , Department D where E.Dept_no=D.Dept_no;
Fetch the records from the View
Select * from V_Employee;
It will fetch 10 million records with associated department.But to fetch that records check the time.Let us consider it will take 2 Mins means 120 secs to fetch records
Step 3 :
Let us Create materialized view which will refresh automatically.
Create or Replace Materialized view MV_Employee
from Employee E , Department D where E.Dept_no=D.Dept_no
Refresh auto on commit select * from Department;
We have created materialized views in sql for that.and lets check performance.
Select* from MV_Employee;
It will fetch 1 million records in 60 secs.So performance is improved double when you use materialized view.
Materialized view log:
What is Incremental or Fast Refresh?
When in database level some DML changes are done then 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.
In this situation materialized view is not full refresh and it only refresh the data on incremental basic and only incremental data is added in to materialized view.
What is Complete Refresh?
Without a materialized views log, Oracle Database must re-execute the materialized view query to refresh the materialized views. This process is called a complete refresh. Usually, a fast refresh takes less time than a complete refresh.A materialized views log is located in the master database in the same schema as the master table. A master table can have only one materialized view’s log defined on it. Oracle Database can use this materialized view log to perform fast refreshes for all fast-refreshable materialized views based on the master table.To fast refresh a materialized join view, you must create a materialized view log for each of the tables referenced by the materialized views.
Hope you will get actual idea about materialized views in SQL.If you like this article do not forget to comment here.