AddThis Social Bookmark Button

Listen Print

News -- Oracle 8i: The Materialized View

by Rick Greenwald
06/01/2000

In our book Oracle Essentials, we tried to give a complete but compact view of the entire range of functionality in the Oracle database. Naturally, there were some features which we couldn't cover in as much depth as we'd like. This article will expand on topics we covered briefly in Oracle Essentials.

What is the purpose of a materialized view?

Before beginning any exploration of the how of the materialized view, it is important to understand the why behind this new data structure.

The materialized view is another in a long line of gofast tricks implemented in the Oracle database. Introduced with Oracle8i, a materialized view is designed to improve performance of the database by doing some intensive work in advance of the results of that work being needed.

In the case of a materialized view, the data for the view is assembled when the view is created or refreshed. Later queries that need this data automatically use the materialized view, thus saving the overhead of performing the work already done by the view.

The work avoided by a materialized view is essentially twofold:

  • A materialized view can be used to pre-collect aggregate values.
  • A materialized view can be used to assemble data that would come from many different tables, which would in turn require many different joins to be performed.

You may recognize these two requirements as being characteristic of data warehousing. And, in fact, the materialized view is most frequently used in data warehouse applications. Data warehousing applications also do not typically require access to real-time data, so the need to assemble data into a materialized view does not necessarily impact the usefulness of the view.

What is a materialized view?

In Oracle8, a new type of database structure, the materialized view, was introduced. A materialized view is a polyglot structure, resembling other existing structures in some ways, yet different in others.

A materialized view is like a view in that it represents data that is contained in other database tables and views; yet it is unlike a view in that it contains actual data. A materialized view is like an index in that the data it contains is derived from the data in database tables and views; yet unlike an index in that its data must be explicitly refreshed. Finally, a materialized view is very much like a snapshot in that an administrator can specify when the data is to be refreshed; but it is unlike a snapshot in that a materialized view should either include summary data or data from many different joined tables.

How is a materialized view created?

Since a materialized view is just another data structure, you create a materialized view with a piece of Data Definition Language (DDL) syntax.

The syntax used is made up of several parts and keywords. The most commonly used syntax options are described in the following table:

CREATE MATERIALIZED VIEW are required keywords
name is the qualified name of the materialized view
Physical attributes clause allows you to specify the physical attributes, such the tablespace name, for the materialized view
BUILD clause The BUILD clause allows you to specify when you want to build the actual data in the table. Your options are BUILD IMMEDIATE, which calls for the view to be immediately built, BUILD DEFERRED, which calls for the view to be built when it is first refreshed (see explanation of REFRESH clause below) or ON PREBUILT TABLE, which indicates that you are identifying a table that is already built as a materialized view.
REFRESH clause Since the materialized view is built on underlying data that is periodically changed, you must specify how and when you want to refresh the data in the view. You can specify that you want a FAST refresh, which will only update the values in the materialized view, assuming that some preconditions are met, COMPLETE, which recreates the view completely, or FORCE, which will do a FAST refresh if possible and a COMPLETE refresh if the preconditions for a FAST refresh are not available.

The REFRESH clause also contains either the keywords ON COMMIT, which will cause a refresh to occur whenever the underlying data is changed and the change is committed, or ON DEMAND, which will only perform a refresh when it is scheduled or explicitly called. You can also use keywords in the REFRESH clause to create a schedule for recurring refresh operations.

AS subquery The last clause of the CREATE MATERIALIZED VIEW command contains the sub-query that will be used to retrieve the data that will compose the materialized view.

Related Reading

Oracle Essentials: Oracle9i, Oracle8i and Oracle8

Oracle Essentials: Oracle9i, Oracle8i and Oracle8
By Rick Greenwald, Robert Stackowiak, Jonathan Stern

Table of Contents
Index
Sample Chapter

Read Online--Safari Search this book on Safari:
 

Code Fragments only

There is one more clause associated with the creation of materialized views, but before you can understand this clause, you must gain a deeper understanding of how a materialized view is used by your Oracle8i database.

How does it work?

At this point, it may be difficult to understand the importance of a materialized view. After all, why not just use one of the other structures that have similar characteristics, such as a summary table or a pre-joined view?

The answer lies in the interaction between a materialized view and the Oracle8i cost-based optimizer. The cost-based optimizer can automatically substitute a materialized view for a standard table or group of tables if appropriate. This capability in the optimizer means that you can add materialized views to your database and reap the performance benefits, without having to rewrite any of your applications.

The cost-based optimizer will examine a particular query to see if it can be satisfied by a materialized view. If it can, the cost-based optimizer will simply use the materialized view instead of the original data structures. The cost-based optimizer has some intelligence, so even if the data in a materialized view can only satisfy part of the query, the optimizer will still use the materialized view for as much of the data as possible.

If you want a materialized view to be considered for use by the cost-based optimizer, you must include the keyword phrase ENABLE QUERY REWRITE in your specification of the view, or as part of an ALTER VIEW statement after the materialized view has been created.

As with other potential choices for the cost-based optimizer, you can use a hint to explicitly direct a query to the materialized view.

What are the management options?

As mentioned above, you can specify some management options for materialized views, like the timing and type of refresh, as part of the DDL used to create the view.

There are a few functions in two of the prebuilt PL/SQL packages that can be effectively used to manage materialized views, which are summarized in the following table.

PL/SQL package PL/SQL function Description
DBMS_OLAP RECOMMEND_MV The RECOMMEND_MV package analyzes the statistics of the uniqueness of your tables and columns and recommends which materialized views should be created. These recommendations are based on a set of all possible queries against the tables.
DBMS_OLAP RECOMMEND_MV_W The RECOMMEND_MV_W procedure performs the same sort of analysis as the RECOMMEND_MV procedure, except that this procedure uses the actual workload of your database in its calculations.
DBMS_MVIEW (synonym for DBMS_SNAPSHOT package) REFRESH_ALL_MVIEWS The REFRESH_ALL_MVIEWS procedure refreshes all the materialized views that are registered. This procedure only refreshes materialized views where the data in the base tables has been changed since the materialized view was refreshed.
DBMS_MVIEW REFRESH_DEPENDENT The REFRESH_DEPENDENT view is used to refresh all materialized views which are dependent on a particular table. The same conditions for refreshing materialized views apply to this procedure as do to the REFRESH_ALL_MVIEWS procedure.

There are more conditions and considerations revolving around materialized views than I am able to discuss in this brief article, but this should give you the idea of how and when to consider using materialized views in your Oracle8i database.

All in all, the materialized view is a nice addition to the arsenal of data structures in Oracle8i. Although your own particular implementation may not need materialized views, these new views can potentially deliver greater throughput without having to rewrite the applications that use the underlying data.


Rick Greenwald has been active in the world of computer software for over 15 years, including stints with Data General, Cognos, and Gupta. He is currently an analyst with Oracle Corporation. He has published five books and countless articles on a variety of technical topics and has spoken at conferences and training sessions across six continents.


Return to oracle.oreilly.com