FROM clause of a query reads from the remote or remoteSecure table function, ClickHouse sends the query to the remote server together with its IN subqueries and joined tables, and the remote server executes them. A table that exists only on the server where you start the query is unknown to the remote server, so the query fails with UNKNOWN_DATABASE or UNKNOWN_TABLE. Write GLOBAL IN or GLOBAL JOIN instead: the local server then runs the subquery itself and sends its result to the remote server together with the query.
Example
The remote server has a table of orders:Received from shows that the remote server raised the error: it received the subquery as part of the query and has no database crm. If the remote server has a database with that name but not the table, the error is UNKNOWN_TABLE (Unknown table expression identifier 'crm.vip_customers'). A regular JOIN with crm.vip_customers fails in the same way.
Use GLOBAL IN
Use GLOBAL JOIN
INNER or LEFT join: for these join kinds the right side is the one that the local server calculates and sends.
How GLOBAL works and what it costs
With GLOBAL IN or GLOBAL JOIN, the server that received your query first runs the subquery (or reads the joined table), puts the result into a temporary table in memory, and sends this temporary table to the remote server with the query. The remote server uses the transmitted data in place of the table it does not have.
The data travels over the network with every query, so keep it small:
- Select only the columns you need and filter the rows in the subquery.
- The result is not deduplicated. Add
DISTINCTto the subquery of aGLOBAL INwhen the column has many repeated values. max_rows_in_setandmax_bytes_in_setlimit how much data aGLOBAL INsubquery may produce.
IN or JOIN.
Related settings
prefer_global_in_and_joinset to1replacesINandJOINwithGLOBAL INandGLOBAL JOINin such queries. Use it when you cannot change the query text.distributed_product_modedoes not help here. It controls subqueries that readDistributedtables and is not used for theremotetable function.
Distributed table: a table used in a regular IN subquery or JOIN must exist on every shard.