Materialized View
Materialized View
ORACLE
Features/Capabilities
Can be partitioned and indexed
Can be queried directly
Can have DML applied against it
Several refresh options are available
Best in read-intensive environments
07/25/08 Yadav, Naresh 3
Advantages and Disadvantages
Advantages
Useful for summarizing, pre-computing,
replicating and distributing data
Faster access for expensive and complex
joins
Transparent to end-users
MVs can be added/dropped without
invalidating coded SQL
Disadvantages
Performance costs of maintaining the
views
07/25/08 Yadav, Naresh 4
Storage costs of maintaining the views
Database Parameter Settings
init.ora parameter
COMPATIBLE=8.1.0 (or above)
System or session settings
query_rewrite_enabled={true|false}
query_rewrite_integrity=
{enforced|trusted|stale_tolerated}
Can be set for a session using
alter session set
query_rewrite_enabled=true;
alter session set
query_rewrite_integrity=enforced;
Privileges which must be granted to users directly
QUERY_REWRITE - for MV using objects in own
schema
GLOBAL_QUERY_REWRITE - for objects in other
schemas
07/25/08 Yadav, Naresh 5
Query Rewrite Details
query_rewrite_integrity Settings:
enforced – rewrites based on Oracle enforced
constraints
Primary key, foreign keys
Refresh Options
– COMPLETE – totally refreshes the view
Can be done at any time; can be time consuming
save mv_example.sql
Elapsed: 01:32:17.93
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=HINT: FIRST_ROWS (Cost=360829
Card=6 Bytes=120)
1 0 SORT (GROUP BY) (Cost=360829 Card=6 Bytes=120)
2 1 PARTITION RANGE (INLIST
3 2 TABLE ACCESS (FULL) OF ‘ITEMS' (Cost=360077
Card=375154 Bytes=7503080)
Execution Plan
----------------------------------------------------------------------------------------------
0 SELECT STATEMENT Optimizer=HINT: FIRST_ROWS (Cost=3749 Card=12 Bytes=276)
1 0 SORT (GROUP BY) (Cost=3749 Card=12 Bytes=276)
2 1 PARTITION RANGE (INLIST)
3 2 TABLE ACCESS (FULL) OF ‘ITEMS_SUMMARY_MV'
(Cost=3723 Card=7331 Bytes=168613)
To validate a dimension:
exec dbms_olap.validate_dimension(<dim_name>,<owner>,FALSE,FALSE);
Execution Plan
----------------------------------------------------------
SELECT STATEMENT Optimizer=CHOOSE (Cost=16174 Card=36258 Bytes=1160256)
SORT (GROUP BY) (Cost=16174 Card=36258 Bytes=1160256)
HASH JOIN (Cost=81 Card=5611339 Bytes=179562848)
TABLE ACCESS (FULL) OF ’CALENDAR' (Cost=2 Card=8017 Bytes=128272)
TABLE ACCESS (FULL) OF ’ITEMS' (Cost=76 Card=69993 Bytes=1119888)
Execution Plan
----------------------------------------------------------
SELECT STATEMENT Optimizer=CHOOSE (Cost=3703 Card=878824 Bytes=44820024)
SORT (GROUP BY) (Cost=3703 Card=878824 Bytes=44820024)
HASH JOIN (Cost=31 Card=878824 Bytes=44820024)
VIEW (Cost=25 Card=8017 Bytes=128272)
SORT (UNIQUE) (Cost=25 Card=8017 Bytes=128272)
TABLE ACCESS (FULL) OF ‘CALENDAR’ (Cost=2 Card=8017 Bytes=128272)
TABLE ACCESS (FULL) OF ‘ITEMS_MV’ (Cost=3 Card=10962 Bytes=383670)
Materialized Views
reduce system cpu/io resource requirements
by pre-calculating and storing results of
intensive queries
allow for the automatic rewriting of intensive
queries
are transparent to the application
have storage/maintenance requirements
can understand complex data relationships
can be refreshed on demand or on a
schedule
Dimensions
allow you to “tell” Oracle about complex data
relationships which can be used to rewrite
07/25/08
queries Yadav, Naresh 26
Requirements for FAST REFRESH
Requirement Joins Only Joins & Single Table
Aggregates Aggregates