Monday 28 November 2016

Difference between View vs Materialized View in database


What is View in database
What is difference between View vs Materialized View in database or SQL?Views are logical virtual table created by “select query” but the result is not stored anywhere in the disk and every time we need to fire the query when we need data, so always we get updated or latest data from original tables. Performance of the view depend upon our select query. If we want to improve the performance of view we should avoid to use join statement in our query or if we need multiple joins between table always try to use index based column for joining as we know index based columns are faster than non index based column. View allow to store definition of the query in the database itself.




What is Materialized View in database
Materialized views are also logical view of our data driven by select query but the result of the query will get stored in the table or disk, also definition of the query will also store in the database .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 for joining also joining is done at the time of materialized views refresh time so no need to every time fire join statement as in case of view.


Difference between View vs Materialized View in database
Based upon on our understanding of View and Materialized View, Let’s see, some short difference between them :

1) First difference between View and materialized view is that, In Views query result is not stored in the disk or database but Materialized view allow to store query result in disk or table.

2) Another difference between View vs materialized view is that, when we create view using any table,  rowid of view is same as original table but in case of Materialized view rowid is different.

3) One more difference between View and materialized view in database is that, In case of View we always get latest data but in case of Materialized view we need to refresh the view for getting latest data.

4) Performance of View is less than Materialized view.

5) This is continuation of first difference between View and Materialized View, In case of view its only the logical view of table no separate copy of table but in case of Materialized view we get physically separate copy of table

6) Last difference between View vs Materialized View is that, In case of Materialized view we need extra trigger or some automatic method so that we can keep MV refreshed, this is not required for views in database.

When to Use View vs Materialized View in SQL
Mostly in application we use views because they are more feasible,  only logical representation of table data no extra space needed. We easily get replica of data and we can perform our operation on that data without affecting actual table data but when we see performance which is crucial for large application they use materialized view where Query Response time matters so Materialized views are used mostly with data ware housing or business intelligence application.

That’s all on difference between View and materialized View in database or SQL. I suggest always prepare this question in good detail and if you can get some hands on practice like creating Views, getting data from Views then try that as well.






View is nothing but a set a sql statements together which join single or multiple tables and  shows the data .. however views do not have the data themselves  but point to the data .
Whereas  Materialized view is a concept mainly used in  Datawarehousing .. these views contain the data itself .Reason being it is easier/faster to access the data.The main purpose of Materialized view is to do calculations and display data from multiple tables using joins .


view: View is a logical or virtual memory which is based on select query
and the simple view is the view in which we can not make DML command if the
view is created by multiple tables.
Materialize veiw: It works faster than simple, Its works as snap shot and used for security purposes and we can make DML command in materialize view



Difference between Views & Materialized views

Materialized views are disk based and update periodically base upon the query definition.

Views are virtual only and run the query definition each time they are accessed.

Views evaluate the data in the tables underlying the view definition at the time the view is queried. It is a logical view of your tables, with no data stored anywhere else. The upside of a view is that it will always return the latest data to you. The downside of a view is that its performance depends on how good a select statement the view is based on. If the select statement used by the view joins many tables, or uses joins based on non-indexed columns, the view could perform poorly.

Materialized views are similar to regular views, in that they are a logical view of your data (based on a select statement), however, the underlying query resultset has been saved to a table. The upside of this is that when you query a materialized view, you are querying a table, which may also be indexed. In addition, because all the joins have been resolved at materialized view refresh time, you pay the price of the join once (or as often as you refresh your materialized view), rather than each time you select from the materialized view. In addition, with query rewrite enabled, Oracle can optimize a query that selects from the source of your materialized view in such a way that it instead reads from your materialized view. In situations where you create materialized views as forms of aggregate tables, or as copies of frequently executed queries, this can greatly speed up the response time of your end user application. The downside though is that the data you get back from the materialized view is only as up to date as the last time the materialized view has been refreshed.

Materialized views can be set to refresh manually, on a set schedule, or based on the database detecting a change in data from one of the underlying tables. Materialized views can be incrementally updated by combining them with materialized view logs, which act as change data capture sources on the underlying tables.

Materialized views are most often used in data warehousing / business intelligence applications where querying large fact tables with thousands of millions of rows would result in query response times that resulted in an unusable application. [Source]



Major Differences between Views and Materialized Views!.
Views are the virtual projection of an output query or the dynamic view of the data in a database that is presented to the user whenever requested. Materialized views on the other hand are a non-virtual schema which is a common part of database warehousing, primarily used for summarizing, pre-computing, replicating and distributing data etc. Now having said that, let’s move on to some basic differences between Views and Materialized views in order to gain a better understanding.

Access to the data
The operations performed using Views directly affects the data in the base table. Therefore they are subjected to the integrity constraints and triggers of the base table.

Materialized views provide an indirect access to the data of the base table. This is due to the fact that they create a separate schema to store the results of a query.
Storage space
One of the common differences between views and materialized views is the way they take up storage space. Since Views are the virtual projection of a query result hence they do not take up any storage area. This helps in reduction of memory usage and encourages the use of Shared SQL.

Whereas the schema created by Materialized Views take up some storage space as it is saved in either the same database as its base table or in a different database.




Usage of Views and Materialized Views
Differences between views and materialized views can also be understood in terms of their usages.

Views have a more restrictive behavior towards a base table.

It could be used to isolate an application in order to prohibit any change in base table definition.
It can be used in order to simplify SQL statements for the user.
Views could be used to enhance security by restricting access to a predetermined set of columns and rows

Existence of Materialized Views is transparent to the SQL except when used for query rewrites.

Query rewrites are said to improve the performance of SQL execution and are useful in a data warehouse environment.
Users can insert, delete and update the data by means of updatable materialized views.

Apart from these differences between views and materialized views, some other important points to keep in mind which further clears the concept of views and materialized views are:

Views can be based on each other in addition to operating on base tables. A view can JOIN another view with a table using – GROUP BY or UNION clause.
Materialized views can be defined on a base table, partitioned table or Views whereas indexes are defined on Materialized views.
Ultimately a materialized view log is a schema object which records changes to a master table's data so that the materialized view defined on that master table can be refreshed incrementally.


Other than the word "view" in their names and the fact that both are defined by an underlying SQL, there is little else common between Oracle views and materialized views – yet they can be a source of great confusion to beginners.

Here’s a summary of the key differences between views and materialized views, to put an end to all mix-ups. By the end of the article, you should be in a position to decide if your specific scenario needs a view, a materialized view, or neither.


1. Moment Of Execution
A view’s SQL is executed at run-time. The results are fetched from the view’s base tables when the view is queried.

A materialized view (called snapshot in older Oracle versions) is a "pre-answered" query – the query is executed when the materialized view is refreshed. Its result is stored in the database and the query only browses the result.

2. Space
A view occupies no space (other than that for its definition in the data dictionary).

A materialized view occupies space. It exists in the same way as a table: it sits on a disk and could be indexed or partitioned.

3. Freshness of Output
A view’s output is built on the fly; it shows real-time data from the base tables being queried.

A materialized view does not reflect real-time data. The data is only as up to date as the last time the materialized view was refreshed.

4. Where To Use
A view is best used when:

You want to hide the implementation details of a complex query
You want to restrict access to a set of rows/columns in the base tables
A materialized view is best used when:

You have a really big table and people do frequent aggregates on it, and you want fast response
You don’t mind the result being a little out of date, or your application data has more queries than updates (as in a BI/data warehousing system)
Caution!

Are you creating a materialized view to avoid the pain of tuning a query? Don’t do it! A materialized view brings with it the overhead of maintaining extra DB objects that need regular refresh besides giving you out-of-date data, all for something that might have been fixed by writing better code.

Whether your query runs directly on tables, on views or via materialized views, it must be the most efficient query possible.



A view is nothing but a SQL query, takes the output of a query and makes it appear like a virtual table, which does not take up any storage space or contain any data

But Materialised views are schema objects, it storing the results of a query in a separate schema object(i.e., take up storage space and contain datas). This indicates the materialized view is returning a physically separate copy of the table data