Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

Materialized View ON PREBUILT TABLE Tips

Expert Oracle Database Tips by Donald BurlesonNovember 10, 2015

Question:  What is the option to create a "materialized view on prebuilt table"?  When do I use the "on prebuilt table" syntax?

Answer:  The "materialized view on prebuilt table" is used when you have already created a replicated table using "create table as select" (CTAS) and now want that table to accept query rewrite. 

In this case, you can convert an existing table to a materialized view by using the ON PREBUILT TABLE clause.

A complete refresh occurs when the Oracle 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 Oracle materialized view.

The following is an example of an Oracle materialized view on prebuilt table with an ON COMMIT refresh: 

CREATE MATERIALIZED VIEW
    empdep
ON PREBUILT TABLE
REFRESH FAST ON COMMIT
ENABLE QUERY REWRITE
   AS SELECT empno, ename, dname, loc,
          e.rowid emp_rowid,
          d.rowid dep_rowid
   FROM emp e, dept d
   WHERE e.deptno = d.deptno;

Materialize View vs. Table

At the segment level, the materialized view and the table are the same.  The biggest difference between a table and a materialized view is the inability to add or modify columns in a materialized view while it is quite simple to do with a table.

The standard approach to building a materialized view creates both a table and a materialized view; however, the data_object_id for the materialized view will be NULL.

In this situation, dropping the materialized view automatically drops the table because it did not have an independent existence.

This is where materialized view ON PREBUILT TABLE comes in very handy.

Taking a new approach via materialized view ON PREBUILT TABLE, first create the table in the same name as the materialized view to be created:

 SQL> create table mv1 (cnt number(10));

Next create the materialized view on prebuilt table by simply adding the ON PREBUILT TABLE clause as shown below:

create materialized view mv1
on prebuilt table
never refresh
as
select cast(count (1) as number(10)) cnt
from t1;

As a result of the materialized view on prebuilt table clause, there are now two objects just as before - one table and one materialized view.  The materialized view took over the command over the segment; however, the table already existed, so the table object was not recreated during the process.

One clever thing about the materialized view on prebuilt table process that it makes it impossible to drop the prebuilt table without first dropping the materialized view.  Even if the materialized view is dropped, the table will revert to its former status as an independent segment.

Of course, the table could then be deleted, if desired, but better yet, it is now possible to do anything allowed with a table, such as select from it, create indexes and modify or add columns.

Once the table has been modified to suit you, just use ON PREBUILT TABLE to recreate the materialized view and you're back in business in no time with your materialized view on prebuilt table.  The end user sees no difference except that the materialized view ON PREBUILT TABLE eliminates the burden of the "standard" drop and recreate of a materialized view and associated table. 

A materialized view ON PREBUILT TABLE can be used on tables of all sizes; however, you're likely to see the most benefit from its use on larger tables where the time impact of a regular drop and rebuild of the materialized view could be on the order of magnitude of hours or days.

Sometimes with more complex code, you will run into an ORA-12058: materialized view cannot use prebuilt table error.  Follow the link for more information on the ORA-12058 error and the proper use of materialized view ON PREBUILT TABLE.

Follow the link for more detailed information on materialized views, the ON PREBUILT TABLE command and how to verify that this materialized view on prebuilt table process works as described.

 
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.

 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster