cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

What are the general performance optimization tips for improving MERGE performance

brickster_2018
Databricks Employee
Databricks Employee
 
1 ACCEPTED SOLUTION

Accepted Solutions

brickster_2018
Databricks Employee
Databricks Employee

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

brickster_2018
Databricks Employee
Databricks Employee

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()

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group