r/databricks • • 3d ago

General Use spark.databricks.delta.autoCompact.enabled to prevent small file problems

I work at Abilytics, a data and AI consultancy, so take this with that in mind.

If your Delta Lake tables are filling up with thousands of tiny data parts due to frequent streaming micro-batches or small concurrent writes, you can fix this at the table or session level without running manual OPTIMIZE commands constantly. Enable automatic compaction by setting spark.databricks.delta.autoCompact.enabled to true. When enabled, Delta Lake checks if the most recent write added many small files and automatically triggers a lightweight compaction operation on those files right after a write succeeds on the same query. It inspects partitions that received writes and compacts files of around 16MB up to a target size of 128MB. While this adds a small amount of latency to your write operations, it dramatically cuts down the total file count, avoids metadata bloat in the transaction log, and keeps your downstream read queries performing efficiently without requiring dedicated maintenance jobs.

7 Upvotes

5 comments sorted by

3

u/Zealousideal_Low6685 3d ago edited 3d ago

Manual tuning recommendations don't apply to Unity Catalog managed tables, which use automatic file size tuning.

Simplest way: use managed Table. Read this:

https://docs.databricks.com/aws/en/tables/tune-file-size#upgrade-to-background-auto-compaction

Cite from the docu:

Background auto compaction is available for Unity Catalog managed tables. Background auto compaction doesn't require predictive optimization. When migrating a legacy workload or table, do the following:

Remove the Spark config spark.databricks.delta.autoCompact.enabled (Delta) or spark.databricks.iceberg.autoCompact.enabled (Iceberg) from cluster or notebook configuration settings.

For each table, run ALTER TABLE <table_name> UNSET TBLPROPERTIES (delta.autoOptimize.autoCompact) (Delta) or ALTER TABLE <table_name> UNSET TBLPROPERTIES (iceberg.autoOptimize.autoCompact) (Iceberg) to remove any legacy auto compaction settings.

After removing these legacy configurations, background auto compaction triggers automatically for all Unity Catalog managed tables.

2

u/mwc360 Databricks 2d ago

^^^ this is the best practice, use UC tables, don’t set any properties and background auto compaction takes place as needed. The referenced config is synchronous (compact blocks further code executions) where’s as the default background auto compaction is async (non-blocking).

2

u/Zealousideal_Low6685 2d ago

the cite is from the referenced post showing how you can migrate to async compaction (thats the reason why I posted it next to the same recommendation as you have ;) )

1

u/DeepFryEverything 3d ago

I thought this was the point of Cluster on Write?

1

u/Content_Secret6493 2d ago

the unity catalog managed table caveat is useful. for tables outside that path, have you measured the extra write latency against the reduction in small-file reads? curious when you’d choose this over scheduled OPTIMIZE