Write ordering controls the physical layout of data within table files. Unlike SORT BY which orders data during query execution, write ordering is applied at write time and persists in the stored files.
Write ordering is supported for Iceberg tables and can be specified during table creation.
Hive supports two write ordering strategies:
Introduced in Hive version 4.1.0
CREATE TABLE table_name (column_definitions) WRITE [LOCALLY] ORDERED BY column_name [ASC | DESC] [NULLS FIRST | NULLS LAST] [, column_name [ASC | DESC] [NULLS FIRST | NULLS LAST] ]* STORED BY ICEBERG [STORED AS file_format];
ASC: Ascending order (default)DESC: Descending orderNULLS FIRST: Null values sorted before non-null valuesNULLS LAST: Null values sorted after non-null valuesSingle column:
CREATE TABLE events ( event_id BIGINT, event_date DATE, event_type STRING ) WRITE LOCALLY ORDERED BY event_date DESC STORED BY ICEBERG STORED AS ORC;
Multiple columns with null handling:
CREATE TABLE orders ( order_id BIGINT, order_date DATE, customer_id INT, amount DECIMAL(10,2) ) WRITE ORDERED BY order_date DESC NULLS FIRST, order_id ASC STORED BY ICEBERG;
Type-Native ordering is most effective for:
Introduced in Hive version 4.2.0
Z-order applies a multi-dimensional clustering technique based on space-filling curves. This approach interleaves column values to co-locate related records across multiple dimensions, enabling efficient filtering on various column combinations.
CREATE TABLE table_name (column_definitions) WRITE [LOCALLY] ORDERED BY ZORDER(column_name [, column_name ]*) STORED BY ICEBERG [STORED AS file_format];
CREATE TABLE user_events ( user_id INT, event_date DATE, event_type STRING, value DOUBLE ) WRITE LOCALLY ORDERED BY ZORDER(user_id, event_date) STORED BY ICEBERG STORED AS ORC;
Z-ordering can alternatively be specified using table properties.
CREATE TABLE table_name (column_definitions) STORED BY ICEBERG TBLPROPERTIES ( 'sort.order' = 'zorder', 'sort.columns' = 'column1,column2' );
Z-order is most effective for:
| Feature | WRITE ORDERED BY | SORT BY |
|---|---|---|
| Application | Write time | Query time |
| Persistence | Permanent in files | Query result only |
| Scope | Physical file layout | Query execution |
| Table Support | Iceberg tables | All table types |