---
title: Data skipping
description: Data skipping on Databricks uses per-file statistics (min/max, null and row counts) to skip irrelevant files at query time for Delta Lake and Apache Iceberg tables.
canonical_url: https://docs.databricks.com/aws/en/tables/data-skipping
---

# Data skipping

> [!NOTE]
>
> In Databricks Runtime 13.3 and above, Databricks recommends using liquid clustering for table layout. Clustering is not compatible with Z-ordering. See [Use liquid clustering for tables](https://docs.databricks.com/aws/en/tables/clustering.md).

Data skipping statistics are collected automatically when you write data to a Delta Lake or managed Apache Iceberg table. Databricks uses per-file statistics (minimum and maximum values, null counts, and total records) at query time to skip irrelevant files and speed up queries.

You must have statistics collected for columns that are used in `ZORDER` statements. See [What is Z-ordering?](https://docs.databricks.com/aws/en/tables/data-skipping.md#delta-zorder).

## <a id="stats-cols" />Specify statistics columns

For Unity Catalog [external tables](https://docs.databricks.com/aws/en/tables/external.md), statistics are collected on the first 32 columns defined in your table schema by default. For Unity Catalog [managed tables](https://docs.databricks.com/aws/en/tables/managed.md), predictive optimization also collects file-skipping statistics on additional columns that are frequently used in your query filters, beyond the first 32 columns. Statistics are collected on a column after the first 32 only if predictive optimization has selected it. Predictive optimization automatically runs `ANALYZE`, a command for collecting statistics. Databricks recommends enabling predictive optimization for all Unity Catalog managed tables to simplify data maintenance and reduce storage costs. See [Predictive optimization for Unity Catalog managed tables](https://docs.databricks.com/aws/en/optimizations/predictive-optimization.md).

If you aren't using predictive optimization, you can modify the behavior that limits statistics collections to 32 columns by setting one of the following table properties:

| Table property               | Databricks Runtime supported              | Description                                                                                                 |
| ---------------------------- | ----------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| `dataSkippingNumIndexedCols` | All supported Databricks Runtime versions | Increase or decrease the number of columns on which statistics are collected. Depends on column order.      |
| `dataSkippingStatsColumns`   | Databricks Runtime 13.3 LTS and above     | Specify a list of column names for which statistics are collected. Supersedes `dataSkippingNumIndexedCols`. |

Table properties can be set at table creation or with `ALTER TABLE` statements. See [Table properties reference](https://docs.databricks.com/aws/en/tables/table-properties.md). The following example overrides default stats collection behavior to set stats collection on named columns:

### Delta Lake

```sql
ALTER TABLE table_name SET TBLPROPERTIES('delta.dataSkippingStatsColumns' = 'col1, col2, col3')
```

### Iceberg table

```sql
ALTER TABLE table_name SET TBLPROPERTIES('iceberg.dataSkippingStatsColumns' = 'col1, col2, col3')
```

Updating these properties doesn't automatically recompute statistics for existing data. Rather, it impacts the behavior of future statistics collection when adding or updating data in the table. Statistics aren't used for columns not included in the current list of statistics columns.

In Databricks Runtime 14.3 LTS and above, if you have altered the table properties or changed the specified columns for statistics, you can manually trigger the recomputation of statistics for a table using the following command:

```sql
ANALYZE TABLE table_name COMPUTE DELTA STATISTICS
```

> [!NOTE]
>
> Long strings are truncated during statistics collection. You might choose to exclude long string columns from statistics collection, especially if the columns aren't used frequently for filtering queries.

## <a id="delta-zorder" />What is Z-ordering?

> [!NOTE]
>
> Databricks recommends using liquid clustering for all new tables. You can't use `ZORDER` in combination with liquid clustering. See [Use liquid clustering for tables](https://docs.databricks.com/aws/en/tables/clustering.md).

Z-ordering is a [technique](https://en.wikipedia.org/wiki/Z-order_curve) to colocate related information in the same set of files. Databricks data-skipping algorithms automatically use this co-locality. This behavior reduces the amount of data that needs to be read. To Z-order data, specify the columns to order on in the `ZORDER BY` clause:

```sql
OPTIMIZE events
WHERE date >= current_timestamp() - INTERVAL 1 day
ZORDER BY (eventType)
```

If you expect a column to be commonly used in query predicates and if that column has high cardinality (that is, a large number of distinct values), then use `ZORDER BY`.

You can specify multiple columns for `ZORDER BY` as a comma-separated list. However, the effectiveness drops with each extra column.

Databricks recommends that you don't use `ZORDER BY` on columns that don't have statistics collected because it is ineffective and uses unnecessary compute resources. Data skipping requires column-local stats such as min, max, and count. You can configure statistics collection on certain columns by reordering columns in the schema, or you can increase the number of columns to collect statistics on.

> [!NOTE]
>
> * Z-ordering is *not idempotent* but aims to be an incremental operation. The time it takes for Z-ordering isn't guaranteed to reduce over multiple runs. However, if no new data was added to a partition that was just Z-ordered, another Z-ordering of that partition doesn't have any effect.
> * Z-ordering aims to produce evenly-balanced data files with respect to the number of tuples, but not necessarily data size in storage. Though file sizes and number of tuples are correlated, there might be situations when they aren't, which skews optimize task times.
>
>   For example, if you `ZORDER BY` *date* and your most recent records are all much wider (such as longer arrays or string values) than the ones in the past, `OPTIMIZE` job's task durations and resulting file sizes might be skewed. This is, however, only a problem for the `OPTIMIZE` command itself; it likely doesn't have any negative effects on subsequent queries.
