2020-04-03 13:23:32 +00:00
---
toc_priority: 33
toc_title: ReplacingMergeTree
---
2020-04-08 14:22:25 +00:00
# ReplacingMergeTree {#replacingmergetree}
2017-12-28 15:13:23 +00:00
2018-12-12 17:28:00 +00:00
The engine differs from [MergeTree ](mergetree.md#table_engines-mergetree ) in that it removes duplicate entries with the same primary key value (or more accurately, with the same [sorting key ](mergetree.md ) value).
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Data deduplication occurs only during a merge. Merging occurs in the background at an unknown time, so you can’ t plan for it. Some of the data may remain unprocessed. Although you can run an unscheduled merge using the `OPTIMIZE` query, don’ t count on using it, because the `OPTIMIZE` query will read and write a large amount of data.
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Thus, `ReplacingMergeTree` is suitable for clearing out duplicate data in the background in order to save space, but it doesn’ t guarantee the absence of duplicates.
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
## Creating a Table {#creating-a-table}
2018-10-19 11:25:22 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-10-19 11:25:22 +00:00
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) ENGINE = ReplacingMergeTree([ver])
[PARTITION BY expr]
[ORDER BY expr]
2019-08-04 19:50:26 +00:00
[PRIMARY KEY expr]
2018-10-19 11:25:22 +00:00
[SAMPLE BY expr]
[SETTINGS name=value, ...]
2017-12-28 15:13:23 +00:00
```
2020-04-03 13:23:32 +00:00
For a description of request parameters, see [request description ](../../../sql_reference/statements/create.md ).
2017-12-28 15:13:23 +00:00
2018-10-19 11:25:22 +00:00
**ReplacingMergeTree Parameters**
2020-03-21 04:11:51 +00:00
- `ver` — column with version. Type `UInt*` , `Date` or `DateTime` . Optional parameter.
2018-10-19 11:25:22 +00:00
2020-03-21 04:11:51 +00:00
When merging, `ReplacingMergeTree` from all the rows with the same primary key leaves only one:
2020-03-20 10:10:48 +00:00
2020-03-21 04:11:51 +00:00
- Last in the selection, if `ver` not set.
- With the maximum version, if `ver` specified.
2018-10-19 11:25:22 +00:00
**Query clauses**
2018-12-25 15:25:43 +00:00
When creating a `ReplacingMergeTree` table the same [clauses ](mergetree.md ) are required, as when creating a `MergeTree` table.
2018-10-19 11:25:22 +00:00
2020-03-20 10:10:48 +00:00
< details markdown = "1" >
< summary > Deprecated Method for Creating a Table< / summary >
2018-10-19 11:25:22 +00:00
2020-03-20 10:10:48 +00:00
!!! attention "Attention"
2018-10-19 11:25:22 +00:00
Do not use this method in new projects and, if possible, switch the old projects to the method described above.
2020-03-20 10:10:48 +00:00
``` sql
2018-10-19 11:25:22 +00:00
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) ENGINE [=] ReplacingMergeTree(date-column [, sampling_expression], (primary, key), index_granularity, [ver])
```
All of the parameters excepting `ver` have the same meaning as in `MergeTree` .
2017-12-28 15:13:23 +00:00
2020-03-21 04:11:51 +00:00
- `ver` - column with the version. Optional parameter. For a description, see the text above.
2020-03-20 10:10:48 +00:00
2018-10-19 11:25:22 +00:00
< / details >
2018-10-16 10:47:17 +00:00
2020-01-30 10:34:55 +00:00
[Original article ](https://clickhouse.tech/docs/en/operations/table_engines/replacingmergetree/ ) <!--hide-->