Sub topic 9
Data skipping: reading fewer files
This chapter assumes you know basic SQL, can run queries on a Databricks cluster or SQL warehouse, and understand that a Delta table is a set of Parquet data files plus a transaction log. Here you will learn how Delta Lake avoids reading files a query cannot need.
How data skipping works
When Delta writes a data file, it records statistics for that file in the transaction log. By default these are the minimum value, maximum value and null count for the first 32 columns of the table, plus the row count.
At query time, the engine compares your filter with those statistics. If a file's range cannot contain matching rows, the engine skips the file without opening it.
Suppose a file holds order_date values from 2023-03-01 to 2023-03-31. A query with WHERE order_date >= '2024-01-01' skips it, because the file's maximum is below the filter.
Why layout decides the benefit
Statistics only help when ranges are narrow and do not overlap. If every file contains a mix of old and new dates, each file's range spans the whole table and no file can be ruled out.
So skipping depends on how rows are placed in files. Data written in arrival order often skips well on a date column, because each file covers a short time span. A column like customer_id is usually scattered across all files, so a filter on it skips almost nothing until you reorganize the data.
The next chapter covers the tools for that: partitioning, Z-ordering and liquid clustering. For now, the key point is that statistics describe the layout you already have.
Checking whether skipping happened
Run your query, then open the Query Profile and look at the scan operator. Compare these metrics:
| Metric | What it tells you |
|---|---|
| Files read | Files the scan actually opened |
| Files pruned | Files skipped using statistics or partitions |
| Bytes read | Data pulled from storage |
If files pruned is near zero on a selective filter, the filter column is not clustered in the files.
Common errors
- Filtering on a column outside the first 32. Such columns have no statistics by default. Reorder the schema or change the
delta.dataSkippingNumIndexedColstable property. - Wrapping the column in a function.
WHERE year(order_date) = 2024can prevent skipping. PreferWHERE order_date >= '2024-01-01' AND order_date < '2025-01-01'. - Expecting skipping on long strings. Min and max values for long strings are truncated, so ranges become loose.
- Skipping on a column that is never filtered. Statistics cost little, but layout work spent on unused columns is wasted.
ALTER TABLE orders
SET TBLPROPERTIES ('delta.dataSkippingNumIndexedCols' = '8');This setting collects statistics only for the first 8 columns. This helps wide tables with long text columns, because collecting statistics on every column slows writes.
Exercise
A table has files written daily, each holding one day of data. Query A filters on event_date = '2024-05-01'. Query B filters on user_id = 42, and every file contains many users. Which query benefits more from data skipping, and why?
Show answer
Query A. Each file's min and max for event_date cover a single day, so almost all files can be skipped. For user_id, every file's range includes 42, so no file can be ruled out. Query B needs a layout change such as Z-ordering or liquid clustering.