Sub topic 10
Checking file counts, sizes, and query pruning
This chapter assumes you know basic SQL, can run commands in a Databricks notebook or SQL warehouse, and understand that a Delta table is Parquet files plus a transaction log. Before you change a table's layout, measure it. After you change it, measure again.
What to measure
Three signals tell you whether a layout is healthy:
- File count and average file size. Many files far below the target size point to a small-files problem.
- Write history. The operations that created the files show where small files come from.
- Files scanned per query. A query that reads most of the table's files is not benefiting from data skipping.
Step 1: File count and average size
DESCRIBE DETAIL returns one row of table metadata, including numFiles and sizeInBytes.
DESCRIBE DETAIL events;Divide sizeInBytes by numFiles to get the average file size. As a rough guide, files of a few hundred megabytes are healthy for large tables. An average of a few megabytes or less on a large table suggests compaction will help.
You can compute the average directly in SQL:
SELECT
numFiles,
sizeInBytes,
ROUND(sizeInBytes / numFiles / 1024 / 1024, 1) AS avg_file_mb
FROM (DESCRIBE DETAIL events);If your runtime does not accept DESCRIBE DETAIL as a subquery, run it on its own and divide by hand.
Step 2: Find where small files come from
DESCRIBE HISTORY lists each commit to the table, with the operation type and metrics.
DESCRIBE HISTORY events;Look at the operation and operationMetrics columns. A long run of WRITE, MERGE, or STREAMING UPDATE commits that each add only a few files is a common source of small files. OPTIMIZE commits appear in the same list, so you can confirm when compaction last ran and how many files it removed and added.
Step 3: Measure query pruning
Pruning is the share of files a query avoids reading. Run a query that filters on a column you care about, then open its query profile in the Databricks UI. The scan operator reports:
- files read and files pruned
- bytes read
- rows scanned
Compare these numbers before and after a layout change, using the same query and the same filter values.
SELECT COUNT(*)
FROM events
WHERE user_id = 12345;If the scan reads nearly all files for a selective filter like this, the data is not clustered on user_id. After OPTIMIZE ... ZORDER BY (user_id) or liquid clustering on user_id, the same query should read far fewer files.
A before-and-after workflow
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Comparing runs without the same filter values | Pruning numbers are not comparable | Reuse the exact same query |
| Reading only `numFiles` | A table with few huge files can still scan too much | Check average size and files read per query |
| Timing a query on a cold cache, then on a warm one | Speed-up appears larger than it is | Compare files and bytes read, not only elapsed time |
| Forgetting that `numFiles` counts only the current version | Old files remain on storage until `VACUUM` | Check storage separately after `VACUUM` |
Exercise
A table has numFiles = 40000 and sizeInBytes = 80 GB. What is the average file size, and what does it suggest?
Show answer
80 GB divided by 40000 files is about 2 MB per file. That is far below the typical target of hundreds of megabytes, so the table likely has a small-files problem and should be compacted with OPTIMIZE. Check DESCRIBE HISTORY to find which writes create the small files.