cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

What are the general performance optimization tips for improving MERGE performance

User16869510359
Esteemed Contributor
 
1 ACCEPTED SOLUTION

Accepted Solutions

User16869510359
Esteemed Contributor

While using MERGE INTO statement, if the source data that will be merged into the target delta table is small enough to be fit into memory of the worker nodes, then it makes sense to broadcast the source data. By doing so, the execution can avoid the shuffle stage, and thereby MERGE INTO can perform better. Below e.g. shows how you can specify the broadcast hint in the MERGE INTO command.

The below example uses the broadcast to broadcast the source data. In this example, targetDeltaTable is the target delta lake table into which you are merging into. The sourceDataFrame is the source data that you wanted to be inserted into the target Delta Lake table. You need to enclose the sourceDataFrame within broadcast statement for it to be broadcasted. "baseline.key = inputs.key" is your Join condition. "partitionPruneString" is the partition pruning. This contains the list of distinct keys in the sourceDataFrame. By specifying this in the MERGE INTO statement partition pruning takes place and helps with better performance.

targetDeltaTable.as("baseline")
.merge(broadcast(sourceDataFrame.as("inputs")), "baseline.date IN (" + partitionPruneString + ")" + "AND baseline.key = inputs.key")
.whenMatched().updateAll()
.whenNotMatched().insertAll()
.execute()

View solution in original post

1 REPLY 1

User16869510359
Esteemed Contributor

While using MERGE INTO statement, if the source data that will be merged into the target delta table is small enough to be fit into memory of the worker nodes, then it makes sense to broadcast the source data. By doing so, the execution can avoid the shuffle stage, and thereby MERGE INTO can perform better. Below e.g. shows how you can specify the broadcast hint in the MERGE INTO command.

The below example uses the broadcast to broadcast the source data. In this example, targetDeltaTable is the target delta lake table into which you are merging into. The sourceDataFrame is the source data that you wanted to be inserted into the target Delta Lake table. You need to enclose the sourceDataFrame within broadcast statement for it to be broadcasted. "baseline.key = inputs.key" is your Join condition. "partitionPruneString" is the partition pruning. This contains the list of distinct keys in the sourceDataFrame. By specifying this in the MERGE INTO statement partition pruning takes place and helps with better performance.

targetDeltaTable.as("baseline")
.merge(broadcast(sourceDataFrame.as("inputs")), "baseline.date IN (" + partitionPruneString + ")" + "AND baseline.key = inputs.key")
.whenMatched().updateAll()
.whenNotMatched().insertAll()
.execute()

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.