Sub topic 1
How a Delta Table Is Stored
Prerequisites
You should know basic SQL, SELECT, and WHERE. You should also understand that Parquet is a column-oriented file format and that a Spark job can run tasks in parallel.
Delta Storage Model
A Delta table stores data in Parquet files. It also has a transaction log in the _delta_log directory. The log records which files belong to each table version, along with schema changes and other actions.
A query uses the log to find the current files. Delta can therefore provide consistent reads while writers add new files or replace old ones.
Why Many Small Files Hurt
Streaming writes, frequent incremental jobs, and many partitions can create thousands of small Parquet files. Each file adds work for the query engine:
- The engine must list and open more files.
- The transaction log contains more file actions.
- Spark may create many small tasks, increasing scheduling overhead.
- File statistics cover smaller row ranges, so queries may need to inspect more files.
Use table metadata and history to investigate the layout and recent write patterns:
DESCRIBE DETAIL events;
DESCRIBE HISTORY events;DESCRIBE DETAIL includes numFiles and sizeInBytes. A high file count compared with the table size suggests a small-files problem. History can show whether frequent writes or table rewrites caused the growth.
Data Skipping and File Statistics
Delta records statistics such as minimum and maximum values for columns in each Parquet file. When a query filters on a column, the engine can skip a file whose statistics prove that it cannot contain matching rows. This is called data skipping.
Data skipping works best when related values are stored together. Randomly distributed values make file ranges overlap, so fewer files can be skipped.
> Data skipping reduces reading. It does not replace a correct filter, and it cannot skip a file when its statistics overlap the filter range.
Compacting Files with OPTIMIZE
OPTIMIZE rewrites many small files into fewer, larger files. It preserves table contents while reducing per-file overhead.
OPTIMIZE events;For a partitioned table, limit the rewrite to the relevant partition range:
OPTIMIZE events
WHERE event_date >= '2024-01-01';Compaction improves file management. It does not automatically arrange rows for every query pattern.
Exercise
What does OPTIMIZE change, and what does the Delta transaction log provide?
Show answer
OPTIMIZE rewrites the physical Parquet files into a more efficient layout. The transaction log identifies the active files and records each table version.