Merges small Parquet files into larger ones to improve query performance. When data is written in small batches, DuckLake creates many small files which slows down reads. Compaction consolidates these files.
When to compact:
After many small inserts (e.g., streaming data, row-by-row imports)
When
db_file_stats()shows high file counts with small average sizesBefore running large analytical queries on frequently-updated tables
Important notes:
Compaction is memory-intensive; use
max_filesto limit batch sizeFiles with different schema versions cannot be merged together
Old files are not immediately deleted; run
db_cleanup_files()after
See also
db_file_stats() to check file statistics before compacting,
db_cleanup_files() to remove old files after compaction
Examples
if (FALSE) { # \dontrun{
db_connect()
# Check if compaction is needed
db_file_stats()
# Compact a specific table
db_compact(table = "imports")
# Compact with memory limit (process 500 files at a time)
db_compact(table = "imports", max_files = 500)
# Compact all tables in a schema
db_compact(schema = "trade")
# Compact entire catalog
db_compact()
# Clean up old files after compaction
db_cleanup_files()
} # }