2020-11-28 15:49:52 +00:00
---
2022-04-09 13:29:05 +00:00
sidebar_label: MaterializedMySQL
sidebar_position: 70
2020-11-28 15:49:52 +00:00
---
2022-04-09 13:29:05 +00:00
# [experimental] MaterializedMySQL
2020-11-28 15:49:52 +00:00
2022-04-09 13:29:05 +00:00
:::warning
This is an experimental feature that should not be used in production.
:::
2021-07-24 12:11:23 +00:00
2022-04-09 13:29:05 +00:00
Creates a ClickHouse database with all the tables existing in MySQL, and all the data in those tables. The ClickHouse server works as MySQL replica. It reads `binlog` and performs DDL and DML queries.
2020-11-28 15:49:52 +00:00
## Creating a Database {#creating-a-database}
``` sql
CREATE DATABASE [IF NOT EXISTS] db_name [ON CLUSTER cluster]
2021-07-26 18:17:28 +00:00
ENGINE = MaterializedMySQL('host:port', ['database' | database], 'user', 'password') [SETTINGS ...]
2021-12-10 10:44:16 +00:00
[TABLE OVERRIDE table1 (...), TABLE OVERRIDE table2 (...)]
2020-11-28 15:49:52 +00:00
```
**Engine Parameters**
2020-11-30 16:09:07 +00:00
- `host:port` — MySQL server endpoint.
2020-12-05 00:53:27 +00:00
- `database` — MySQL database name.
2020-11-28 15:49:52 +00:00
- `user` — MySQL user.
- `password` — User password.
2021-07-24 09:36:41 +00:00
**Engine Settings**
2021-08-06 08:29:03 +00:00
2021-08-07 07:36:38 +00:00
- `max_rows_in_buffer` — Maximum number of rows that data is allowed to cache in memory (for single table and the cache data unable to query). When this number is exceeded, the data will be materialized. Default: `65 505` .
- `max_bytes_in_buffer` — Maximum number of bytes that data is allowed to cache in memory (for single table and the cache data unable to query). When this number is exceeded, the data will be materialized. Default: `1 048 576` .
- `max_flush_data_time` — Maximum number of milliseconds that data is allowed to cache in memory (for database and the cache data unable to query). When this time is exceeded, the data will be materialized. Default: `1000` .
2021-08-07 07:38:34 +00:00
- `max_wait_time_when_mysql_unavailable` — Retry interval when MySQL is not available (milliseconds). Negative value disables retry. Default: `1000` .
2021-08-07 07:36:48 +00:00
- `allows_query_when_mysql_lost` — Allows to query a materialized table when MySQL is lost. Default: `0` (`false`).
2022-03-01 02:21:58 +00:00
- `materialized_mysql_tables_list` — a comma-separated list of mysql database tables, which will be replicated by MaterializedMySQL database engine. Default value: empty list — means whole tables will be replicated.
2021-08-06 08:29:03 +00:00
```sql
2021-07-29 15:20:55 +00:00
CREATE DATABASE mysql ENGINE = MaterializedMySQL('localhost:3306', 'db', 'user', '***')
SETTINGS
2021-07-24 09:36:41 +00:00
allows_query_when_mysql_lost=true,
max_wait_time_when_mysql_unavailable=10000;
```
2021-08-07 07:36:52 +00:00
**Settings on MySQL-server Side**
2021-07-25 22:31:34 +00:00
2021-08-12 06:46:16 +00:00
For the correct work of `MaterializedMySQL` , there are few mandatory `MySQL` -side configuration settings that must be set:
2021-07-25 22:31:34 +00:00
2021-08-12 06:51:05 +00:00
- `default_authentication_plugin = mysql_native_password` since `MaterializedMySQL` can only authorize with this method.
2021-08-12 06:51:22 +00:00
- `gtid_mode = on` since GTID based logging is a mandatory for providing correct `MaterializedMySQL` replication.
2021-08-03 13:47:16 +00:00
2022-04-09 13:29:05 +00:00
:::note
While turning on `gtid_mode` you should also specify `enforce_gtid_consistency = on` .
:::
2021-07-25 22:31:34 +00:00
2021-08-06 08:43:46 +00:00
## Virtual Columns {#virtual-columns}
2020-11-28 15:49:52 +00:00
2021-07-26 18:17:28 +00:00
When working with the `MaterializedMySQL` database engine, [ReplacingMergeTree ](../../engines/table-engines/mergetree-family/replacingmergetree.md ) tables are used with virtual `_sign` and `_version` columns.
2021-07-29 15:20:55 +00:00
2021-01-18 13:43:00 +00:00
- `_version` — Transaction counter. Type [UInt64 ](../../sql-reference/data-types/int-uint.md ).
- `_sign` — Deletion mark. Type [Int8 ](../../sql-reference/data-types/int-uint.md ). Possible values:
2021-07-29 15:20:55 +00:00
- `1` — Row is not deleted,
2021-01-18 13:43:00 +00:00
- `-1` — Row is deleted.
2020-11-28 15:49:52 +00:00
## Data Types Support {#data_types-support}
2020-12-05 00:53:27 +00:00
| MySQL | ClickHouse |
|-------------------------|--------------------------------------------------------------|
| TINY | [Int8 ](../../sql-reference/data-types/int-uint.md ) |
| SHORT | [Int16 ](../../sql-reference/data-types/int-uint.md ) |
| INT24 | [Int32 ](../../sql-reference/data-types/int-uint.md ) |
| LONG | [UInt32 ](../../sql-reference/data-types/int-uint.md ) |
| LONGLONG | [UInt64 ](../../sql-reference/data-types/int-uint.md ) |
| FLOAT | [Float32 ](../../sql-reference/data-types/float.md ) |
| DOUBLE | [Float64 ](../../sql-reference/data-types/float.md ) |
| DECIMAL, NEWDECIMAL | [Decimal ](../../sql-reference/data-types/decimal.md ) |
2022-04-09 13:29:05 +00:00
| DATE, NEWDATE | [Date ](../../sql-reference/data-types/date.md ) |
2020-12-05 00:53:27 +00:00
| DATETIME, TIMESTAMP | [DateTime ](../../sql-reference/data-types/datetime.md ) |
| DATETIME2, TIMESTAMP2 | [DateTime64 ](../../sql-reference/data-types/datetime64.md ) |
2022-01-21 03:37:34 +00:00
| YEAR | [UInt16 ](../../sql-reference/data-types/int-uint.md ) |
| TIME | [Int64 ](../../sql-reference/data-types/int-uint.md ) |
2021-06-25 11:27:03 +00:00
| ENUM | [Enum ](../../sql-reference/data-types/enum.md ) |
2020-12-05 00:53:27 +00:00
| STRING | [String ](../../sql-reference/data-types/string.md ) |
| VARCHAR, VAR_STRING | [String ](../../sql-reference/data-types/string.md ) |
| BLOB | [String ](../../sql-reference/data-types/string.md ) |
2022-01-21 03:37:34 +00:00
| GEOMETRY | [String ](../../sql-reference/data-types/string.md ) |
2021-07-25 22:31:34 +00:00
| BINARY | [FixedString ](../../sql-reference/data-types/fixedstring.md ) |
2021-12-17 06:48:49 +00:00
| BIT | [UInt64 ](../../sql-reference/data-types/int-uint.md ) |
2022-01-05 12:34:15 +00:00
| SET | [UInt64 ](../../sql-reference/data-types/int-uint.md ) |
2020-12-05 00:53:27 +00:00
2020-11-28 15:49:52 +00:00
[Nullable ](../../sql-reference/data-types/nullable.md ) is supported.
2022-01-21 03:37:34 +00:00
The data of TIME type in MySQL is converted to microseconds in ClickHouse.
2021-08-06 08:29:03 +00:00
Other types are not supported. If MySQL table contains a column of such type, ClickHouse throws exception "Unhandled data type" and stops replication.
2020-11-30 16:09:07 +00:00
## Specifics and Recommendations {#specifics-and-recommendations}
2020-11-28 15:49:52 +00:00
2021-08-06 08:43:46 +00:00
### Compatibility Restrictions {#compatibility-restrictions}
2021-07-25 22:31:34 +00:00
Apart of the data types limitations there are few restrictions comparing to `MySQL` databases, that should be resolved before replication will be possible:
- Each table in `MySQL` should contain `PRIMARY KEY` .
- Replication for tables, those are containing rows with `ENUM` field values out of range (specified in `ENUM` signature) will not work.
2020-12-20 16:38:40 +00:00
### DDL Queries {#ddl-queries}
2020-11-28 15:49:52 +00:00
2020-12-05 00:53:27 +00:00
MySQL DDL queries are converted into the corresponding ClickHouse DDL queries ([ALTER](../../sql-reference/statements/alter/index.md), [CREATE ](../../sql-reference/statements/create/index.md ), [DROP ](../../sql-reference/statements/drop.md ), [RENAME ](../../sql-reference/statements/rename.md )). If ClickHouse cannot parse some DDL query, the query is ignored.
2020-11-28 15:49:52 +00:00
2021-01-18 13:43:00 +00:00
### Data Replication {#data-replication}
2020-11-28 15:49:52 +00:00
2021-07-26 18:17:28 +00:00
`MaterializedMySQL` does not support direct `INSERT` , `DELETE` and `UPDATE` queries. However, they are supported in terms of data replication:
2020-11-28 15:49:52 +00:00
2020-12-20 16:38:40 +00:00
- MySQL `INSERT` query is converted into `INSERT` with `_sign=1` .
2020-11-28 15:49:52 +00:00
2021-07-29 15:20:55 +00:00
- MySQL `DELETE` query is converted into `INSERT` with `_sign=-1` .
2020-11-28 15:49:52 +00:00
2021-12-10 10:44:16 +00:00
- MySQL `UPDATE` query is converted into `INSERT` with `_sign=-1` and `INSERT` with `_sign=1` if the primary key has been changed, or
`INSERT` with `_sign=1` if not.
2020-12-20 16:38:40 +00:00
2021-07-26 18:17:28 +00:00
### Selecting from MaterializedMySQL Tables {#select}
2020-12-20 16:38:40 +00:00
2021-07-26 18:17:28 +00:00
`SELECT` query from `MaterializedMySQL` tables has some specifics:
2020-11-28 15:49:52 +00:00
2021-12-10 10:44:16 +00:00
- If `_version` is not specified in the `SELECT` query, the
[FINAL ](../../sql-reference/statements/select/from.md#select-from-final ) modifier is used, so only rows with
`MAX(_version)` are returned for each primary key value.
2020-11-28 15:49:52 +00:00
2021-12-10 10:44:16 +00:00
- If `_sign` is not specified in the `SELECT` query, `WHERE _sign=1` is used by default. So the deleted rows are not
included into the result set.
2020-11-28 15:49:52 +00:00
2021-07-19 07:10:18 +00:00
- The result includes columns comments in case they exist in MySQL database tables.
2021-07-16 16:13:09 +00:00
2020-12-20 16:38:40 +00:00
### Index Conversion {#index-conversion}
2020-11-28 15:49:52 +00:00
MySQL `PRIMARY KEY` and `INDEX` clauses are converted into `ORDER BY` tuples in ClickHouse tables.
2021-12-10 10:44:16 +00:00
ClickHouse has only one physical order, which is determined by `ORDER BY` clause. To create a new physical order, use
[materialized views ](../../sql-reference/statements/create/view.md#materialized ).
2020-11-28 15:49:52 +00:00
2021-01-18 13:43:00 +00:00
**Notes**
2020-11-28 15:49:52 +00:00
2021-01-18 13:43:00 +00:00
- Rows with `_sign=-1` are not deleted physically from the tables.
2021-12-10 10:44:16 +00:00
- Cascade `UPDATE/DELETE` queries are not supported by the `MaterializedMySQL` engine, as they are not visible in the
MySQL binlog.
2021-01-18 13:43:00 +00:00
- Replication can be easily broken.
- Manual operations on database and tables are forbidden.
2021-12-10 10:44:16 +00:00
- `MaterializedMySQL` is affected by the [optimize_on_insert ](../../operations/settings/settings.md#optimize-on-insert )
setting. Data is merged in the corresponding table in the `MaterializedMySQL` database when a table in the MySQL
server changes.
### Table Overrides {#table-overrides}
Table overrides can be used to customize the ClickHouse DDL queries, allowing you to make schema optimizations for your
application. This is especially useful for controlling partitioning, which is important for the overall performance of
MaterializedMySQL.
2021-12-21 18:04:38 +00:00
These are the schema conversion manipulations you can do with table overrides for MaterializedMySQL:
* Modify column type. Must be compatible with the original type, or replication will fail. For example,
you can modify a UInt32 column to UInt64, but you can not modify a String column to Array(String).
* Modify [column TTL ](../table-engines/mergetree-family/mergetree/#mergetree-column-ttl ).
* Modify [column compression codec ](../../sql-reference/statements/create/table/#codecs ).
* Add [ALIAS columns ](../../sql-reference/statements/create/table/#alias ).
* Add [skipping indexes ](../table-engines/mergetree-family/mergetree/#table_engine-mergetree-data_skipping-indexes )
* Add [projections ](../table-engines/mergetree-family/mergetree/#projections ). Note that projection optimizations are
disabled when using `SELECT ... FINAL` (which MaterializedMySQL does by default), so their utility is limited here.
`INDEX ... TYPE hypothesis` as [described in the v21.12 blog post]](https://clickhouse.com/blog/en/2021/clickhouse-v21.12-released/)
may be more useful in this case.
* Modify [PARTITION BY ](../table-engines/mergetree-family/custom-partitioning-key/ )
* Modify [ORDER BY ](../table-engines/mergetree-family/mergetree/#mergetree-query-clauses )
* Modify [PRIMARY KEY ](../table-engines/mergetree-family/mergetree/#mergetree-query-clauses )
* Add [SAMPLE BY ](../table-engines/mergetree-family/mergetree/#mergetree-query-clauses )
* Add [table TTL ](../table-engines/mergetree-family/mergetree/#mergetree-query-clauses )
2021-12-10 10:44:16 +00:00
```sql
CREATE DATABASE db_name ENGINE = MaterializedMySQL(...)
[SETTINGS ...]
[TABLE OVERRIDE table_name (
[COLUMNS (
2021-12-21 18:04:38 +00:00
[col_name [datatype] [ALIAS expr] [CODEC(...)] [TTL expr], ...]
[INDEX index_name expr TYPE indextype[(...)] GRANULARITY val, ...]
[PROJECTION projection_name (SELECT < COLUMN LIST EXPR > [GROUP BY] [ORDER BY]), ...]
)]
[ORDER BY expr]
[PRIMARY KEY expr]
[PARTITION BY expr]
[SAMPLE BY expr]
[TTL expr]
2021-12-10 10:44:16 +00:00
), ...]
```
Example:
```sql
CREATE DATABASE db_name ENGINE = MaterializedMySQL(...)
TABLE OVERRIDE table1 (
COLUMNS (
2021-12-21 18:04:38 +00:00
userid UUID,
category LowCardinality(String),
timestamp DateTime CODEC(Delta, Default)
2021-12-10 10:44:16 +00:00
)
PARTITION BY toYear(timestamp)
),
TABLE OVERRIDE table2 (
COLUMNS (
2021-12-21 18:04:38 +00:00
client_ip String TTL created + INTERVAL 72 HOUR
)
SAMPLE BY ip_hash
2021-12-10 10:44:16 +00:00
)
```
2021-12-21 18:04:38 +00:00
The `COLUMNS` list is sparse; existing columns are modified as specified, extra ALIAS columns are added. It is not
possible to add ordinary or MATERIALIZED columns. Modified columns with a different type must be assignable from the
original type. There is currently no validation of this or similar issues when the `CREATE DATABASE` query executes, so
extra care needs to be taken.
2021-12-10 10:44:16 +00:00
You may specify overrides for tables that do not exist yet.
2020-11-28 15:49:52 +00:00
2022-04-09 13:29:05 +00:00
:::warning
It is easy to break replication with table overrides if not used with care. For example:
2021-12-13 10:35:41 +00:00
2022-04-09 13:29:05 +00:00
* If an ALIAS column is added with a table override, and a column with the same name is later added to the source
MySQL table, the converted ALTER TABLE query in ClickHouse will fail and replication stops.
* It is currently possible to add overrides that reference nullable columns where not-nullable are required, such as in
`ORDER BY` or `PARTITION BY` . This will cause CREATE TABLE queries that will fail, also causing replication to stop.
:::
2021-12-13 10:35:41 +00:00
2020-11-28 15:49:52 +00:00
## Examples of Use {#examples-of-use}
2020-12-20 16:38:40 +00:00
Queries in MySQL:
2020-11-28 15:49:52 +00:00
2020-12-20 16:38:40 +00:00
``` sql
2020-11-28 15:49:52 +00:00
mysql> CREATE DATABASE db;
mysql> CREATE TABLE db.test (a INT PRIMARY KEY, b INT);
mysql> INSERT INTO db.test VALUES (1, 11), (2, 22);
mysql> DELETE FROM db.test WHERE a=1;
mysql> ALTER TABLE db.test ADD COLUMN c VARCHAR(16);
mysql> UPDATE db.test SET c='Wow!', b=222;
mysql> SELECT * FROM test;
2020-12-20 16:38:40 +00:00
```
2021-01-18 13:43:00 +00:00
2020-12-20 16:38:40 +00:00
```text
2021-12-21 18:04:38 +00:00
┌─a─┬───b─┬─c────┐
│ 2 │ 222 │ Wow! │
└───┴─────┴──────┘
2020-11-28 15:49:52 +00:00
```
Database in ClickHouse, exchanging data with the MySQL server:
2020-12-20 16:38:40 +00:00
The database and the table created:
2020-11-28 15:49:52 +00:00
``` sql
2021-07-26 18:17:28 +00:00
CREATE DATABASE mysql ENGINE = MaterializedMySQL('localhost:3306', 'db', 'user', '***');
2020-11-28 15:49:52 +00:00
SHOW TABLES FROM mysql;
```
``` text
┌─name─┐
│ test │
└──────┘
```
2020-12-20 16:38:40 +00:00
After inserting data:
2020-11-28 15:49:52 +00:00
``` sql
SELECT * FROM mysql.test;
```
``` text
2021-07-29 15:20:55 +00:00
┌─a─┬──b─┐
│ 1 │ 11 │
│ 2 │ 22 │
2020-11-28 15:49:52 +00:00
└───┴────┘
```
2020-12-20 16:38:40 +00:00
After deleting data, adding the column and updating:
2020-11-28 15:49:52 +00:00
``` sql
SELECT * FROM mysql.test;
```
``` text
2021-07-29 15:20:55 +00:00
┌─a─┬───b─┬─c────┐
│ 2 │ 222 │ Wow! │
2020-11-28 15:49:52 +00:00
└───┴─────┴──────┘
```
2021-09-19 20:05:54 +00:00
[Original article ](https://clickhouse.com/docs/en/engines/database-engines/materialized-mysql/ ) <!--hide-->