Introduction
This article is intended to illustrate the concepts of materialized views in database systems and their realization through examples written for SQL Server and Oracle database systems. This kind of view enables enhancing the performance of your application, especially in business intelligence solutions where we deal with very complex queries (a lot of join operations) accessing databases commonly ranging in size from several gigabytes to a few terabytes. We'll walk through some common scenarios in an enterprise where materialized view solutions can be applied in order to improve the response time on expensive operations such as report queries that join two very large tables.
Find more about SQL Server here: SQL Server
Materialized Views
Views are virtual tables composed of the result set of a SQL query and the contents are usually not stored physically. They allow for hiding the complexity of SQL queries thus creating a level of abstraction. You can think of views as a stored query in the server. The main advantage of views is that they can subset the data contained in a table exposing only the interesting columns and rows in order to abstract the presentation of the table; they can join and simplify multiple tables into a single virtual table; they can act as aggregated tables; they can restrict the direct access to tables by end users, and finally, they can provide a standard interface of the data.
Find more about Views here: Views in SQL Server
Views cannot solve all the problems explained above. For example, if a view performs an aggregation of millions of rows, the query performance of the view decreases dramatically and this operation is performed many times every day, the database system must access millions of rows repeatedly. A new data structure must be defined to deal with this kind of scenario. A materialized view is defined just as a regular view but the result set of the query is stored as persistent data object such as a table which is frequently updated from the underlying base tables when changes occur. They are useful to aggregate data in business intelligence applications with complex queries.
Materialized views in Oracle Database
Oracle has added the capability of creating materialized views since Oracle 8i stores the underlying result set as a physical object like a table.
Let's suppose you want to create a Decision-Support System (DSS) and use Business Intelligence (BI) technologies for achieving your goals. Typically, BI solutions use pre-calculated formulas for speeding up long-time queries. One strategy to create pre-calculated data is to use materialized views which store the calculated data and refreshed any change from the base tables. I will illustrate this solution through an example.
First of all, we need to analyze the SQL statement syntax for materialized views. You can create a materialized view in Oracle with the SQL statement create materialized view. There are some options associated with this statement such as.
-
Build method. It's the time when the view contents are first computed. There are two options: the first one is immediate to specify that the view is to be populated when created, this is the default build method and the second one is deferred to specify that the view is to be populated by the next refresh operation.
-
Refresh mode. It's the time when the view contents are recomputed and how often it occurs. When the view is refreshed each time the underlying base tables are changed and commit the change, the mode is called on commit. When the user calls one of the three DBMS_MVIEW refresh procedures, the mode is called on demand. The last mode is to refresh the view periodically and the user can tell when to refresh and how often to do it.
-
Refresh method. It tells how to refresh the view. The complete method is to recompute the view from the base tables and the queries. The fast method tries to update the view incrementally without recomputing. The last method is force and Oracle will decide if a fast method is possible, if not, will do a complete refresh.
-
Query rewriting. The enable query rewrite tells the query optimizer that it should try to rewrite the associated queries.

Indrani RoyPosted Apr 2, 2018, 9:34 PM
Hello sir, I want help in creating materialized view dynamically. Suppose I create a web form in vb that accepts a sql query in a text box, there's a ADD button to add this query to the database, a HIT/MISS button to calculate how many times the attributes are used by a particular query and store it in a matrix, from this matrix the frequently used attributes are fetched and forms a materialized view. Please guide me how to do this