Hubert-Dudek
Databricks MVP

ZORDER BY

Colocate column information in the same set of files. Co-locality is used by Delta Lake data-skipping algorithms to dramatically reduce the amount of data that needs to be read. You can specify multiple columns for ZORDER BY as a comma-separated list. However, the effectiveness of the locality drops with each additional column.

it is from https://docs.databricks.com/spark/latest/spark-sql/language-manual/delta-optimize.html

So for delta files once partition disappear it is really important to have Z-order as it will handle effectively your query, so you need:

OPTIMIZE data ZORDER BY (userid, date)


My blog: https://databrickster.medium.com/