Efficient / Alternate Inserts with NOT EXISTS or EXISTS condition in SQL Server (UPSERT with MERGE Statement)
Efficient / Alternate Inserts with NOT EXISTS or EXISTS condition in SQL Server (UPSERT with MERGE Statement)
Normally, we are inserting records into a target table, to avoid duplicates, we use a NOT EXISTS or EXISTS condition. This condition work good if the target table has less number of records.
IF the Target table has millions of records, this exists condition becomes a costly operation as it needs to check with every record in table while inserting the current record. To avoid this performance issue, we make use of MERGE statement in SQL Server.
In the below query, we can insert when the records into target table when that record is not available and can update the record when it is already available.
Source Table: col1,col2,joiningCol1,joiningCol2
Target Table: col3,col4,JoiningCol1,joiningCol2
MERGE INTO TargetTable AS Target
USING(
SELECT *
FROM SourceTable
WHERE col1>1)
AS Source
ON Source.joiningCol1 = Target.joiningCol1 AND joiningCol2=Target.joiningCol2
WHEN MATCHED THEN
UPDATE SET Target.col3=Source.col1 AND Target.col4=Source.col2
NOT MATCHED THEN
INSERT (col3,col4)
VALUES (Source.col1,Source.col2);
I have used this query for the table that has a billion records already and the operation took less than a minute to get executed...
Normally, we are inserting records into a target table, to avoid duplicates, we use a NOT EXISTS or EXISTS condition. This condition work good if the target table has less number of records.
IF the Target table has millions of records, this exists condition becomes a costly operation as it needs to check with every record in table while inserting the current record. To avoid this performance issue, we make use of MERGE statement in SQL Server.
In the below query, we can insert when the records into target table when that record is not available and can update the record when it is already available.
Source Table: col1,col2,joiningCol1,joiningCol2
Target Table: col3,col4,JoiningCol1,joiningCol2
MERGE INTO TargetTable AS Target
USING(
SELECT *
FROM SourceTable
WHERE col1>1)
AS Source
ON Source.joiningCol1 = Target.joiningCol1 AND joiningCol2=Target.joiningCol2
WHEN MATCHED THEN
UPDATE SET Target.col3=Source.col1 AND Target.col4=Source.col2
NOT MATCHED THEN
INSERT (col3,col4)
VALUES (Source.col1,Source.col2);
I have used this query for the table that has a billion records already and the operation took less than a minute to get executed...
Comments
Post a Comment