Tuning using Materilized Views
| Article Index |
|---|
| 1. Prerequisites for using materialized views |
| 2. Refreshing materialized views |
| 3. Creating a materialized view |
| 4. Monitoring materialized views |
Materialized view allow us to pre-join complex views and pre-compute summaries for super-fast response time
1. Prerequisites for using materialized views
In order to use materialized views, set special initialization parameters and grant special authority to the users of materialized views.
1 |
optimizer_mode = choose, first_rows, OR all_rows |
There are three acceptable values for query_rewrite_integrity:
- trusted—Assumes that the materialized view is current
- enforced (default)—Always goes to materialized view with fresh data
- stale_tolerated—Uses materialized view with both stale and fresh data
Next, you must grant several system privileges to all users who will be using the materialized views.
1 |
GRANT query rewrite TO scott; |
Invoking SQL query rewrite
Query rewrite is generally automatic, but you can explicitly enable it by using session, alter system, or SQL hints:
ALTER {SESSION|SYSTEM} DISABLE QUERY REWRITE
SELECT /*+REWRITE(mv1)*/...
2. Refreshing materialized views
if you specify REFRESH FAST for a single-table aggregate materialized view, you must have created a materialized view log for the underlying table, or the refresh command will fail. When creating a materialized view, you have the option of specifying whether the refresh occurs manually (ON DEMAND) or automatically (ON COMMIT, DBMS_JOB). To use the fast
warehouse refresh facility, you must specify the ON DEMAND mode. To refresh the materialized view, call one of the procedures in DBMS_MVIEW.
The DBMS_MVIEW package provides three types of refresh operations:
DBMS_MVIEW.REFRESH—Refreshes one or more materialized views
DBMS_MVIEW.REFRESH_ALL_MVIEWS—Refreshes all materialized views
DBMS_MVIEW.REFRESH_DEPENDENT—Refreshes all table-based materialized views
Manual complete refresh
A complete refresh occurs when the materialized view is initially defined, unless it references a prebuilt table, and a complete refresh may be requested at any time during the life of the materialized view. Because the refresh involves reading the detail table to compute the results for the materialized view, this can be a very time-consuming process, especially if huge amounts of data need to be read and processed.
Manual fast (incremental) refresh
If you specify REFRESH FAST (which means that only deltas performed by UPDATE, INSERT, DELETE on the base tables will be refreshed), Oracle performs further verification of the query definition to ensure that fast refresh can always be performed if any of the detail tables change. These additional checks include the following:
- A materialized view log must be present for each detail table.
- The RowIDs of all the detail tables must appear in the SELECT list of the MVIEW query definition.
- If there are outer joins, unique constraints must be placed on the join columns of the inner table.
You can use the DBMS_MVIEW package to manually invoke either a fast refresh or a complete refresh, where F equals Fast Refresh and C equals Complete Refresh:
1
EXECUTE DBMS_MVIEW.REFRESH('emp_dept_sum','F');
1 |
EXECUTE DBMS_MVIEW.REFRESH('emp_dept_sum','F');
|
Automatic fast refresh of materialized views
The automatic fast refresh feature is completely new in Oracle9i, so you can refresh a snapshot with DBMS_JOB in a short interval according to the snapshot log. With Oracle 9i, it's possible to refresh automatically on the next COMMIT performed at the master table. This ON COMMIT refreshing can be used with materialized views on single-table aggregates and materialized views containing joins only. ON COMMIT MVIEW logs must be built as ROWID logs, not as primary-key logs. For performance reasons, it's best to create indexes on the
ROWIDs of the MVIEW. Note that the underlying table for the MVIEW can be prebuilt. Below is an example of a materialized view with an ON COMMIT refresh.
1 |
CREATE MATERIALIZED VIEW |
3. Creating a materialized view
- Set the initialization parameters and bounce the database
- Create the materialized view table. Here, we specify that the materialized view will be refreshed every two hours with the refresh fast option.
Instead of using DBMS_MVIEW, you can automatically refresh the MVIEW (Snapshot) using Oracle DBMS_JOB Management. - Create the optimizer statistics and refresh the materialized view.
- Test the materialized view.
- Create the MVIEW log(s) MATERIALIZED VIEW.
- Execute a manual complete refresh.
4. Monitoring materialized views
Oracle provides information in the data dictionary to monitor the behavior of materialized views. When you’re monitoring materialized views, it’s critical that you check the refresh interval in the dba_jobs view. Here is a SQL statement to check the generated job status for materialized views:
1 |
SELECT |
Last Updated (Monday, 23 November 2009 12:23)



