Automatically rebuild a second table after data changes in first table

Posted on 2012-12-27
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 73

Accepted Solution

sdstuber earned 500 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 22

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.
LVL 22

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) ?
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 73

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 22

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 73

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to recover a database from a user managed backup

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now