Skip to main content
ALTER TABLE ... UPDATE has no FROM or JOIN clause, and a subquery that refers to the row being updated is rejected with Correlated subqueries in mutation update are not supported. To set a column to values that are stored in another table, load that table into a dictionary and look the value up with dictGet in the update expression.

Example

The orders table has an empty region column. The region of each customer is in the customers table:
Create a dictionary on top of customers, keyed by the column you join on:
Update the column. dictHas restricts the update to the rows that have a match:
Drop the dictionary when the update has finished:

Values that come from a join of several tables

A dictionary can load the result of a query instead of a table. Put the join into the QUERY of the source and name each column of the result after the dictionary attribute it fills. In this example the manager of an order is found through customers and regions:
One ALTER TABLE ... UPDATE can assign several columns, separated by commas, so that the affected parts are rewritten once.

Things to know

  • Rows without a match. For a key that is not in the dictionary, dictGet returns the default value of the attribute (an empty string or 0). Without the WHERE dictHas(...) condition, the update overwrites the column in those rows with that default. To keep the current value instead, use dictGetOrDefault: UPDATE region = dictGetOrDefault('customers_dict', 'region', customer_id, region) WHERE 1.
  • Key type and layout. For a String key or a key of several columns, use COMPLEX_KEY_HASHED. A key of one column can be passed to dictGet as is; a key of several columns is passed as a tuple, for example dictGet('prices_dict', 'price', (sku, currency)). For a single UInt64 key, use HASHED. See passing keys to dictionary functions.
  • The update is a mutation. It rewrites the data parts that contain matching rows in the background, and by default the statement returns before that work is done. mutations_sync = 1 makes the statement wait. Otherwise, follow the progress in system.mutations and keep the dictionary until is_done is 1.
  • The dictionary is a copy in memory. It is loaded in full on first use, so the lookup table has to fit in memory; bytes_allocated in system.dictionaries shows its size. With LIFETIME(0) it is never refreshed: if you change the source table after the dictionary was loaded, run SYSTEM RELOAD DICTIONARY customers_dict before the update.
  • Replicated tables. dictGet and dictHas are non-deterministic functions, because the content of a dictionary can differ between servers. On a ReplicatedMergeTree table the statement fails unless you allow it with allow_nondeterministic_mutations = 1:
    With the setting enabled, it is your responsibility that the dictionary exists on every replica and holds the same data there. Create it with ON CLUSTER or in a Replicated database, and do not change the source table until the mutation has finished.
  • ClickHouse Cloud. A dictionary that reads from a ClickHouse table may need a USER and PASSWORD in its source. See the note in ClickHouse dictionary source.

Alternatives

  • A table with the Join engine and the joinGet function work the same way: UPDATE region = joinGet('default.customers_join', 'region', customer_id). Write the name of the Join table with its database. joinGet is non-deterministic as well, so the note about replicated tables applies.
  • The lightweight UPDATE statement accepts the same expression: UPDATE orders SET region = dictGet('customers_dict', 'region', customer_id) WHERE dictHas('customers_dict', customer_id). It is a beta feature and needs the table settings described on its page.