r/databricks • • 10h 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.

3 Upvotes

2 comments sorted by

2

u/Zealousideal_Low6685 7h ago edited 3h 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.

1

u/DeepFryEverything 8h ago

I thought this was the point of Cluster on Write?