While reviewing our SQL transformations, I kept finding the same thing: an ORDER BY on the final SELECT of a job that writes a table. The intent is reasonable. Sorted data compresses better, and sorted files let query engines skip row groups using Parquet’s min/max statistics. But an ORDER BY in the query that produces a table doesn’t give you a sorted table, and it isn’t free. We removed every one of them, and none of our output tests changed.
What ORDER BY costs
A global ORDER BY in Spark is a range-partitioned sort. Spark first samples the data to choose range boundaries, then shuffles every row to the task that owns its range, then sorts within each task. For a large table that is a full extra shuffle: all the data serialized, written to local disk, sent over the network and read back, plus a sampling pass before it. Skewed sort keys make it worse, because one range ends up with far more rows than the others.
Why the table still isn’t sorted
Between the query and the files on disk, the write path usually reorders rows again:
- The writer’s own distribution. Iceberg asks Spark to distribute and order rows to suit the table before writing, for example hashing rows by partition so each partition is written by as few tasks as possible. That adds another shuffle after yours, and a shuffle doesn’t preserve order.
- Partitioned writes. When writing a partitioned table, Spark sorts rows by the partition columns within each task so it can write one partition at a time. Unless your sort key starts with the partition columns, that local sort rearranges your order.
- File-size control. Pipelines often add a
repartitionorcoalescebefore writing to control how many files a table gets. Ours did. Any of these reshuffles the data after your sort. - Table maintenance. Compaction rewrites small files into larger ones. Unless it’s told to sort, it doesn’t keep your order either.
And even when files do happen to come out sorted, nothing promises a reader that order. A query without its own ORDER BY returns rows in whatever order the engine reads them, and engines read files in parallel.
What to do instead
If the reason for sorting is query performance, ask the table format to sort at write time. Iceberg tables can declare a write order, which Iceberg applies after its own distribution step, so the files really are sorted:
ALTER TABLE analytics.events WRITE ORDERED BY event_name, user_id;
Compaction can sort too, which keeps the table sorted as small writes accumulate:
CALL catalog.system.rewrite_data_files(
table => 'analytics.events',
strategy => 'sort',
sort_order => 'event_name ASC, user_id ASC'
);
For plain Parquet tables, sort within each task instead of globally: SORT BY in Spark SQL or sortWithinPartitions in the DataFrame API. Each file comes out sorted, which is what min/max skipping needs, and there’s no global shuffle. File statistics work per file, so a total order across files buys you little.
And measure first. As far as I could tell, the ORDER BY clauses we removed were added in the hope that they’d help, not because a query was slow. If a query is slow, look at its plan and at which columns it filters on, then choose a sort order for the table based on that.
Leave a Reply