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 :

  1. copy data older x days log archive
  2. 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        
  1. is thing ensure not trying insert existing data in table?
  2. 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

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -