Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Automatically rebuild a second table after data changes in first table

Posted on 2012-12-27
Medium Priority
Last Modified: 2012-12-28
Environment:  HPUX 11.11 / Oracle 9207

I work with a vendor supplied app, so we are unable to modify table structures etc.

There is a table that defines a hierarchy of accounts that is in the format


A parent can have multiple children, each child can have one parent.  So it builds a tree.

A view was built that joins this table to itself 6 times so that it can build a hierarchy 6 levels deep and query transactions using these accounts for any level in the hierarchy (a level may represent a division, branch, department etc) so it's a way to summarize costs up the accounting structure.

Because the table is joined to itself so many times in the view it's fairly expensive.  It has to do a full table scan of itself several times every time it's referenced in a query and some of the queries reference the view 4 or 5 or more times.

The hierarchy is not updated very often, so in order to improve performance, I thought I'd create a table based upon the view definition.  It improves query performance significantly.

The problem is keeping it updated when the master table is updated.

I have a couple of thoughts so far, would appreciate it anyone else has any other bright ideas.

I had thought about an update trigger to drop and rebuild the table, but DDL performs an implicit commit and don't want to do that since if something goes wrong later, it can't rollback (option a)

When the master table is updated it throws a request out to the OS and a background process runs that perform some other updates.  I could add a table rebuild to the end of this process, however I'd have to connect to the database as the schema owner in a shell script that called SQLPlus (and I don't want the schema owner password in clear text in a script).  (option b)

One option to tie into the above could be to have this reference table in its own schema, grant select on it to the query user and create a synonym so that it's transparent to the query user. (option c)

My last thought was to have an after update trigger launch a job - but I don't know if that's counted as a separate transaction or would be included in the transaction that launches to job (and since the triggering update occurs in an OLTP system, can't have it waiting forever for a return). (option d)

If anyone has any other bright ideas (I'm leaning towards option c at the moment) or just wants to chime in on my thoughts so far, it would be appreciated.
Question by:Steve Wales
LVL 74

Accepted Solution

sdstuber earned 2000 total points
ID: 38724801
>>> I thought I'd create a table based upon the view definition.

This is called a materialized view.

You can build them with fast refresh on commit or with a periodic job.


LVL 32

Expert Comment

ID: 38724825
Can you post a describe of your master table?
LVL 23

Author Comment

by:Steve Wales
ID: 38724827
I had completely forgotten about MV's - haven't had a need to use one in years.

Thanks, I'll dig into that and get back with you.
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

LVL 23

Author Comment

by:Steve Wales
ID: 38725356

My view of the data is a self join - it joins the table to itself a total of 6 times.

According to the documentation link, under the subheading "Materialized Views Containing Only Joins", A materialized view containing only joins can be defined to be refreshed ON COMMIT or ON DEMAND. If it is ON COMMIT, the refresh is performed at commit time of the transaction that does DML on the materialized view's detail table. Oracle does not allow self-joins in materialized join views.


The former gives ORA-12054: cannot set OIN COMMIT refresh attribute for the materialized view

The latter gives ORA-12015: cannot create a fast refresh materialized view from a complex query

So I'm left with a less than "automatic" refresh since it's considered a complex view (doing 6 left outer joins to itself, since an account could have from 0 to 6 "parents" depending on the depth of the tree).

Are there any other options other than doing CREATE MV START sysdate NEXT sysdate+1/24 or something similar so that I have something resembling a more immediate rebuild that doesn't require me to constantly rebuild an entity that won't get changed very often ?

Related question:  If I specify REFRESH FORCE in the START / NEXT syntax, is Oracle clever enough to realized (thanks to the MV LOG) that even though I have a job scheduled to do it, that it doesn't really have to ?   Is there a way to test it (I can manually run the job, but I don't know if it actually performed any updates or not) ?
LVL 74

Expert Comment

ID: 38725787
the dbms_job created can simply be dropped if you don't really want it to run.

or, set it to broken and you can run it as needed, or simply call the refresh procedure yourself
LVL 23

Author Comment

by:Steve Wales
ID: 38725871
Well, it's more that the rebuild needs to happen when the users make changes - which may be once a year or once a week during year end ... it's not set in stone.

So I need a way to trigger the rebuild to happen after a change in the table.  If calling the refresh job doesn't do any work if there's no indication of a change in the materialized view log, then that might be sufficient.

I don't want the materialized view to be consistently recreated for no reason - it may cause issues during reporting (year end is coming after all).

Does that make the issue clearer ?
LVL 35

Expert Comment

by:Mark Geerlings
ID: 38725951
How about a variation of your option a that just does an update (or insert) rather than a full recreate?
LVL 74

Expert Comment

ID: 38726077
the materialized view logs will keep track of changes,  if there are no changes, then the refresh won't do anything.

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to recover a database from a user managed backup

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question