Inner join on an insert to assure integrity
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:
DELETE logs
OUTPUT deleted.* INTO archive
WHERE datediff(day, timestamp, GetDate()) > @jour
I would go with option 2 because it would be a faster process since the SQL is less complex.
I prefer the idea of wrapping it in a transaction so that a roll-back will occur in case of any errors.
精彩评论