<?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=Cost-based_optimizer_(Spark)</id>
	<title>Cost-based optimizer (Spark) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Cost-based_optimizer_(Spark)"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Cost-based_optimizer_(Spark)&amp;action=history"/>
	<updated>2026-12-06T00:08:10Z</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=Cost-based_optimizer_(Spark)&amp;diff=135&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=Cost-based_optimizer_(Spark)&amp;diff=135&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:37Z</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            = Cost-based optimizer (Spark)&lt;br /&gt;
| also_known_as   = CBO, Spark SQL cost-based optimization&lt;br /&gt;
| field           = Query optimization&lt;br /&gt;
| introduced      = Apache Spark 2.2.0&lt;br /&gt;
| key_parts       = Table and column statistics, &amp;lt;code&amp;gt;ANALYZE TABLE&amp;lt;/code&amp;gt;, cardinality estimation, join reordering, histograms&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Catalyst optimizer]], [[Adaptive Query Execution]], [[Broadcast join]], [[Logical plan and physical plan]], [[Hive Metastore]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the Spark 2.2.0 release notes, SPARK-16026, the v4.3.0 tag of apache/spark (SQLConf.scala, docs/sql-performance-tuning.md, docs/sql-ref-syntax-aux-analyze-table.md, docs/sql-ref-syntax-qry-explain.md) and ANALYZE TABLE, DESCRIBE and EXPLAIN COST output from a PySpark 4.3.0 run}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], the &amp;#039;&amp;#039;&amp;#039;cost-based optimizer&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;CBO&amp;#039;&amp;#039;&amp;#039;) is the part of the [[Catalyst optimizer]] that uses table and column statistics, such as row counts, distinct values and minimum and maximum values, to estimate how many rows each step of a query produces, and uses those estimates to choose join orders and join strategies.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&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; Spark 2.2.0 introduced cardinality estimation for filter, join, aggregate, project and limit operators and cost-based join reordering.&amp;lt;ref name=&amp;quot;r220&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-2-2-0.html |title=Spark Release 2.2.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The umbrella issue for the framework, SPARK-16026, was closed with fix version 2.3.0.&amp;lt;ref name=&amp;quot;jira16026&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-16026 |title=SPARK-16026: Cost-based Optimizer Framework |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; In Spark 4.3.0 the CBO is still off by default (&amp;lt;code&amp;gt;spark.sql.cbo.enabled=false&amp;lt;/code&amp;gt;), and most queries rely instead on size estimates and on [[Adaptive Query Execution]] at run time.&lt;br /&gt;
&lt;br /&gt;
== What statistics does Spark use? ==&lt;br /&gt;
The Spark tuning guide lists three sources of statistics:&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Data source&amp;#039;&amp;#039;&amp;#039;: statistics that Spark reads from the data itself, such as the counts and minimum and maximum values in [[Apache Parquet]] file metadata.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Catalog&amp;#039;&amp;#039;&amp;#039;: statistics stored in the catalog, such as the [[Hive Metastore]], and collected or updated by &amp;lt;code&amp;gt;ANALYZE TABLE&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Runtime&amp;#039;&amp;#039;&amp;#039;: statistics that Spark measures while a query runs, used by [[Adaptive Query Execution]].&lt;br /&gt;
&lt;br /&gt;
Without the CBO, Catalyst still uses one statistic everywhere: the estimated size in bytes of each plan node. That size decides, for example, whether a table is small enough for a [[broadcast join]].&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt; When the size of a table is unknown, the internal default &amp;lt;code&amp;gt;spark.sql.defaultSizeInBytes&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;Long.MaxValue&amp;lt;/code&amp;gt;, so the optimizer does not broadcast a table unless it knows the table is small.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do you collect statistics with ANALYZE TABLE? ==&lt;br /&gt;
&amp;lt;code&amp;gt;ANALYZE TABLE&amp;lt;/code&amp;gt; collects statistics for one table, or for all tables in a database, and stores them in the catalog for the optimizer.&amp;lt;ref name=&amp;quot;analyze&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-syntax-aux-analyze-table.html |title=ANALYZE TABLE (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Statement !! What it collects&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ANALYZE TABLE t COMPUTE STATISTICS NOSCAN&amp;lt;/code&amp;gt; || Table size in bytes only, without scanning the data&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ANALYZE TABLE t COMPUTE STATISTICS&amp;lt;/code&amp;gt; || Row count and size in bytes&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ANALYZE TABLE t COMPUTE STATISTICS FOR COLUMNS a, b&amp;lt;/code&amp;gt; || Table statistics plus column statistics for the listed columns&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ANALYZE TABLE t COMPUTE STATISTICS FOR ALL COLUMNS&amp;lt;/code&amp;gt; || Table statistics plus column statistics for every column&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ANALYZE TABLES IN db COMPUTE STATISTICS&amp;lt;/code&amp;gt; || Table statistics for every table in the database&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Source: Spark 4.3.0 SQL reference.&amp;lt;ref name=&amp;quot;analyze&amp;quot; /&amp;gt; A &amp;lt;code&amp;gt;PARTITION (...)&amp;lt;/code&amp;gt; clause limits the statement to some partitions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;DESCRIBE EXTENDED&amp;lt;/code&amp;gt; shows what was collected.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt; After &amp;lt;code&amp;gt;ANALYZE TABLE t1 COMPUTE STATISTICS FOR ALL COLUMNS&amp;lt;/code&amp;gt; on a 1,000-row Parquet table in PySpark 4.3.0, &amp;lt;code&amp;gt;DESCRIBE EXTENDED t1&amp;lt;/code&amp;gt; reported &amp;lt;code&amp;gt;Statistics: 5665 bytes, 1000 rows&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;DESCRIBE EXTENDED t1 id&amp;lt;/code&amp;gt; reported &amp;lt;code&amp;gt;min 0&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;max 999&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;num_nulls 0&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;distinct_count 1000&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;avg_col_len 8&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;max_col_len 8&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;histogram NULL&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Histograms are not collected by default. With &amp;lt;code&amp;gt;spark.sql.statistics.histogram.enabled=true&amp;lt;/code&amp;gt; (default false, since 2.3.0), &amp;lt;code&amp;gt;ANALYZE TABLE ... FOR COLUMNS&amp;lt;/code&amp;gt; also builds equi-height histograms, which improve estimates but cost an extra table scan.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt; Statistics are not refreshed automatically when data changes, unless &amp;lt;code&amp;gt;spark.sql.statistics.size.autoUpdate.enabled&amp;lt;/code&amp;gt; is set (default false), and even then only the table size is updated.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== When is the CBO used? ==&lt;br /&gt;
The CBO is used only when it is turned on, and its parts have separate switches. All of them are off by default in Spark 4.3.0:&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Setting !! Default !! Since !! Effect&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.cbo.enabled&amp;lt;/code&amp;gt; || false || 2.2.0 || Estimate plan statistics (row counts, column statistics) from the collected statistics&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.cbo.joinReorder.enabled&amp;lt;/code&amp;gt; || false || 2.2.0 || Reorder joins by estimated cost (join reordering in the CBO)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.cbo.joinReorder.dp.threshold&amp;lt;/code&amp;gt; || 12 || 2.2.0 || The maximum number of joined nodes for the dynamic programming algorithm&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.cbo.starSchemaDetection&amp;lt;/code&amp;gt; || false || 2.2.0 || Reorder joins with a star schema heuristic&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.cbo.planStats.enabled&amp;lt;/code&amp;gt; || false || 3.0.0 || Fetch row counts and column statistics from the catalog into the logical plan&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The effect is visible with &amp;lt;code&amp;gt;EXPLAIN COST&amp;lt;/code&amp;gt;, which prints the optimized logical plan with statistics.&amp;lt;ref name=&amp;quot;explain&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-syntax-qry-explain.html |title=EXPLAIN (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; In this PySpark 4.3.0 run, table &amp;lt;code&amp;gt;t1&amp;lt;/code&amp;gt; has 1,000 rows and &amp;lt;code&amp;gt;t2&amp;lt;/code&amp;gt; has 10, both analyzed for all columns, and the query is &amp;lt;code&amp;gt;SELECT * FROM t1 JOIN t2 ON t1.k = t2.k WHERE t1.id &amp;lt; 100&amp;lt;/code&amp;gt;. With the CBO off, the filter does not change the size estimate, and the join is estimated at 7.8 MiB:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Join Inner, (k#5104L = k#5105L), Statistics(sizeInBytes=7.8 MiB)&lt;br /&gt;
:- Filter ((isnotnull(id#5103L) AND (id#5103L &amp;lt; 100)) AND isnotnull(k#5104L)), Statistics(sizeInBytes=5.5 KiB)&lt;br /&gt;
:  +- Relation spark_catalog.default.t1[id#5103L,k#5104L] parquet, Statistics(sizeInBytes=5.5 KiB)&lt;br /&gt;
+- Filter isnotnull(k#5105L), Statistics(sizeInBytes=1438.0 B)&lt;br /&gt;
   +- Relation spark_catalog.default.t2[k#5105L,name#5106] parquet, Statistics(sizeInBytes=1438.0 B)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With &amp;lt;code&amp;gt;spark.sql.cbo.enabled=true&amp;lt;/code&amp;gt;, the same plan carries row counts. The filter &amp;lt;code&amp;gt;id &amp;lt; 100&amp;lt;/code&amp;gt; is estimated at 101 rows (the true count is 100), and the join estimate falls to 4.4 KiB:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
Join Inner, (k#5104L = k#5105L), Statistics(sizeInBytes=4.4 KiB, rowCount=101)&lt;br /&gt;
:- Filter ((isnotnull(id#5103L) AND (id#5103L &amp;lt; 100)) AND isnotnull(k#5104L)), Statistics(sizeInBytes=2.4 KiB, rowCount=101)&lt;br /&gt;
:  +- Relation spark_catalog.default.t1[id#5103L,k#5104L] parquet, Statistics(sizeInBytes=23.4 KiB, rowCount=1.00E+3)&lt;br /&gt;
+- Filter isnotnull(k#5105L), Statistics(sizeInBytes=290.0 B, rowCount=10)&lt;br /&gt;
   +- Relation spark_catalog.default.t2[k#5105L,name#5106] parquet, Statistics(sizeInBytes=290.0 B, rowCount=10)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== CBO and Adaptive Query Execution ==&lt;br /&gt;
The CBO and [[Adaptive Query Execution]] solve the same problem at different times. The CBO works before the query runs, from statistics that must be collected in advance and can be stale. AQE works during the query, from exact sizes of completed shuffle stages, but can only change the parts of the plan that have not run yet.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Catalyst optimizer]]&lt;br /&gt;
* [[Adaptive Query Execution]]&lt;br /&gt;
* [[Broadcast join]]&lt;br /&gt;
* [[Logical plan and physical plan]]&lt;br /&gt;
* [[Hive Metastore]]&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>