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