sql - Inner join on an insert to assure integrity -
i'm copying data table other one. i'm wondering better maintain data integrity , performance.
steps :
- copy data older x days log archive
- remove data log if exist in archive
from
insert archive select * logs datediff(day, logs.timestamp, getdate()) > @day delete logsf logs logsf inner join archive archivef on logsf.uuid = archivef.uuid datediff(day, logsf.timestamp, getdate()) > @jour
to
insert archive select * logs datediff(day, logs.timestamp, getdate()) > @day , not exists ( select * archive datediff(day, logs.timestamp, getdate()) > @day ) delete logsf logs logsf inner join archive archivef on logsf.uuid = archivef.uuid datediff(day, logsf.timestamp, getdate()) > @jour
- is thing ensure not trying insert existing data in table?
- if 2 original query within transaction, 2nd option pointless (and adding useless processing time)?
which 1 use :
insert if not exist + delete if exist (independance)
or
insert , delete if no error (transaction)
or
combine both
and why?
with sql server can use output clause put deleted items directly archive table:
delete logs output deleted.* archive datediff(day, timestamp, getdate()) > @jour
Comments
Post a Comment