Query rewrites are where the real benefit of materialized views kick in.
When you have a materialized view that has query rewrites enabled, the Oracle cost-based query optimizer will automatically rewrite a submitted query to take advantage of a materialized view. The substitution of a materialized view for a requested table or view is totally transparent to the user, although a user can directly query a materialized view.
The Oracle cost-based optimizer determines whether to use a materialized view in a query according to a set of conditions:
- Query rewrite must be enabled for the database session.
- The materialized view must have the query rewrite enabled.,
- The SQL query submitted can use the materialized view.
The cost-based optimizer determines if a materialized query can be used according to a series of tests:
There is also a set of integrity constraints that will allow or disallow the use of a materialized view depending on how recently the data in the view has been refreshed. The following SlideShow illustrates some of the conditions where a materialized view could be selected by the cost-based optimizer:
Materialized View Match
The fourth section of the create materialized view command is the query that the materialized view will use:
[ for update ] [{disable | enable} query rewrite]
as subquery.
If you specify for update, the materialized view will be updatable; otherwise, it will be readonly.
Most materialized views are read-only replicas of the master data. If you use updatable materialized views, you need to be concerned with issues such as two-way replication of changes and the reconciliation of conflicting data changes. Updatable materialized views are an example
of multimaster replication.
Note: The query that forms the basis of the materialized view should not use the User or SysDate pseudo-columns.
The following example creates a read-only materialized view called LOCAL_BOOKSHELF in a local database, based on a remote table named BOOKSHELF that is accessible via the REMOTE_CONNECT database link. The materialized view is placed in the USERS tablespace.
create materialized view LOCAL_BOOKSHELF
tablespace USERS
refresh force
start with SysDate next SysDate+7
with primary key
as
select * from BOOKSHELF@REMOTE_CONNECT;
Oracle responds with
Materialized view created.
The command shown in the preceding example will create a read-only materialized view
called LOCAL_BOOKSHELF. Its underlying table will be created in a tablespace named USERS. You can place materialized view logs in tablespaces apart from the materialized views they support. The force refresh option is specified because no materialized view log exists on the base
table for the materialized view; Oracle will try to use a fast refresh but will use a complete refresh until the materialized view log is created. The materialized view’s query specifies that the entire BOOKSHELF table, with no modifications, is to be copied to the local database. As soon as the LOCAL_BOOKSHELF materialized view is created, its underlying table will be populated with the
BOOKSHELF data. Thereafter, the materialized view will be refreshed every seven days.
The storage parameters that are not specified will use the default values for those parameters for the USERS tablespace.
You can use the EXPLAIN PLAN command to see if a particular query will be rewritten to use a materialized view.
Explain Plan Command
In the next lesson, you will learn how to manage materialized views.