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
Esteemed Contributor
Esteemed Contributor
 
1 ACCEPTED SOLUTION

Accepted Solutions

brickster_2018
Esteemed Contributor
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

brickster_2018
Esteemed Contributor
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()

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!