I’m copying data from a table to an other one. I’m wondering what would be better to maintain data integrity and performance.
Steps :
- copy data older than x days from log to archive
- remove data from log if it exist in archive
From
INSERT INTO archive
SELECT * FROM logs
WHERE datediff(day, logs.timestamp, GetDate()) > @day
DELETE logsf FROM logs As logsf
INNER JOIN archive as archivef ON logsf.uuid = archivef.uuid
WHERE datediff(day, logsf.timestamp, GetDate()) > @jour
To
INSERT INTO archive
SELECT * FROM logs
WHERE datediff(day, logs.timestamp, GetDate()) > @day
AND NOT EXISTS (
SELECT * FROM archive
WHERE datediff(day, logs.timestamp, GetDate()) > @day
)
DELETE logsf FROM logs As logsf
INNER JOIN archive as archivef ON logsf.uuid = archivef.uuid
WHERE datediff(day, logsf.timestamp, GetDate()) > @jour
- Is this a good thing to do to ensure you are not trying to insert already existing data in a table?
- If my 2 original query are within a transaction, is my 2nd option pointless (and adding useless processing time)?
Which one would you use :
Insert if not exist + Delete if exist (independance)
or
Insert and delete if no error (transaction)
or
combine both
and why?
With SQL Server you can use the output clause to put the deleted items directly into the archive table: