(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to create Z-order indexes in Lakehouse: step by step

João Barros 26 de November de 2026 4 min read

This tutorial shows how to create and use Z-order indexes in Lakehouse to improve the performance of analytical queries over Delta tables. It explains why Z-order, when it makes sense, and presents a practical example with commands and best practices to avoid common mistakes.

Prerequisites

  • Account and workspace with Lakehouse (for example Azure Synapse/Databricks/Power BI Fabric) with support for Delta tables.
  • Permissions to read/write Delta tables and run SQL/PySpark commands.
  • A Delta table with a large volume of data and columns frequently used in filters/ranges (e.g.: event_date, customer_id, region).
  • Basic familiarity with SQL and Delta maintenance commands (OPTIMIZE/REWRITE).

Step 1: Understand why to use Z-order

Z-order is a multidimensional ordering technique that improves data locality within files, reducing I/O for queries that filter by multiple columns. In Lakehouse, using Z-order on columns frequently used in WHERE or JOIN can speed up queries and reduce read costs. It does not replace partitioning: Z-order complements partitions when there are many distinct values.

Step 2: Choose the right columns for Z-order

Select columns that frequently appear in combined filters (e.g.: event_date + customer_id). Avoid using columns with extremely high cardinality if the query only filters by a single column. Also plan the order: the first column should be the most selective in typical queries.

Step 3: Check statistics and files before optimizing

Before creating Z-order, check the number of files and the average size. This helps decide whether to first compact files (OPTIMIZE/REWRITE) to avoid working over many small files.

-- Exemplo SQL para ver número de ficheiros (Databricks/SQL):
DESCRIBE DETAIL delta.`/caminho/para/tabela`;
-- Ou ver ficheiros no storage e tamanho médio por partição

Step 4: Compact files (when applicable)

If there are many small files, compact them before applying Z-order. In Databricks/Delta Lake use OPTIMIZE; in environments without OPTIMIZE, use a REWRITE/INSERT OVERWRITE with a coalesce.

-- Databricks SQL exemplo:
OPTIMIZE nome_schema.nome_tabela WHERE event_date >= '2026-01-01';

-- Alternativa PySpark para compactar (quando não existe OPTIMIZE):
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
df = spark.read.format("delta").load("/caminho/para/tabela")
df.repartition(200).write.format("delta").mode("overwrite").option("replaceWhere","event_date >= '2026-01-01'").save("/caminho/para/tabela")

Step 5: Apply Z-order

Applying Z-order organizes files physically according to the Z-order. In Databricks/Delta Lake use CLUSTER BY / OPTIMIZE ... ZORDER BY. Run it when data is stable (not during heavy writes).

-- Databricks SQL exemplo:
OPTIMIZE nome_schema.nome_tabela ZORDER BY (event_date, customer_id);

-- Alternativa PySpark (quando OPTIMIZE não está disponível):
from delta.tables import DeltaTable
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
df = spark.read.format("delta").load("/caminho/para/tabela")
# Reordenar por colunas e reescrever
df.orderBy("event_date","customer_id")\
  .repartition(200)\
  .write.format("delta")\
  .mode("overwrite")\
  .save("/caminho/para/tabela")

Step 6: Avoid common mistakes

Common mistakes: running Z-order on tables under constant write (leads to fragmentation), choosing low-selectivity columns, not compacting files beforehand (little effect), and expecting immediate gains on queries that do not filter by the Z-order columns. Always test in a staging environment and measure before/after.

Verify the result

Confirm improvements by comparing execution plans and bytes read before and after. Use EXPLAIN/PROFILE and measure time and bytes read. Also check the number and size of files and whether the physical order changed (Delta metadata).

-- Exemplo SQL para medir antes/depois:
EXPLAIN SELECT * FROM nome_schema.nome_tabela WHERE event_date = '2026-02-01' AND customer_id = 'C123';
-- Ou usar a ferramenta de profiling do workspace para ver bytes lidos e tempo

Conclusion

Z-order in Lakehouse is a practical technique to accelerate multidimensional queries on Delta tables when combined with partitioning and compaction. Next steps: test different combinations of Z-order columns, automate OPTIMIZE/ZORDER in ETL pipelines and monitor the gain. Tip: start with a representative dataset to avoid unnecessary costs.