Oracle triggers - problem with mutating tables

前端 未结 8 1840
情书的邮戳
情书的邮戳 2020-12-06 19:31

My tables:

TableA (id number, state number)
TableB (id number, tableAId number, state number)
TableC (id number, tableBId number, state number)
8条回答
  •  自闭症患者
    2020-12-06 19:47

    I saw some opinions, that mutating table error indicates flaws in logic of the
    application - is this true and how can I change my logic in order to prevent this
    error?

    I don't know where you saw that, but I know have posted that opinion many times berfore.

    Why do I think mutating tables are usually indicative of a flaw in the data model? Because the kind of "requirement" which drives code that hurls ORA-4091 is frequently associated with poor design, especially insufficient normalisation.

    You scenario is a classic example of this. You get the ORA-04091 because you are selecting from TableC when your insert or update it. But why are you selecting from TableC? Because you "need" to update a column on its parent, TableB. But that column is redundant information. In a fully-normalised data model that column would not exist.

    Denormalisation is often touted as a mechanism for improving the performance of queries. Unfortunately the proponents of denormalisation gloss over its cost, which is paid in the currency of excessive complexity when we insert, update and delete.

    So, how can you change your logic? The simple answer is to drop the columns and don't bother storing the smallest state by parent ID. Instead, execute a MIN() query whenever you need that information. If you need it frequently and it would be expensive to execute the query then you build materialized views which store the data (be sure to use ENABLE QUERY REWRITE)

提交回复
热议问题