Databricks Serverless Webinar
Watch On-Demand
Close navigation menu icon in white

Partition pruning lets Spark skip partitions a query doesn't need. Static pruning happens at planning time, when a filter sits directly on the partition column. Dynamic partition pruning (DPP) happens at runtime: Spark filters a small dimension table first, then uses the result to skip partitions in the large fact table it joins to. DPP is on by default since Spark 3.0.

Below: how the two compare, when DPP fires, how to check pruning in a plan, and an experiment where the join order decided which tables got pruned.

What partition pruning is

A partitioned table is stored as one folder per partition value, for example one folder per event_date. If a query only needs some of those values, Spark can skip the other folders entirely. No files opened, no bytes read.

On large tables, reading less data is usually the biggest single speedup a query can get. A 1,000-partition table that only needs one partition does roughly 0.1% of the I/O.

Static vs dynamic partition pruning

Static partition pruningDynamic partition pruning (DPP)
When it happensAt planning timeAt runtime
What triggers itA filter on the partition column, with a known valueA join on the partition column, with a selective filter on the other table
ExampleWHERE t1.key = 40 (key is the partition column)WHERE t1.key = t2.dim_key AND t2.val > 40
What you see in the planPartitionFilters with the literal valuePartitionFilters with dynamicpruningexpression
Available sinceLong before Spark 3Spark 3.0

Static pruning also follows equality through a join. If t1.key = 40 and t1.key = t2.dim_key, Spark infers t2.dim_key = 40 and prunes both tables.

How dynamic partition pruning works

DPP is built for star schemas: a large fact table partitioned by a key, joined to a small dimension table that's filtered on some other column.

  1. Spark runs the filter on the dimension table.
  2. It broadcasts the matching join keys.
  3. It pushes those keys into the fact table scan as a partition filter.
  4. The fact table scan reads only the partitions with matching keys.

DPP only fires when:

  • The fact table is partitioned on the join key
  • The join is an equi-join
  • The other side has a filter selective enough to be worth it
  • The filtering side is small enough to broadcast, in most cases

The settings that control it:

SettingDefaultWhat it does
spark.sql.optimizer.dynamicPartitionPruning.enabledtrueTurns DPP on or off
spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnlytrueOnly prune when the broadcast from the join can be reused
spark.sql.optimizer.dynamicPartitionPruning.useStatstrueUses distinct counts to decide if pruning is worth it
spark.sql.optimizer.dynamicPartitionPruning.fallbackFilterRatio0.5Assumed filter ratio when statistics aren't available

Adaptive Query Execution, on by default since Spark 3.2, works alongside DPP.

How to check that pruning happened

In EXPLAIN output, look at the scan node for each partitioned table:

  • PartitionFilters: [isnotnull(key), (key = 40)] means static pruning.
  • PartitionFilters: [isnotnull(key), dynamicpruningexpression(key IN dynamicpruning#...)] means DPP.
  • PartitionFilters: [] means every partition gets read.

In the Spark UI SQL tab, the scan node shows number of partitions read. Compare it to the table's total. That's how we measured everything below.

[cta-banner]

Our experiment: when join order changed what got pruned

We built 3 small tables to mimic a real star-schema workload. The data is tiny, but the partition counts are realistic, and the plans show exactly what Spark reads.

Run on Spark 3.0.1. Behavior may differ in later versions. Test on yours.

TableColumnsPartition columnRowsPartitions
T1key, valkey21,000,0001,000
T2dim_key, valdim_key800400
T3key, valkey11,000,0001,000

Settings for the first half: CBO off, join reorder off, AQE off, and DPP turned off so we could see static pruning alone.

Static pruning: two tables

A plain join reads everything: all 1,000 partitions of T1 and all 400 of T2.

select t1.key from t1, t2 where t1.key = t2.dim_key
Spark plan for a T1 and T2 join with no filter, reading all 1,000 T1 partitions and all 400 T2 partitions

Add a filter on T1's partition column, and Spark reads one partition from each table. It inferred the T2 filter from the join condition.

select t1.key from t1, t2 where t1.key = t2.dim_key and t1.key = 40
Spark plan with a filter on T1 key 40, reading one partition from T1 and one from T2

Putting the filter on T2 instead gives the same result. A range filter works too: 59 partitions read from each table, down from 1,000.

select t1.key from t1, t2 where t1.key = t2.dim_key and t2.dim_key > 40 and t2.dim_key < 100
Spark plan with a range filter on dim_key, reading 59 partitions from each table

Static pruning: three tables, two join orders

Add T3, joined to T1 on its partition column. Same logic says T3 should read one partition too.

select t1.key from t1, t3, t2
where t1.key = t3.key and t1.val = t3.val
and t1.key = t2.dim_key and t2.dim_key = 40
Spark plan for a three-table join listed as T1, T3, T2, where T3 reads all 1,000 partitions

It didn't. T1 and T2 read one partition each. T3 read all 1,000.

Same query, with only the table order in the FROM clause changed:

select t1.key from t1, t2, t3
where t1.key = t3.key and t1.val = t3.val
and t1.key = t2.dim_key and t2.dim_key = 40
Spark plan for the same three-table join listed as T1, T2, T3, where every table reads one partition

Now every table read one partition. In Spark 3.0.1, the order you list tables in can decide whether static pruning reaches all of them.

Dynamic pruning: two tables

Move the filter to a non-partition column on T2. With DPP off, nothing gets pruned.

select t1.key from t1, t2 where t1.key = t2.dim_key and t2.val > 40
Spark plan with a filter on T2 val and DPP off, reading all partitions of both tables

Turn DPP back on and rerun:

set spark.sql.optimizer.dynamicPartitionPruning.enabled = true
Spark plan with DPP on, where T1 reads 360 of 1,000 partitions using a subquery broadcast from T2

T2 still reads all 400 partitions. It's the filtering side. But T1 now reads 360 of 1,000. The plan shows the SubqueryBroadcast from T2 feeding the T1 scan.

Dynamic pruning: three tables

select t1.key from t1, t2, t3
where t1.key = t3.key and t3.key = t2.dim_key and t1.key = t2.dim_key
and t2.val > 40 and t2.val < 100

T1 got pruned to 59 partitions. T3 read all 1,000.

The Spark 3.0.1 code applied only one DPP subquery here. So the join order picked which large table got pruned. List T3 before T1 and DPP goes to T3 instead, which is worse: T1 is almost twice T3's size.

Does the cost-based optimizer fix it?

No. We turned on CBO and join reordering and collected statistics:

set spark.sql.cbo.enabled = true;
set spark.sql.cbo.joinReorder.enabled = true;
ANALYZE TABLE t1 PARTITION (key) COMPUTE STATISTICS NOSCAN;
ANALYZE TABLE t3 PARTITION (key) COMPUTE STATISTICS NOSCAN;
ANALYZE TABLE t2 PARTITION (dim_key) COMPUTE STATISTICS NOSCAN;
ANALYZE TABLE t2 COMPUTE STATISTICS FOR COLUMNS dim_key;
ANALYZE TABLE t1 COMPUTE STATISTICS FOR COLUMNS key;
ANALYZE TABLE t3 COMPUTE STATISTICS FOR COLUMNS key;

The plan didn't change. CBO didn't move DPP to the bigger table.

[cta-banner]

What to do about it in your own queries

  • Filter the small table, then join the big ones to it. Put the selective predicate on the dimension table and join each large partitioned table on its partition column.
  • Check the scan for every large partitioned table in the plan. Look for empty PartitionFilters on any scan with hundreds of partitions.
  • Try a different join order if a big table isn't pruned. It's a cheap test.
  • Collect statistics anyway, but don't count on the cost-based optimizer to fix pruning.
  • Retest after a Spark upgrade. Our results are from 3.0.1. Pruning rules change between versions.

For partition pruning on Databricks, including Delta tables and dynamic file pruning, see Databricks Partition Pruning Mistakes Costing You Performance. For how Spark builds the plan in the first place, see What the Spark Catalyst Optimizer Does for Performance.

How Unravel fits in

Three small tables were enough to show how much a query's shape changes what Spark reads. A real platform runs thousands of queries a day, most of them repeated. Arvix, Unravel's autonomous AI operator, rewrites inefficient queries across your entire fleet, including scans that read far more partitions than they need. Learn more about Spark optimization with Unravel.

Get a free health check to see which of your queries read data they don't need.

See Unravel in action.
Book a Demo
Unlock your data environment health with a free health check.
Request Your Health Check Report
Table of Contents