<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Salting</id>
	<title>Salting - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Salting"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Salting&amp;action=history"/>
	<updated>2026-12-06T00:14:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Salting&amp;diff=176&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Salting&amp;diff=176&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:50Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Salting&lt;br /&gt;
| also_known_as   = Key salting, salted join&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = Random salt column on the skewed side, replicated (exploded) other side, join on key plus salt&lt;br /&gt;
| implementations = Applies to any shuffle-based engine; usually written by hand in [[Apache Spark]]&lt;br /&gt;
| related         = [[Data skew]], [[Shuffle]], [[Adaptive Query Execution]], [[Join strategy hints]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|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}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Salting&amp;#039;&amp;#039;&amp;#039; 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.&amp;lt;ref name=&amp;quot;perf&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-performance-tuning.html |title=Performance Tuning (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;spark29544&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-29544 |title=SPARK-29544: Optimize skewed join at runtime with new Adaptive Execution |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How salting works ==&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;shuffle&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html#shuffle-operations |title=RDD Programming Guide: Shuffle operations (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; 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.&amp;lt;ref name=&amp;quot;dbxskew&amp;quot;&amp;gt;{{Cite web |url=https://docs.databricks.com/aws/en/optimizations/spark-ui-guide/long-spark-stage-page |title=Skew and spill |publisher=Databricks |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; For a join, salting changes the shuffle key from &amp;lt;code&amp;gt;key&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;(key, salt)&amp;lt;/code&amp;gt;, where &amp;lt;code&amp;gt;salt&amp;lt;/code&amp;gt; is an integer from 0 to N - 1:&lt;br /&gt;
&lt;br /&gt;
# On the large, skewed side, add a column with a random salt for each row.&lt;br /&gt;
# On the other side, replicate each row N times, once per salt value (for example with &amp;lt;code&amp;gt;explode&amp;lt;/code&amp;gt; over a sequence, which returns one row per array element).&amp;lt;ref name=&amp;quot;pyfuncs&amp;quot; /&amp;gt;&lt;br /&gt;
# Join on both the original key and the salt.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Example in PySpark ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql import functions as F&lt;br /&gt;
&lt;br /&gt;
N = 8  # number of salt values&lt;br /&gt;
&lt;br /&gt;
orders_salted = orders.withColumn(&amp;quot;salt&amp;quot;, (F.rand() * N).cast(&amp;quot;int&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
customers_salted = customers.withColumn(&lt;br /&gt;
    &amp;quot;salt&amp;quot;, F.explode(F.sequence(F.lit(0), F.lit(N - 1)))&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
joined = orders_salted.join(customers_salted, [&amp;quot;customer_id&amp;quot;, &amp;quot;salt&amp;quot;]).drop(&amp;quot;salt&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;rand()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;sequence()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;explode()&amp;lt;/code&amp;gt; are built-in functions of &amp;lt;code&amp;gt;pyspark.sql.functions&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;pyfuncs&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/reference/pyspark.sql/functions.html |title=Functions (PySpark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; A variant salts only the known hot keys and leaves other keys unsalted, which keeps the replicated side small.&lt;br /&gt;
&lt;br /&gt;
=== Salting a full outer join ===&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
matched_or_left = orders_salted.join(customers_salted, [&amp;quot;customer_id&amp;quot;, &amp;quot;salt&amp;quot;], &amp;quot;left&amp;quot;).drop(&amp;quot;salt&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
order_keys = orders.select(&amp;quot;customer_id&amp;quot;).distinct()&lt;br /&gt;
right_only = customers.join(order_keys, &amp;quot;customer_id&amp;quot;, &amp;quot;left_anti&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
full = matched_or_left.unionByName(right_only, allowMissingColumns=True)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Salting an aggregation ===&lt;br /&gt;
For a skewed &amp;lt;code&amp;gt;GROUP BY&amp;lt;/code&amp;gt;, salting runs the aggregation in two steps: first group by &amp;lt;code&amp;gt;(key, salt)&amp;lt;/code&amp;gt;, then group the partial results by &amp;lt;code&amp;gt;key&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;dbxskew&amp;quot; /&amp;gt; This works for aggregates that can be combined, such as &amp;lt;code&amp;gt;sum&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;count&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;max&amp;lt;/code&amp;gt;; an average is computed from a salted sum and a salted count.&lt;br /&gt;
&lt;br /&gt;
== When does AQE make salting unnecessary? ==&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;spark29544&amp;quot; /&amp;gt; A partition counts as skewed when it is over 5 times the median size and over 256 MB, by default.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;ref name=&amp;quot;rule&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/OptimizeSkewedJoin.scala |title=OptimizeSkewedJoin.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Salting is still used when:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;The join type cannot be split.&amp;#039;&amp;#039;&amp;#039; 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.&amp;lt;ref name=&amp;quot;rule&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;The skew is in an aggregation or window.&amp;#039;&amp;#039;&amp;#039; The AQE skew rule targets sort-merge and shuffled hash joins, not aggregations.&amp;lt;ref name=&amp;quot;rule&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;AQE is off&amp;#039;&amp;#039;&amp;#039; (&amp;lt;code&amp;gt;spark.sql.adaptive.enabled=false&amp;lt;/code&amp;gt;) or skew join handling is off (&amp;lt;code&amp;gt;spark.sql.adaptive.skewJoin.enabled=false&amp;lt;/code&amp;gt;).&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;The skew is below the thresholds&amp;#039;&amp;#039;&amp;#039; but still slows the stage. Lowering &amp;lt;code&amp;gt;spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;skewedPartitionFactor&amp;lt;/code&amp;gt; is often simpler than salting in that case.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Salting compared with other skew remedies ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Remedy !! Who does it !! Cost !! Join types&lt;br /&gt;
|-&lt;br /&gt;
| AQE skew join (Spark)&lt;br /&gt;
| The engine, at runtime&lt;br /&gt;
| Replicates only the matching partition of the other side&lt;br /&gt;
| Splittable sides only (see above)&amp;lt;ref name=&amp;quot;rule&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| [[Broadcast join]]&lt;br /&gt;
| The planner, or a &amp;lt;code&amp;gt;BROADCAST&amp;lt;/code&amp;gt; hint&lt;br /&gt;
| The small side is copied to every executor&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
| Most join types; which side can be broadcast depends on the join type&lt;br /&gt;
|-&lt;br /&gt;
| Salting&lt;br /&gt;
| The user, in the query&lt;br /&gt;
| Replicates the whole other side N times&lt;br /&gt;
| 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&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Data skew]]&lt;br /&gt;
* [[Shuffle]]&lt;br /&gt;
* [[Adaptive Query Execution]]&lt;br /&gt;
* [[Join strategy hints]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>