Jump to content

Salting

From Open Lakehouse Wiki
Salting
Also known as Key salting, salted join
Field Distributed data processing
Key parts Random salt column on the skewed side, replicated (exploded) other side, join on key plus salt
Open source implementations Applies to any shuffle-based engine; usually written by hand in Apache Spark
Related Data skew, Shuffle, Adaptive Query Execution, Join strategy hints
Last verified 23 November 2026 against the v4.3.0 tag of apache/spark (docs/sql-performance-tuning.md, adaptive/OptimizeSkewedJoin.scala, python/pyspark/sql/functions, docs/rdd-programming-guide.md), SPARK-29544 and the Databricks skew and spill guide.

Salting is a manual technique for data skew in which a random number, the salt, is added to a hot join or grouping key so that the rows for that key are spread over several tasks instead of one. In Apache Spark before 3.0 the engine had no automatic skew join handling, so salting was done by hand; since Spark 3.0, Adaptive Query Execution (AQE) splits most skewed joins automatically, and salting is kept for the cases AQE does not cover.[1][2]

How salting works

A shuffle sends all rows with the same key to the same partition. If one key holds most of the rows, one task does most of the work.[3] The Databricks guide to skew describes the remedy as appending a random bucket suffix to the skewed keys, and, for an aggregation, grouping and aggregating in two stages.[4] For a join, salting changes the shuffle key from key to (key, salt), where salt is an integer from 0 to N - 1:

  1. On the large, skewed side, add a column with a random salt for each row.
  2. On the other side, replicate each row N times, once per salt value (for example with explode over a sequence, which returns one row per array element).[5]
  3. Join on both the original key and the salt.

Each hot key is now split across N shuffle partitions. Because each salted row carries exactly one salt value and the other side has a copy for every value, each salted row still meets every matching row from the other side exactly once. The cost is that the other side grows N times, so N is kept as small as the skew allows.

The result is unchanged only when no row of the replicated side has to appear on its own. That holds for an inner join, a left outer join that preserves the salted side, and a left semi or left anti join with the salted side on the left. It does not hold when the join must keep unmatched rows of the replicated side, as a right outer join that preserves the replicated side or a full outer join does: an unmatched replicated row then appears once per salt value, N times instead of once.

Example in PySpark

from pyspark.sql import functions as F

N = 8  # number of salt values

orders_salted = orders.withColumn("salt", (F.rand() * N).cast("int"))

customers_salted = customers.withColumn(
    "salt", F.explode(F.sequence(F.lit(0), F.lit(N - 1)))
)

joined = orders_salted.join(customers_salted, ["customer_id", "salt"]).drop("salt")

rand(), sequence() and explode() are built-in functions of pyspark.sql.functions.[5] A variant salts only the known hot keys and leaves other keys unsalted, which keeps the replicated side small.

Salting a full outer join

A full outer join can be rebuilt from parts that salt correctly: a salted left outer join that keeps every row of the skewed side, plus the rows of the other side that have no match, found with an unsalted left anti join against the distinct keys of the skewed side.

matched_or_left = orders_salted.join(customers_salted, ["customer_id", "salt"], "left").drop("salt")

order_keys = orders.select("customer_id").distinct()
right_only = customers.join(order_keys, "customer_id", "left_anti")

full = matched_or_left.unionByName(right_only, allowMissingColumns=True)

Salting an aggregation

For a skewed GROUP BY, salting runs the aggregation in two steps: first group by (key, salt), then group the partial results by key.[4] This works for aggregates that can be combined, such as sum, count, min and max; an average is computed from a salted sum and a salted count.

When does AQE make salting unnecessary?

AQE skew join handling, added in Spark 3.0.0 and on by default whenever AQE is on (the default since Spark 3.2.0), detects a skewed shuffle partition at runtime and splits it into smaller tasks, replicating the matching partition of the other side. That is the same idea as salting, done by the engine and only for the partitions that need it.[1][2] A partition counts as skewed when it is over 5 times the median size and over 256 MB, by default.[1]

For an inner join, a left or right outer join on the side that can be split, or a semi or anti join, AQE usually makes salting unnecessary.[6] Salting is still used when:

  • The join type cannot be split. In Spark 4.3.0, AQE splits the left side only for inner, cross, left semi, left anti and left outer joins, and the right side only for inner, cross and right outer joins. A full outer join, or a left outer join whose right side is skewed, is not split.[6]
  • The skew is in an aggregation or window. The AQE skew rule targets sort-merge and shuffled hash joins, not aggregations.[6]
  • AQE is off (spark.sql.adaptive.enabled=false) or skew join handling is off (spark.sql.adaptive.skewJoin.enabled=false).[1]
  • The skew is below the thresholds but still slows the stage. Lowering spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes or skewedPartitionFactor is often simpler than salting in that case.[1]

Salting compared with other skew remedies

Remedy Who does it Cost Join types
AQE skew join (Spark) The engine, at runtime Replicates only the matching partition of the other side Splittable sides only (see above)[6]
Broadcast join The planner, or a BROADCAST hint The small side is copied to every executor[1] Most join types; which side can be broadcast depends on the join type
Salting The user, in the query Replicates the whole other side N times Inner joins; left outer, left semi and left anti joins with the salted side on the left; full outer joins only when rebuilt as above; also aggregations

See also

References

  1. ↑ 1.0 1.1 1.2 1.3 1.4 1.5 Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 SPARK-29544: Optimize skewed join at runtime with new Adaptive Execution. Apache Software Foundation JIRA.Retrieved 2026-11-23.
  3. ↑ RDD Programming Guide: Shuffle operations (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ 4.0 4.1 Skew and spill. Databricks.Retrieved 2026-11-23.
  5. ↑ 5.0 5.1 Functions (PySpark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ 6.0 6.1 6.2 6.3 OptimizeSkewedJoin.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.