In your book you mention that you use materialize views to pre answer large queries, but you also did mention that they can also be used for effective replication.
1. Can you please explain how materialized views can be used for replication?
2. Please give example on those scenarios where 'refresh of commit' will not work?
3. Is the WITH clause in sql similar to a materialized view in concept and in how it works behind the scenes?
4. What does the hint 'materialize' accomplish? and do hints work in 10GR2 onwards at all?
Thanks
Otn
1) it is not obvious (if you can use them to copy data - eg: replicate data - within a single database.... and they work with dblinks... then, you can use them to copy (replicate) data..)
you create a materialized view
selecting data from a remote database
and it synchronizes it upon each refresh
hence, you have replicated data - this goes back to version 7.0 of Oracle when they were originally called "snapshots"
http://docs.oracle.com/cd/B19306_01/server.102/b14226/repoverview.htm#sthref49 2) refresh on commit will NEVER work over a database link, that would be synchronous replication and synchronous replication just isn't anything anyone wants to even consider.
Refresh on commit is appropriate only in a single database.
3) no, a materialized view EXISTS. If you create on and 1,000 people execute a query that uses it - the materialized view is used 1,000 times. It is not created 1,000 times - it already exists.
A WITH subquery would be materialized for EACH QUERY execution. It would be materialized 1,000 times in the above scenario.
4) This undocumented hint causes Oracle to put the query block that was hinted as "materialize" into a temporary table for the duration of the query - and then that data is removed.
ops$tkyte%ORA10GR2> create table t as select * from all_objects;
Table created.
ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> @at
ops$tkyte%ORA10GR2> column PLAN_TABLE_OUTPUT format a90 truncate
ops$tkyte%ORA10GR2> set autotrace traceonly explain
ops$tkyte%ORA10GR2> with data
2 as
3 (select count(*) cnt from t )
4 select t.*, data.cnt from t, data
5 /
Execution Plan
----------------------------------------------------------
Plan hash value: 658131203
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 52697 | 7256K| 445 (3)| 00:00:03 |
| 1 | NESTED LOOPS | | 52697 | 7256K| 445 (3)| 00:00:03 |
| 2 | VIEW | | 1 | 13 | 220 (2)| 00:00:02 |
| 3 | SORT AGGREGATE | | 1 | | | |
| 4 | TABLE ACCESS FULL| T | 52697 | | 220 (2)| 00:00:02 |
| 5 | TABLE ACCESS FULL | T | 52697 | 6587K| 224 (4)| 00:00:02 |
-----------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
ops$tkyte%ORA10GR2> with data
2 as
3 (select /*+ MATERIALIZE */ count(*) cnt from t )
4 select t.*, data.cnt from t, data
5 /
Execution Plan
----------------------------------------------------------
Plan hash value: 3271945946
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%C
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 52697 | 7256K| 447
| 1 | TEMP TABLE TRANSFORMATION | | | |
| 2 | LOAD AS SELECT | | | |
| 3 | SORT AGGREGATE | | 1 | |
| 4 | TABLE ACCESS FULL | T | 52697 | | 220
| 5 | MERGE JOIN CARTESIAN | | 52697 | 7256K| 226
| 6 | VIEW | | 1 | 13 | 2
| 7 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6603_323E82A | 1 | 13 | 2
| 8 | BUFFER SORT | | 52697 | 6587K| 226
| 9 | TABLE ACCESS FULL | T | 52697 | 6587K| 224
------------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> set autotrace off
see steps 1..4 - in the second plan - the loading of a temporary table with the data, instead of merging it all together into one big plan as the first one chose to.
Hints are a supported feature of the database, even in the current release 11g.