Documentation Index
Fetch the complete documentation index at: https://private-7c7dfe99-page-updates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Updating and deleting ClickHouse data with mutations
Although ClickHouse is geared toward high volume analytic workloads, it is possible in some situations to modify or delete existing data. These operations are labeled “mutations” and are executed using theALTER TABLE command.
Updating data
Use theALTER TABLE...UPDATE command to update rows in a table:
<expression> is the new value for the column where the <filter_expr> is satisfied. The <expression> must be the same datatype as the column or be convertible to the same datatype using the CAST operator. The <filter_expr> should return a UInt8 (zero or non-zero) value for each row of the data. Multiple UPDATE <column> statements can be combined in a single ALTER TABLE command separated by commas.
Examples:
-
A mutation like this allows updating replacing
visitor_idswith new ones using a dictionary lookup: -
Modifying multiple values in one command can be more efficient than multiple commands:
-
Mutations can be executed
ON CLUSTERfor sharded tables:
It isn’t possible to update columns that are part of the primary or sorting key.
Deleting data
Use theALTER TABLE command to delete rows:
<filter_expr> should return a UInt8 value for each row of data.
Examples
-
Delete any records where a column is in an array of values:
-
What does this query alter?
To delete all of the data in a table, it is more efficient to use the command
TRUNCATE TABLE [<database].]<table> command. This command can also be executed ON CLUSTER.DELETE statement docs page for more details.
Lightweight deletes
Another option for deleting rows is to use theDELETE FROM command, which is referred to as a lightweight delete. The deleted rows are marked as deleted immediately and will be automatically filtered out of all subsequent queries, so you don’t have to wait for a merging of parts or use the FINAL keyword. Cleanup of data happens asynchronously in the background.
hits table where the Title column contains the text hello:
- This feature is only available for the
MergeTreetable engine family. - Lightweight deletes are synchronous by default, waiting for all replicas to process the delete. The behavior is controlled by the
lightweight_deletes_syncsetting.