SELECT and INSERT queries to be performed on data that are stored on a remote MySQL server.
Syntax
Arguments
Arguments also can be passed using named collections. In this case
host and port should be specified separately. This approach is recommended for production environment.
Simple WHERE clauses such as =, !=, >, >=, <, <= are currently executed on the MySQL server.
The rest of the conditions and the LIMIT sampling constraint are executed in ClickHouse only after the query to MySQL finishes.
Passing a query instead of a table name
Instead of a table name, the third argument can be aSELECT query that is passed to MySQL as is. The structure of the resulting table is inferred from the query result. The query can be written either as a subquery, or wrapped into the query function:
INSERT into it is not allowed. The same syntax is supported by the MySQL table engine.
The subquery form
(SELECT ...) is parsed by ClickHouse and re-serialized in the MySQL dialect (backtick identifier quoting) before being sent to the server. It must therefore be valid ClickHouse SQL. To pass MySQL-specific syntax that ClickHouse does not parse, use the query('...') form, whose text is sent to MySQL verbatim.Any outer WHERE, LIMIT, aggregation, etc. of the surrounding ClickHouse query is not pushed down into the passed query — it is applied in ClickHouse after the full query result is fetched. To restrict the data read from MySQL, put the filter inside the passed query. With external_table_strict_query = 1 an outer filter that cannot be pushed down is rejected with an exception instead of being applied locally.|. For example:
Returned value
A table object with the same columns as the original MySQL table.Some data types of MySQL can be mapped to different ClickHouse types - this is addressed by query-level setting mysql_datatypes_support_level
In the
INSERT query to distinguish table function mysql(...) from table name with column names list, you must use keywords FUNCTION or TABLE FUNCTION. See examples below.Examples
Table in MySQL:enable_compression
Enables compression for the MySQL protocol connection.
Default value: false.
This setting applies to:
- the
mysqltable function; - the
MySQLtable engine; - the
MySQLdatabase engine; - named collections used by MySQL integrations.