Trigger on a materialized view

i have a materialized view on a prebuilt table..
is it possible to write after triggers on the table?
regards
raj

check a last updated column in the materialised view after a refresh..
and collect those rows and process those as :new rows....
but i need to keep on checking if the materialized view is refreshed or not.....You would need to use a job to repeatedly check the MVIEW for any rows which have been updated since the last time the job ran. You would need some form of log to record the time your job used to make that check. It is a bit clunky, but if you read Tom Kyte's article you'll understand why triggers don't work.
An alternative approach would be to have the source tables which feed the MVIEW also trigger whatever additional processing it is that you require. But this would work only in a handful of situations.
Is this related to other question about Java Stored Procedures?
Cheers, APC
blog: http://radiofreetooting.blogspot.com

Similar Messages

  • Triggers on Materialized View Fast Refresh

    Hello all!
    I have a Materialized View defined as below:
    CREATE MATERIALIZED VIEW mxsvativi
    BUILD IMMEDIATE
    REFRESH FAST ON COMMIT
    WITH PRIMARY KEY
    AS
    SELECT codativ, TRIM (ramo) AS ramo, percdesc FROM tbativi;And, on this MV, I have a trigger declared as:
    CREATE OR REPLACE TRIGGER TK_SYNC_FV_MXSATIVI
    BEFORE DELETE OR INSERT OR UPDATE
    OF CODATIV
      ,RAMO
      ,PERCDESC
    ON MXSVATIVI
    REFERENCING NEW AS NEW OLD AS OLD
    FOR EACH ROW
    DECLARE
       voper     NUMBER;                                --Operation
    BEGIN
       IF INSERTING
       THEN
          voper := 0;
       ELSIF UPDATING
       THEN
          voper := 1;
       ELSIF DELETING
       THEN
          voper := 2;
       END IF;
       --Save voper to a tblOperLog
    END;This trigger should save some data on a table called tblOperLog, based on the operation that is being performed on the MV (Insert, Delete or Update).
    I´ve tested this on Oracle 10G and also on Oracle 11G (11.1). On these, when we perform a Update on the parent table (on this eg, TBATIVI) the trigger TK_SYNC_FV_MXSATIVI records an Update Operation on the table tblOperLog. But, on a Oracle 11G 11.2.0.2.0 is logging a Delete and a Insert for each update that is done.
    Does anybody know how could I force the proccess that updates the MV to emits an update instead of a delete/insert?
    Thanks a lot!

    Are you doing an incremental refresh of the materialized view? Or are you doing a full refresh?
    Is the materialized view in 11.2 fast refreshable? Perhaps you're missing a materialized view log or missing some data from the materialized view log.
    In general, I'd be wary of trying to use a trigger on a materialized view to log changes to a table since there are no guarantees that the materialized view is always going to be incrementally refreshed nor that the refresh is always going to happen in exactly the same way.
    Justin

  • Overhead of Triggers VS Materialized View Logs

    If I need to log INSERTs, UPDTEs, and DELETEs on a table - is there a difference in the overhead of doing so via triggers vs MV logs?
    Thanks

    You said that MV log is marginally more efficient than a trigger. I am curious why. In what way do they work differently under the hood?If the essence of discussion is only why MV log is more efficient than trigger than I think Justin has pointed out below good technical points i.e.
    1.You don't have to pass control over to the PL/SQL engine.
    2.You're also writing the binary data that you'd put in the redo logs rather than column level data.
    And below text from docs :
    Internal Trigger for the Materialized View Log
    When changes are made to the master table or master materialized view using DML, an internal trigger records information about the affected rows in the materialized view log. This information includes the values of the primary key, rowid, or object id, or both, as well as the values of the other columns logged in the materialized view log. This is an internal AFTER ROW trigger that is automatically activated when you create a materialized view log for the target master table or master materialized view. It inserts a row into the materialized view log whenever an INSERT, UPDATE, or DELETE statement modifies the table's data. This trigger is always the last trigger to fire.http://docs.oracle.com/cd/B28359_01/server.111/b28326/repmview.htm#i30732
    3.Trigger i.e. we are coding to track the changes, MV log i.e. tool/technology which is provided by Oracle itself; so definately "We are not that much capable to write the code which is equal or more efficient than Oracle itself right ?"
    In fact, functionality of MV is Instead of Trigger, but under the cover, with near to Oracle's internals.
    Regards
    Girish Sharma

  • Materialized VIews Query

    Hi,
    I want to Insert a record in my table every time materilized view brings in a new record . Is this possible , if yes anyone please suggest solution.
    Thanks
    Aali

    Hi,
    check this link for TRIGGER on MV,
    Materialized View Trigger does not fire
    Also did you try creating a trigger on the TABLE which will INSERT into the TABLE where you want it to be inserted.
    Thanks

  • Trigger Email with refresh of materialized view

    What I am trying to do is when a materialized view is refreshed Oracle will essentially query the MV and for all the branches on the report send an email to the designated person in that branch. Is this possible and what I need help on is how to set the trigger and then the email part. The branch will have to be looked up on a table to get the email address. Thanks for any help on this!

    This is more of a pl/sql/sql question than apex.. Soooo.. I would suggest you ask it here:PL/SQL
    Thank you,
    Tony Miller
    Webster, TX
    There are two kinds of pedestrians -- the quick and the dead.
    If this question is answered, please mark the thread as closed and assign points where earned..

  • Trigger on materialized view

    i have a materialized view that is getting refreshed fast upon commit. i created an after insert trigger that for each new row on the view, inserts
    a row in a certain table. is this safe to do?

    No, it's not safe to do so.
    It was explained that Oracle does not guarantee how the changes are applied to the materialized view and in which order. Moreover the mechanism of the refresh could potentially change between releases. Also there were numerous bugs reported for the triggers created on MV container table.
    If you have access to Oracle Metalink look for the article [ID 67424.1].
    Good luck.
    Edited by: Max Seleznev on Dec 10, 2012 2:32 PM

  • How to refresh materialized view by trigger

    hi,
    im having a problem in refreshing materialized view through a trigger.
    CREATE OR REPLACE TRIGGER L_TRG_MARC_TEST
    BEFORE DELETE OR INSERT OR UPDATE ON MARC_BOOK_B_ENG
    BEGIN
    DBMS_MVIEW.REFRESH('TITLE_ENG_MV','C','',TRUE,FALSE,0,0,0,TRUE);
    END;
    Trigger created.
    SQL> DELETE FROM MARC_BOOK_B_ENG;
    DELETE FROM MARC_BOOK_B_ENG
    ERROR at line 1:
    ORA-04092: cannot COMMIT in a trigger
    ORA-06512: at "SYS.DBMS_SNAPSHOT", line 849
    ORA-06512: at "SYS.DBMS_SNAPSHOT", line 832
    ORA-06512: at "LIBQAL1.L_TRG_MARC_TEST", line 2
    ORA-04088: error during execution of trigger 'LIBQAL1.L_TRG_MARC_TEST'
    plz tell me how to refresh materialized view thru trigger,,and what is wrong in above mentioned situation.
    Regards

    DBMS_MVIEW.REFRESH implicitly commits. COMMIT is not permitted in triggers, as you can see, unless you use AUTONOMOUS_TRANSACTION.
    Try
    CREATE OR REPLACE TRIGGER L_TRG_MARC_TEST
    BEFORE DELETE OR INSERT OR UPDATE ON MARC_BOOK_B_ENG
    DECLARE
    PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
    DBMS_MVIEW.REFRESH('TITLE_ENG_MV','C','',TRUE,FALSE,0,0,0,TRUE);
    END;

  • ! Materialized View log VS Trigger to trace table changes !

    Hi All
    I have to trace changes on couple tables and only those changes then send to other system.
    What do you think is better solution to create a Status table and update it via trigger.
    I saw that MV log can be used for such purpose, but I am not able to create a MV logging on more than one table.
    Or is there any other more effective way to trace changes on table?
    Thanks a lot
    Martin

    It sounds like you should be looking at something like Change Data Capture (CDC). You could also create materialized view logs on each table where you wanted to track changes, create appropriate materialized views on the remote database, and consume the data there. It really depends on what exactly you want to do with the changes you're tracking.
    Justin

  • How to add trigger on materialized view?

    Hi, I'm trying to add trigger on materialized view. I don't know how to do it. I'm new to oracle and PL/SQL. Can you guys put some working example please? Thank you for every advise.

    Please check the below link. This answers you query:
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:672989600346945045

  • Insted of trigger on Materialized view?

    Hi,
    Can we write insted of trigger on materialized view?
    Piyush

    peeyushgehlot wrote:
    When we do full refresh on materialized view, view gets truncated and new records get inserted, that's is on books and we can do job according, but when incremental refresh is done, what trigger will perform i am not sure.I'm not sure that I follow. A full refresh will cause insert triggers (row and table level, before and after) to fire for every row in the materialized view. An incremental refresh will cause insert, update, and delete triggers (row and table level, before and after) to fire for every row that is modified. There may, of course, be updates that don't actually change the data depending on how the materialized view is defined and how the changes need to be processed.
    Justin

  • Refresh/Update data in a materialized view

    Hi,
    I have question about the data in a materialized view and how it is refreshed. My mat view has all my dimension-ids and my (for my specialize needs) aggregated measures from my fact table. I used the mat view wizard to create my view - which works perfectly. But now I wonder if I have to create some sort of mapping(?) or some sort of trigger to refresh the data in the mat view. Or is the data automatically refreshed when I start my fact table mappings. I use OWB 11gR2
    thx

    MVs have properties for refresh - you an refresh based on schedules or when dependent data is committed or manually.
    Cheers
    David

  • How to get Materialized View to ignore unused columns in source table

    When updating a column in a source table, records are generated in the corresponding materialized view log table. This happens even if the column being updated is not used in any MV that references the source table. That could be OK, so long as those updates are ignored. However they are not ignored, so when the MV is fast refreshed, I find it can take over a minute, even though no changes are required or made. Is there some way of configuring the materialized view log such that the materialized view refresh ignores these updates ?
    So for examle if I have table TEST:
    CREATE table test (
    d_id NUMBER(10) PRIMARY KEY,
    d_name VARCHAR2(100),
    d_desc VARCHAR2(256)
    This has an MV log MLOG$_TEST:
    CREATE MATERIALIZED VIEW LOG ON TEST with rowid, sequence, primary key;
    CREATE MATERIALIZED VIEW test_mv
    refresh fast on demand
    as
    select d_id, d_name
    from test;
    INSERT 200,000 records
    exec dbms_mview.refresh('TEST_MV','f');
    update test set d_desc = upper(d_desc) ;
    exec dbms_mview.refresh('TEST_MV','f'); -- This takes 37 seconds, yet no changes are required.
    Oracle 10g/11g

    I would love to hear a positive answer to this question - I have the exact same issue :-)
    In the "old" days (version 8 I think it was) populating the materialized view logs was done by Oracle auto-creating triggers on the base table. A "trick" could then make that trigger become "FOR UPDATE OF <used_column_list>". Now-a-days it has been internalized so such "triggers" are not visible and modifiable by us mere mortals.
    I have not found a way to explicitly tell Oracle "only populate MV log for updates of these columns." I think the underlying reason is that the MV log potentially could be used for several different materialized views at possibly several different target databases. So to be safe that the MV log can be used for any MV created in the future - Oracle always populates MV log at any update (I think.)
    One way around the problem is to migrate to STREAMS replication rather than materialized views - but it seems to me like swatting a fly with a bowling ball...
    One thing to be aware of: Once the MV log has been "bloated" with a lot of unneccessary logging, you may perhaps see that all your FAST REFRESHes afterwards becomes slow - even after the one that checked all the 200000 unneccessary updates. We have seen that it can happen that Oracle decides on full table scanning the MV log when it does a fast refresh - which usually makes sense. But after a "bloat" has happened, the high water mark of the MV log is now unnaturally high, which can make the full table scan slow by scanning a lot of empty blocks.
    We have a nightly job that checks each MV log if it is empty. If it is empty, it locks the MV log and locks the base table, checks for emptiness again, and truncates the MV log if it is still empty, before finally unlocking the tables. That way if an update during the day has happened to bloat the MV log, all the empty space in the MV log will be reclaimed at night.
    But I hope someone can answer both you and me with a better solution ;-)

  • Creating a Materialized View Log After the Data has been instered

    Hi,
    I am trying to create a method of replication from Oracle to MySQL using the Materialized View Log table.
    This has been done before and works quite well, the only problem is that I am trying to impliment the log after the table has been created and populated and wish to push all the existing data through the log file...
    Does anyone know if it is possible to refresh the Materialized View Log and not a Materialized View.
    The way the replication is intended to work is:
    Oracle> Data inserted into table
    Oracle> writes the vector data to the MVL
    Script> Monitors the MVL and can see the changes being made to the Oracle Table
    Script> Updates MySQL with the data and removes the rows from the MVL
    MySQL is then used with other unix systems
    Currently we export the data from the table using Triggers and a cronjob running every x minute to check for changes in the TriggerTables
    Many thanks for your time on this, I have been checking for almost a whole working day and not found the answer to this problem.

    Thats what I thought, the MVL will only read data that has changed since it was created and wont have the option to load in all the data as though it was made before the table was created.
    From what I have read, the MVL is quicker than a Trigger and I have some free code that prooved to work from a MVL using it as a reference to know what records to update. There is not that much to a MVL, a record ID and type of update, New, Update or Delete.
    I think what I will have to do is work on a the same principle for the MVL but use a Trigger as this way we can do a full reload if required at any point.
    Many thanks for your help.

  • Materialized view with join

    In 10g release 2,I tried to create following materialized view with join:
    test_link is a normal table
    test_geom is a table contains a column in SDO_GEOMETRY
    CREATE MATERIALIZED VIEW LOG ON test_link with rowid
    CREATE MATERIALIZED VIEW LOG ON test_geom with rowid,primary key
    CREATE MATERIALIZED VIEW MV_LINK USING INDEX REFRESH FAST ON DEMAND AS
    SELECT li.rowid link_rowid,geom.rowid geom_rowid,li.link_id,geom.link
    FROM test_link li, test_geom geom
    WHERE li.link_id=geom.link_id
    But I always got an error like:
    ORA-12015: cannot create a fast refresh materialized view from a complex query
    If I change the geometry table to another table, everything works fine.
    Anyone have ideas?

    Unfortunately, creating a fast refreshable materialized view on a join with one of the select columns being a user defined type (sdo_geometry is a user defined type) is not allowed. See 5303489 in the metalink bug database.
    You could do like the workaround in the article suggests and create two materialized views and then create a regular view on top.
    In our scenario, our materialized view also contains unions, so we would really like to have one physical object at the end of the day. One approach that we are currently investigating is to create the materialized view (MV1) without the geometry column, which makes it fast refreshable, and also create a materialized view (MV2) on the table containing the geometry column. MV2 is also fast refreshable. We then create a table (T3) that contains all of the columns from MV1, plus a geometry column. An insert, update, delete trigger on MV1 is created. The trigger is used to push all of the columns from MV1 to T3 and the geometry column from MV2 to T3. I have created the above in one of our test environments and haven't encountered any issues yet.
    Let me know if you come up with a better approach.

  • Can't update master table when creating a materialized view log.

    Hi all,
    I am facing a very strange issue when trying to update a table on which I have created a materialized view log (to enable downstream fast refresh of MV's). The database I am working on is 10.2.0.4. Here is my issue:
    1. I can successfully update (via merge) a dimension table, call it TABLEA, with 100k updates. However when I create a materialized view log on TABLEA the merge statement hangs (I killed the query after leaving it to run for 8 hrs!). TABLEA has 11m records and has a number of indexes (bitmaps and btree) and constraints on it.
    2. I then create a copy of TABLEA, call it TABLEB and re-created all the indexes and constraints that exist on TABLEA. I created a materialzied view log on TABLEB and ran the same update....the merge completed in under 5min!
    The only difference between TABLEA and TABLEB is that the dimension TABLEA is referenced by a number of FACT tables (by FKs on the FACTS) however this surely should not cause a problem. I don't understand why the merge on TABLEA is not completing...even though it works fine on its copy TABLEB? I have tried rebuilding the indexes on TABLEA but this did not work.
    Any help or ideas on this would be most appreciated.
    Kind Regards
    Mitesh
    email: [email protected]

    Thats what I thought, the MVL will only read data that has changed since it was created and wont have the option to load in all the data as though it was made before the table was created.
    From what I have read, the MVL is quicker than a Trigger and I have some free code that prooved to work from a MVL using it as a reference to know what records to update. There is not that much to a MVL, a record ID and type of update, New, Update or Delete.
    I think what I will have to do is work on a the same principle for the MVL but use a Trigger as this way we can do a full reload if required at any point.
    Many thanks for your help.

Maybe you are looking for

  • Eventually denied permission to write in home directory

    Hello, My wife is using a Mid-2007, 2.4GHz Intel Core 2 Duo iMac (iMac7,1) with 6GB RAM and more than 1TB of free available HD space.  She is running Mountain Lion (10.8.3) and has administrator privileges. After she has been using the computer for a

  • Why is Adobe Muse kind of slow?

    i have adobe Muse on a Win 7 Machine running with 4gb of Ram and a solid state drive. Should I up my memory to 8? I realize that would definately help but is that what is causing my issue

  • What happened to my wish list since the last update?

    I'm only seeing a handful of albums in my wish list after installing the latest update.

  • Data Flow Question

    I have a Air app where an employee creates an order. This stores the order information in an order arraycollection, they also add items in the order and those go in the orderitems arraycollection. Now because the employee may not always have a networ

  • Sort full-screen Browser in Browser mode?

    Hi. I'm missing something -- the ability to sort the current container while in full-screen browser mode. In full-screen, there are only two viewing modes, Browser (shows contents of selected containers) and Viewer (shows selected images). In Viewer