Skip to content
ClickHouse Docs

ALTER TABLE ... MODIFY ORDER BY

Autogenerated from ClickHouse system tables

ALTER TABLE [db].name [ON CLUSTER cluster] MODIFY ORDER BY new_expression

The command changes the sorting key of the table to new_expression (an expression or a tuple of expressions). Primary key remains the same.

The command is lightweight in a sense that it only changes metadata. To keep the property that data part rows are ordered by the sorting key expression you cannot add expressions containing existing columns to the sorting key (only columns added by the ADD COLUMN command in the same ALTER query, without default column value).

For the same reason you cannot change the sort direction (ASC or DESC) of a sorting key column that new_expression keeps: the existing parts stay physically sorted in the direction the column had when they were written, and no regular data part records that direction. To use a different direction, create a new table with the desired ORDER BY and copy the data into it with INSERT ... SELECT.

Part of: ALTER

Related: ALTER, CREATE TABLE, ALTER TABLE ... MODIFY SAMPLE BY