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
Theorders table has an empty region column. The region of each customer is in the customers table:
customers, keyed by the column you join on:
dictHas restricts the update to the rows that have a match:
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 theQUERY 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:
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,
dictGetreturns the default value of the attribute (an empty string or0). Without theWHERE dictHas(...)condition, the update overwrites the column in those rows with that default. To keep the current value instead, usedictGetOrDefault:UPDATE region = dictGetOrDefault('customers_dict', 'region', customer_id, region) WHERE 1. -
Key type and layout. For a
Stringkey or a key of several columns, useCOMPLEX_KEY_HASHED. A key of one column can be passed todictGetas is; a key of several columns is passed as a tuple, for exampledictGet('prices_dict', 'price', (sku, currency)). For a singleUInt64key, useHASHED. 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 = 1makes the statement wait. Otherwise, follow the progress insystem.mutationsand keep the dictionary untilis_doneis1. -
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_allocatedinsystem.dictionariesshows its size. WithLIFETIME(0)it is never refreshed: if you change the source table after the dictionary was loaded, runSYSTEM RELOAD DICTIONARY customers_dictbefore the update. -
Replicated tables.
dictGetanddictHasare non-deterministic functions, because the content of a dictionary can differ between servers. On aReplicatedMergeTreetable the statement fails unless you allow it withallow_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 withON CLUSTERor in aReplicateddatabase, 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
USERandPASSWORDin its source. See the note in ClickHouse dictionary source.
Alternatives
- A table with the
Joinengine and thejoinGetfunction work the same way:UPDATE region = joinGet('default.customers_join', 'region', customer_id). Write the name of theJointable with its database.joinGetis non-deterministic as well, so the note about replicated tables applies. - The lightweight
UPDATEstatement 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.