0

My DWH is deployed on Azure Synapse SQL pool. I loaded data to DWH by script that consists of update, insert and delete (u-i-d) operations. The duration of full load to target table was 12minutes for near 50million of rows.
Recently I tried to use MERGE statement instead of u-i-d. And I found that MERGE performance much worse than u-i-d - 1hour for MERGE against 12minutes for u-i-d!

Please share your experience with MERGE statement on Azure synapse, friends! Does MERGE really work worse in Synapse than separate update-insert-delete operations?

niko
  • 11
  • 1

1 Answers1

1

As per MS doc on MERGE (Transact-SQL) - SQL Server | Microsoft Learn, Merge statement works better for complex statements and for simple activities merging using Insert, Update and delete statement works better.

The conditional behavior described for the MERGE statement works best when the two tables have a complex mixture of matching characteristics. For example, inserting a row if it doesn't exist, or updating a row if it matches. When simply updating one table based on the rows of another table, improve the performance and scalability with basic INSERT, UPDATE, and DELETE statements.

  • I tried to repro and compare both approaches using simple statements.
  • Sample tables are taken as in below image.

enter image description here

  • Merge statement is used to merge and it took 9 seconds
MERGE Products AS  TARGET

USING UpdatedProducts AS  SOURCE

ON (TARGET.ProductID =  SOURCE.ProductID)

--When records are matched, update the records if there is any change

WHEN  MATCHED  AND  TARGET.ProductName <>  SOURCE.ProductName OR  TARGET.Rate <>  SOURCE.Rate

THEN  UPDATE  SET  TARGET.ProductName =  SOURCE.ProductName, TARGET.Rate =  SOURCE.Rate

--When no records are matched, insert the incoming records from source table to target table

WHEN  NOT  MATCHED  BY  TARGET

THEN  INSERT (ProductID, ProductName, Rate) VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate)

--When there is a row that exists in target and same record does not exist in source then delete this record target

WHEN  NOT  MATCHED  BY  SOURCE

THEN  DELETE ;

enter image description here

  • Then tried with Update, Insert and delete statement. It took nearly 0 second. enter image description here

Update, insert and delete works better for simple scenarios.

Aswin
  • 4,090
  • 2
  • 4
  • 16