Oracle 10g: correttamente utilizzando ora_rowscn per rilevare tabella riga cambia (cioè, inserimenti, aggiornamenti eliminazioni)

dba.stackexchange https://dba.stackexchange.com/questions/5061

Domanda

Alcune ricerche in controllo quando i record di una tabella sono stati aggiornati, modificati o cancellati mi ha portato alla colonna pseudo noto come ora_rowscn.

In primo luogo, faccio questo:

select max(ora_rowscn) from tablename;

I prendere nota del numero. Poi faccio un inserimento, aggiornamento e cancellazione, verifica che il valore massimo prima e dopo ogni. Sembra di incrementare per ogni tipo di modifica.

Se vi state chiedendo il motivo per cui sto facendo questo, si memorizza nella cache un elenco di entità nel nostro servizio di C # Windows. Questo servizio viene eseguito su due server con bilanciamento del carico, quindi non c'è un'istanza separata di ogni esecuzione. Quando un aggiornamento si verifica sul server A, le esigenze di server B per sapere a questo proposito. Quello che voglio fare è di cache max (ora_rowscn) in una variabile. Ogni volta che la nostra applicazione va ad inserire, aggiornare o eliminare un record, si otterrà un nuovo massimo dal database. Se il valore è diverso allora ovviamente sa che deve andare a prendere un nuovo elenco dal database.

Quindi la mia domanda reale è questa:? Ci sono altri ostacoli che devono essere consapevoli di che potrebbe risultare in un inserimento, aggiornamento o la cancellazione di un record non incrementare questo valore

Modifica:? Qualcuno può aggiungere ora_rowscn come un tag

È stato utile?

Soluzione

Ci sono altri ostacoli che dovrei essere consapevoli di questo risultato potrebbe in un inserimento, aggiornamento o la cancellazione di un record non incrementare questo valore?

ora_rowscn è sempre incrementato quando una riga variazioni - ma in una configurazione di default può anche essere incrementato quando una riga non cambia

Se avete bisogno di controllare l'intera tabella per udates, un metodo è quello di uso auditing . D'altra parte, se avete solo bisogno di controllare la riga che si sta tentando di aggiornamento per i conflitti, ora_rowscn con rowdependencies è l'ideale.

Altri suggerimenti

Getting the max(ora_rowscn) will require a full table scan each time you do it. It may be faster just to refresh the entire cache each time.

It sounds like you need a way to notify the other service that a change took place and what the change was. You could maintain a log table with a column that indicates which system needs to consume the change. The column could have two function based indexes one for each service so that each index contains only the entries that need to be consumed. Then as they are consumed they can make the value NULL to remove it from the index.

Or you could just use Oracle's Advanced Queuing.

As @Leigh Riffel noted, a select max(ora_rowscn) will result in a full table scan. An alternative is to have a timestamp column (we will call sys_ts for this example) which is populated to the time of statement execution (which is not the same as statement commitment, which is when the scn is generated/populated). An index on the sys_ts column will allow you to look at the most recent x (say 25) rows to find the max ora_rowscn only from those rows.

The most straight forward way to do this would be to use the rownum to limit results as described in this[1] ask tom article:

select *
from (select sys_ts, ora_rowscn
from table order by SYS_TS desc) where rownum < 25;

Unfortunately, that also results in a full table scan (at least in oracle 11.2.0.3). This has been reported to Oracle, but determined not to be a defect (bug 17347125).

This requires a bit more work to achieve effectively the same result:

select b.sys_ts,b.ora_rowscn from
    (select rid from 
        (select rowid as rid from table order by sys_ts desc) 
    where rownum <= 25) a, table b
where a.rid = b.rowid;

[1] - http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a dba.stackexchange
scroll top