2020-03-20 10:10:48 +00:00
# AggregatingMergeTree {#aggregatingmergetree}
2017-12-28 15:13:23 +00:00
2018-12-12 17:28:00 +00:00
The engine inherits from [MergeTree ](mergetree.md#table_engines-mergetree ), altering the logic for data parts merging. ClickHouse replaces all rows with the same primary key (or more accurately, with the same [sorting key ](mergetree.md )) with a single row (within a one data part) that stores a combination of states of aggregate functions.
2018-10-19 11:25:22 +00:00
You can use `AggregatingMergeTree` tables for incremental data aggregation, including for aggregated materialized views.
2017-12-28 15:13:23 +00:00
2018-12-12 17:28:00 +00:00
The engine processes all columns with [AggregateFunction ](../../data_types/nested_data_structures/aggregatefunction.md ) type.
2018-03-25 02:04:22 +00:00
2018-10-19 11:25:22 +00:00
It is appropriate to use `AggregatingMergeTree` if it reduces the number of rows by orders.
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
## Creating a Table {#creating-a-table}
2017-12-28 15:13:23 +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]
2017-12-28 15:13:23 +00:00
(
2018-10-19 11:25:22 +00:00
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) ENGINE = AggregatingMergeTree()
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
2019-08-01 20:33:26 +00:00
[TTL expr]
2018-10-19 11:25:22 +00:00
[SETTINGS name=value, ...]
2017-12-28 15:13:23 +00:00
```
2018-12-12 17:28:00 +00:00
For a description of request parameters, see [request description ](../../query_language/create.md ).
2017-12-28 15:13:23 +00:00
2018-10-19 11:25:22 +00:00
**Query clauses**
2018-03-25 02:04:22 +00:00
2019-05-20 02:49:08 +00:00
When creating a `AggregatingMergeTree` table the same [clauses ](mergetree.md ) are required, as when creating a `MergeTree` table.
2018-03-25 02:04:22 +00:00
2020-03-20 10:10:48 +00:00
< details markdown = "1" >
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
< summary > Deprecated Method for Creating a Table< / summary >
!!! 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.
2017-12-28 15:13:23 +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 [=] AggregatingMergeTree(date-column [, sampling_expression], (primary, key), index_granularity)
2017-12-28 15:13:23 +00:00
```
2018-10-19 11:25:22 +00:00
All of the parameters have the same meaning as in `MergeTree` .
< / details >
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
## SELECT and INSERT {#select-and-insert}
2017-12-28 15:13:23 +00:00
2018-12-12 17:28:00 +00:00
To insert data, use [INSERT SELECT ](../../query_language/insert_into.md ) query with aggregate -State- functions.
2018-10-19 11:25:22 +00:00
When selecting data from `AggregatingMergeTree` table, use `GROUP BY` clause and the same aggregate functions as when inserting data, but using `-Merge` suffix.
2017-12-28 15:13:23 +00:00
2020-03-19 11:51:22 +00:00
In the results of `SELECT` query, the values of `AggregateFunction` type have implementation-specific binary representation for all of the ClickHouse output formats. If dump data into, for example, `TabSeparated` format with `SELECT` query then this dump can be loaded back using `INSERT` query.
2018-01-19 14:36:40 +00:00
2020-03-20 10:10:48 +00:00
## Example of an Aggregated Materialized View {#example-of-an-aggregated-materialized-view}
2018-10-19 11:25:22 +00:00
`AggregatingMergeTree` materialized view that watches the `test.visits` table:
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
CREATE MATERIALIZED VIEW test.basic
2018-10-19 11:25:22 +00:00
ENGINE = AggregatingMergeTree() PARTITION BY toYYYYMM(StartDate) ORDER BY (CounterID, StartDate)
2017-12-28 15:13:23 +00:00
AS SELECT
CounterID,
StartDate,
sumState(Sign) AS Visits,
uniqState(UserID) AS Users
FROM test.visits
GROUP BY CounterID, StartDate;
```
2020-03-19 11:51:22 +00:00
Inserting data into the `test.visits` table.
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
INSERT INTO test.visits ...
```
2018-10-19 11:25:22 +00:00
The data are inserted in both the table and view `test.basic` that will perform the aggregation.
To get the aggregated data, we need to execute a query such as `SELECT ... GROUP BY ...` from the view `test.basic` :
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT
StartDate,
sumMerge(Visits) AS Visits,
uniqMerge(Users) AS Users
FROM test.basic
GROUP BY StartDate
ORDER BY StartDate;
```
2020-01-30 10:34:55 +00:00
[Original article ](https://clickhouse.tech/docs/en/operations/table_engines/aggregatingmergetree/ ) <!--hide-->