Skip to main content
When the 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:
The local server, where you run the queries, has a list of customers that the remote server does not have:
Filtering the remote table by the local one fails:
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

Keep the local table on the right side of an 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 DISTINCT to the subquery of a GLOBAL IN when the column has many repeated values.
  • max_rows_in_set and max_bytes_in_set limit how much data a GLOBAL IN subquery may produce.
If the table is large and you need it often, create a copy of it on the remote server and use a regular IN or JOIN.
  • prefer_global_in_and_join set to 1 replaces IN and JOIN with GLOBAL IN and GLOBAL JOIN in such queries. Use it when you cannot change the query text.
  • distributed_product_mode does not help here. It controls subqueries that read Distributed tables and is not used for the remote table function.
The same rules apply to queries that read from a Distributed table: a table used in a regular IN subquery or JOIN must exist on every shard.