AggregatingMergeTree

The engine inherits from 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) with a single row (within a one data part) that stores a combination of states of aggregate functions.

You can use AggregatingMergeTree tables for incremental data aggregation, including for aggregated materialized views.

The engine processes all columns with the following types:

It is appropriate to use AggregatingMergeTree if it reduces the number of rows by orders.

Creating a Table

  1. CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
  2. (
  3. name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
  4. name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
  5. ...
  6. ) ENGINE = AggregatingMergeTree()
  7. [PARTITION BY expr]
  8. [ORDER BY expr]
  9. [SAMPLE BY expr]
  10. [TTL expr]
  11. [SETTINGS name=value, ...]

For a description of request parameters, see request description.

Query clauses

When creating a AggregatingMergeTree table the same clauses are required, as when creating a MergeTree table.

Deprecated Method for Creating a Table

Attention

Do not use this method in new projects and, if possible, switch the old projects to the method described above.

  1. CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
  2. (
  3. name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
  4. name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
  5. ...
  6. ) ENGINE [=] AggregatingMergeTree(date-column [, sampling_expression], (primary, key), index_granularity)

All of the parameters have the same meaning as in MergeTree.

SELECT and INSERT

To insert data, use INSERT SELECT query with aggregate -State- functions.
When selecting data from AggregatingMergeTree table, use GROUP BY clause and the same aggregate functions as when inserting data, but using -Merge suffix.

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.

Example of an Aggregated Materialized View

AggregatingMergeTree materialized view that watches the test.visits table:

  1. CREATE MATERIALIZED VIEW test.basic
  2. ENGINE = AggregatingMergeTree() PARTITION BY toYYYYMM(StartDate) ORDER BY (CounterID, StartDate)
  3. AS SELECT
  4. CounterID,
  5. StartDate,
  6. sumState(Sign) AS Visits,
  7. uniqState(UserID) AS Users
  8. FROM test.visits
  9. GROUP BY CounterID, StartDate;

Inserting data into the test.visits table.

  1. INSERT INTO test.visits ...

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:

  1. SELECT
  2. StartDate,
  3. sumMerge(Visits) AS Visits,
  4. uniqMerge(Users) AS Users
  5. FROM test.basic
  6. GROUP BY StartDate
  7. ORDER BY StartDate;