← Delta Optimization track

Databricks · Delta Optimization · Lesson 07

Auto Optimize
& autotuning.

Two settings — optimized writes and auto compaction — keep your files a healthy size automatically. And when you don't pick a size, Databricks autotunes it by how big the table is.

= optimizeWrite + autoCompact Autotune 256 MB → 1 GB > 1 TB? still OPTIMIZE Verified Jun 2026 docs

What it is

"Auto optimize" is an umbrella name for two settings working together so you don't drown in tiny files:

This lesson ties the two together and answers the next question: how big should a "right-sized" file be? That's the target file size — either an explicit number you set (delta.targetFileSize), or, when you set nothing, an autotuned size based on how big the table is (256 MB for most tables, scaling to 1 GB for very large ones).

Analogy
Auto optimize is the self-cleaning kitchen. Optimized writes plates food onto a few big plates instead of dozens of tiny ramekins (write-time); auto compaction is the busser who clears and consolidates leftover small plates right after the meal (post-write). Target file size is "use dinner plates, not saucers" — and autotuning is "the bigger the banquet, the bigger the platters."

Real-world use case
A streaming-fed bronze.events table getting hundreds of tiny micro-batch commits per hour. Turn on both settings so each commit writes larger files and gets compacted right after — the small-file problem never builds up. For long-term consolidation and skipping you still schedule OPTIMIZE / use liquid clustering.

Why it matters

The one rule to remember Auto optimize is for routine, automatic small-file control. For tables larger than 1 TB, you still schedule OPTIMIZE to consolidate — and prefer liquid clustering when you need data laid out for fast filtering.

See it work — the autotune curve

When delta.targetFileSize is not set, Databricks picks the target file size from the table's size. Drag the slider to a table size and watch the target — and the rough file count — change.

Autotune target-file-size curve

No explicit targetFileSize → target scales with table size: 256 MB below 2.56 TB, linearly 256 MB→1 GB from 2.56–10 TB, 1 GB above 10 TB.

—
Autotune curve — target file size vs table size
—
autotuned target / file
—
approx. file count
—
autotune band

Simplified illustration. The real target is computed from table size by Databricks; file count here is target-implied (size ÷ target) and ignores partitioning, the growing-target caveat, and any explicit targetFileSize.

Target file size & autotuning

1 · delta.targetFileSize — the explicit target

Sets the size each file should aim for. Accept a readable size like '100mb' or raw bytes like 104857600. Default is None (unset → autotuning). When set, it's honored by OPTIMIZE, liquid clustering, auto compaction, and optimized writes.

Managed-table caveat On UC managed tables with a SQL warehouse, or DBR 11.3 LTS+, only OPTIMIZE respects targetFileSize — the write-time / auto-compaction paths autosize. Setting it won't change every write path; run OPTIMIZE to apply it.
SQL-- Pin an explicit target. Accepts '100mb' or a raw byte count (104857600).
ALTER TABLE main.delta_opt_demo.events
  SET TBLPROPERTIES ('delta.targetFileSize' = '128mb');  -- honored by OPTIMIZE, LC, autoCompact, optimizeWrite

-- Equivalent using raw bytes (128 MB = 134217728). Same effect.
ALTER TABLE main.delta_opt_demo.events
  SET TBLPROPERTIES ('delta.targetFileSize' = '134217728');

2 · File-size autotuning by table size (when no explicit target)

When targetFileSize is not set, Databricks autotunes the target from the table's size:

Table sizeAutotuned target file size
< 2.56 TB256 MB
2.56–10 TBgrows linearly 256 MB → 1 GB
> 10 TB1 GB
The autotune gotcha A growing target does NOT re-optimize existing files. As a table crosses size thresholds the target rises, but files already written keep their old size — so large tables may keep some small files unless you set a fixed targetFileSize (or run OPTIMIZE to rewrite them).
SQL-- Autotuning is the DEFAULT (no targetFileSize set) — nothing to enable.
-- To AVOID the "growing target leaves old small files" caveat on a huge table,
-- pin a fixed target and OPTIMIZE so existing files are rewritten to it:
ALTER TABLE main.delta_opt_demo.events
  SET TBLPROPERTIES ('delta.targetFileSize' = '512mb');  -- fixed target for a very large table
OPTIMIZE main.delta_opt_demo.events;                  -- rewrite existing files to the new target

3 · delta.tuneFileSizesForRewrites

Biases sizing toward rewrite-friendly sizes for tables that see frequent rewrites (heavy MERGE/UPDATE/DELETE), where smaller files make each rewrite cheaper. Niche — most workloads are fine with autotuning or one targetFileSize.

SQL-- For very rewrite-heavy tables, bias toward rewrite-friendly file sizes.
ALTER TABLE main.delta_opt_demo.events
  SET TBLPROPERTIES ('delta.tuneFileSizesForRewrites' = 'true');

4 · UC managed tables autotune by default

Managed = self-sizing
Unity Catalog managed tables get automatic file-size tuning by default (DBR 11.3 LTS+ / SQL warehouse), and can benefit from background auto compaction that does not require predictive optimization. EXTERNAL tables don't get the same automatic behavior.

SQL-- On UC MANAGED tables, file-size tuning is automatic by default. Inspect:
DESCRIBE EXTENDED main.delta_opt_demo.events;   -- properties + Predictive Optimization field
SHOW TBLPROPERTIES main.delta_opt_demo.events;  -- see autoOptimize.* and targetFileSize if set

Auto-optimize decision map

Pick a scenario and see which levers apply — and which fall to OPTIMIZE / liquid clustering / predictive optimization. The point: auto optimize handles the routine; the rest handle whole-table layout.

Which levers apply?

Choose an operation / scenario.

—
optimizeWrite
—
autoCompact
—
scheduled OPTIMIZE
—
liquid clustering

Simplified illustration. "Applies" is a guidance signal, not a guarantee for every table/runtime; for MERGE/UPDATE/DELETE optimized writes & auto compaction are always on (can't disable).

How it works — the umbrella & what it doesn't do

"Auto optimize" = optimizeWrite + autoCompact

It's not a single switch — it's the pair that runs at different moments of the same write: optimizeWrite shuffles before the write (fewer, larger files); autoCompact compacts after the commit (within partitions). They're independent — you can enable either alone. "Auto optimize" is a legacy umbrella term; modern guidance thinks in the individual settings.

SQL-- Enable BOTH halves of "auto optimize" on a table.
ALTER TABLE main.delta_opt_demo.events
  SET TBLPROPERTIES (
    'delta.autoOptimize.optimizeWrite' = 'true',   -- write-time: fewer, larger files (Lesson 05)
    'delta.autoOptimize.autoCompact'   = 'auto'    -- post-write: compact small files; 'auto' autotunes size
  );
SHOW TBLPROPERTIES main.delta_opt_demo.events;       -- confirm both are set
PYSPARK# Session-level equivalents (apply to writes from this Spark session).
spark.conf.set("spark.databricks.delta.optimizeWrite.enabled", "true")   # write-time half
spark.conf.set("spark.databricks.delta.autoCompact.enabled",  "auto")    # post-write half ('auto' autotunes)

Reduce, don't replace OPTIMIZE — and the 1 TB rule

These settings keep each write's files healthy, operating per-write / within partitions. They don't do a global whole-table consolidation, and they don't give data-skipping locality on filter columns. On a large, long-lived table, many independent writes still leave it sub-optimal overall.

SQL-- Auto optimize handles routine sizing; for big tables ALSO consolidate periodically.
OPTIMIZE main.delta_opt_demo.events
WHERE event_date >= DATE'2026-06-01';   -- scope to recent data to keep the job cheap

-- For NEW tables, prefer liquid clustering so data is laid out for fast filtering
-- (same OPTIMIZE triggers clustering incrementally). See Lesson 08.
ALTER TABLE main.delta_opt_demo.events CLUSTER BY (event_type, event_date);

Where auto optimize ends and the rest begins

Who does what

LeverWhen it runsScopeHonors targetFileSize?Reach for it when
optimizeWrite (L05)Write-time (shuffle before write)This writeYes (managed/SQL-warehouse path may autosize)Prevent small files at the source
autoCompact (L06)Post-commit, synchronousWithin partitions of this writeYes ('auto' autotunes)Mop up small files after frequent commits
targetFileSizeConsumed by the above + OPTIMIZE/LCPer table— (it is the target)You know the size, or to pin a huge table
Autotuning (default)When no targetFileSize setPer table, by table sizen/aLet the platform size (256 MB → 1 GB)
Scheduled OPTIMIZE (L04)On demand / scheduledWhole table or WHERE subsetYesWhole-table consolidation; > 1 TB
Liquid clustering (L08)OPTIMIZE triggers itWhole table, by keysYesLay data out for fast filtering on new tables
Predictive optimization (L09)Async, serverless, automaticWhole table (UC managed)via OPTIMIZEHands-off maintenance on managed tables

Uses, edge cases & limitations

Uses

Edge cases an interviewer probes

Limitations

Common gotchas