Sub topic 3
Data skipping and file statistics
This chapter assumes you know basic SQL, what a Parquet file is, and that a Delta table is Parquet data files plus a transaction log. It explains how Delta avoids reading files a query cannot match, and why file layout decides how well that works.
What data skipping does
Every time Delta writes a data file, it records statistics for that file in the transaction log. By default these are collected for the first 32 columns of the table. For each column they include:
- The minimum value in the file.
- The maximum value in the file.
- The number of null values.
The table also records the total row count per file.
When you run a query with a filter, the engine checks the log before it opens any Parquet file. If the filter cannot match anything between a file's minimum and maximum, the engine skips that file. Skipping happens at the file level and costs no data reads.
SELECT * FROM events WHERE user_id = 4200;For this query, a file with user_id min 5000 and max 9000 is skipped. A file with min 1 and max 8000 must be read, because 4200 falls inside its range.
Why layout decides the benefit
Min/max statistics only help when ranges are narrow and do not overlap much. Consider two layouts of the same rows, with user_id values from 1 to 40,000 split into four files.
| Layout | File 1 range | File 2 range | File 3 range | File 4 range | Files read for `user_id = 4200` |
|---|---|---|---|---|---|
| Rows sorted by `user_id` | 1–10000 | 10001–20000 | 20001–30000 | 30001–40000 | 1 |
| Rows in arrival order | 3–39990 | 1–39950 | 7–40000 | 2–39980 | 4 |
The data and the statistics are identical in kind. Only the grouping of rows into files differs. In the second layout every range spans nearly the whole domain, so no file can be ruled out.
Checking whether skipping works
Data skipping is not something you configure per query. You measure it afterward. In the Spark UI or the query profile, compare the number of files in the table with the number of files the scan read. A selective filter that reads nearly every file indicates poor skipping.
You can get the table's total file count with DESCRIBE DETAIL:
DESCRIBE DETAIL events;The numFiles column gives the total to compare against.
Common errors
- Filtering on a column outside the statistics range. Only the first 32 columns get statistics by default. A filter on column 40 skips nothing. Either reorder columns when you create the table or adjust the
delta.dataSkippingNumIndexedColsproperty. - Expecting skipping on long strings. Statistics for long string values are truncated, so ranges are coarse. Avoid using long free-text columns as filter keys.
- Wrapping the column in a function. A filter like
WHERE upper(country) = 'DE'or a cast on the column often prevents the engine from comparing the literal to min/max values. Filter on the raw column where you can. - Blaming the engine for random layout. If rows arrive in an order unrelated to your filter columns, ranges overlap in every file. Skipping fails because of layout, and the next chapters cover how to change it.
Exercise
A table has 1,000 files. A query filtering on customer_id = 77 reads 990 of them. The first 32 columns include customer_id. What is the most likely cause, and what kind of fix addresses it?
Show answer
Each file's min/max range for customer_id is wide and overlaps with the others, because rows with the same or nearby customer IDs are spread across many files. The fix is to change the layout so similar customer_id values are stored together, for example with Z-ordering or liquid clustering on customer_id.