Posts

Showing posts with the label NOT EXIST performance issues

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...