2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/engines/table-engines/mergetree-family/summingmergetree
2022-04-09 13:29:05 +00:00
sidebar_position: 50
sidebar_label: SummingMergeTree
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# SummingMergeTree
2017-12-28 15:13:23 +00:00
2020-06-18 08:24:31 +00:00
The engine inherits from [MergeTree ](../../../engines/table-engines/mergetree-family/mergetree.md#table_engines-mergetree ). The difference is that when merging data parts for `SummingMergeTree` tables ClickHouse replaces all the rows with the same primary key (or more accurately, with the same [sorting key ](../../../engines/table-engines/mergetree-family/mergetree.md )) with one row which contains summarized values for the columns with the numeric data type. If the sorting key is composed in a way that a single key value corresponds to large number of rows, this significantly reduces storage volume and speeds up data selection.
2017-12-28 15:13:23 +00:00
2021-05-27 19:48:20 +00:00
We recommend using the engine together with `MergeTree` . Store complete data in `MergeTree` table, and use `SummingMergeTree` for aggregated data storing, for example, when preparing reports. Such an approach will prevent you from losing valuable data due to an incorrectly composed primary key.
2018-10-19 11:25:22 +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],
...
2019-02-15 08:45:22 +00:00
) ENGINE = SummingMergeTree([columns])
2018-10-19 11:25:22 +00:00
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[SETTINGS name=value, ...]
```
2020-07-09 15:10:35 +00:00
For a description of request parameters, see [request description ](../../../sql-reference/statements/create/table.md ).
2018-10-19 11:25:22 +00:00
2022-06-24 15:13:15 +00:00
### Parameters of SummingMergeTree
2018-10-19 11:25:22 +00:00
2022-06-24 15:13:15 +00:00
#### columns
`columns` - a tuple with the names of columns where values will be summarized. Optional parameter.
2020-03-21 04:11:51 +00:00
The columns must be of a numeric type and must not be in the primary key.
2018-10-19 11:25:22 +00:00
2022-12-14 18:55:42 +00:00
If `columns` is not specified, ClickHouse summarizes the values in all columns with a numeric data type that are not in the primary key.
2018-10-19 11:25:22 +00:00
2022-06-24 15:13:15 +00:00
### Query clauses
2018-10-19 11:25:22 +00:00
2020-06-18 08:24:31 +00:00
When creating a `SummingMergeTree` table the same [clauses ](../../../engines/table-engines/mergetree-family/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
2023-03-27 18:54:05 +00:00
:::note
2022-04-09 13:29:05 +00:00
Do not use this method in new projects and, if possible, switch the old projects to the method described above.
:::
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 [=] SummingMergeTree(date-column [, sampling_expression], (primary, key), index_granularity, [columns])
```
All of the parameters excepting `columns` have the same meaning as in `MergeTree` .
2020-03-21 04:11:51 +00:00
- `columns` — tuple with names of columns values of which will be summarized. 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 >
2020-03-20 10:10:48 +00:00
## Usage Example {#usage-example}
2018-10-19 11:25:22 +00:00
Consider the following table:
2020-03-20 10:10:48 +00:00
``` sql
2018-10-19 11:25:22 +00:00
CREATE TABLE summtt
(
key UInt32,
value UInt32
)
ENGINE = SummingMergeTree()
ORDER BY key
```
Insert data to it:
2020-03-20 10:10:48 +00:00
``` sql
2019-09-23 15:31:46 +00:00
INSERT INTO summtt Values(1,1),(1,2),(2,1)
2017-12-28 15:13:23 +00:00
```
2018-12-25 15:25:43 +00:00
ClickHouse may sum all the rows not completely ([see below](#data-processing)), so we use an aggregate function `sum` and `GROUP BY` clause in the query.
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
SELECT key, sum(value) FROM summtt GROUP BY key
2017-12-28 15:13:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-10-19 11:25:22 +00:00
┌─key─┬─sum(value)─┐
│ 2 │ 1 │
│ 1 │ 3 │
└─────┴────────────┘
```
2020-03-18 18:43:51 +00:00
## Data Processing {#data-processing}
2018-10-19 11:25:22 +00:00
2020-04-08 14:22:25 +00:00
When data are inserted into a table, they are saved as-is. ClickHouse merges the inserted parts of data periodically and this is when rows with the same primary key are summed and replaced with one for each resulting part of data.
2017-12-28 15:13:23 +00:00
2021-05-04 13:12:39 +00:00
ClickHouse can merge the data parts so that different resulting parts of data can consist rows with the same primary key, i.e. the summation will be incomplete. Therefore (`SELECT`) an aggregate function [sum() ](../../../sql-reference/aggregate-functions/reference/sum.md#agg_function-sum ) and `GROUP BY` clause should be used in a query as described in the example above.
2017-12-28 15:13:23 +00:00
2020-04-30 18:19:18 +00:00
### Common Rules for Summation {#common-rules-for-summation}
2017-12-28 15:13:23 +00:00
2018-10-19 11:25:22 +00:00
The values in the columns with the numeric data type are summarized. The set of columns is defined by the parameter `columns` .
2017-12-28 15:13:23 +00:00
2018-10-19 11:25:22 +00:00
If the values were 0 in all of the columns for summation, the row is deleted.
2017-12-28 15:13:23 +00:00
2018-10-19 11:25:22 +00:00
If column is not in the primary key and is not summarized, an arbitrary value is selected from the existing ones.
2018-06-14 11:09:28 +00:00
2018-10-19 11:25:22 +00:00
The values are not summarized for columns in the primary key.
2020-04-30 18:19:18 +00:00
### The Summation in the Aggregatefunction Columns {#the-summation-in-the-aggregatefunction-columns}
2018-10-19 11:25:22 +00:00
2020-06-18 08:24:31 +00:00
For columns of [AggregateFunction type ](../../../sql-reference/data-types/aggregatefunction.md ) ClickHouse behaves as [AggregatingMergeTree ](../../../engines/table-engines/mergetree-family/aggregatingmergetree.md ) engine aggregating according to the function.
2018-10-19 11:25:22 +00:00
2020-03-20 10:10:48 +00:00
### Nested Structures {#nested-structures}
2018-10-19 11:25:22 +00:00
Table can have nested data structures that are processed in a special way.
If the name of a nested table ends with `Map` and it contains at least two columns that meet the following criteria:
2020-03-21 04:11:51 +00:00
- the first column is numeric `(*Int*, Date, DateTime)` or a string `(String, FixedString)` , let’ s call it `key` ,
- the other columns are arithmetic `(*Int*, Float32/64)` , let’ s call it `(values...)` ,
2018-10-19 11:25:22 +00:00
then this nested table is interpreted as a mapping of `key => (values...)` , and when merging its rows, the elements of two data sets are merged by `key` with a summation of the corresponding `(values...)` .
2017-12-28 15:13:23 +00:00
Examples:
2020-03-20 10:10:48 +00:00
``` text
2023-02-17 22:46:30 +00:00
DROP TABLE IF EXISTS nested_sum;
CREATE TABLE nested_sum
(
date Date,
site UInt32,
hitsMap Nested(
browser String,
imps UInt32,
clicks UInt32
)
) ENGINE = SummingMergeTree
PRIMARY KEY (date, site);
INSERT INTO nested_sum VALUES ('2020-01-01', 12, ['Firefox', 'Opera'], [10, 5], [2, 1]);
INSERT INTO nested_sum VALUES ('2020-01-01', 12, ['Chrome', 'Firefox'], [20, 1], [1, 1]);
INSERT INTO nested_sum VALUES ('2020-01-01', 12, ['IE'], [22], [0]);
INSERT INTO nested_sum VALUES ('2020-01-01', 10, ['Chrome'], [4], [3]);
OPTIMIZE TABLE nested_sum FINAL; -- emulate merge
SELECT * FROM nested_sum;
┌───────date─┬─site─┬─hitsMap.browser───────────────────┬─hitsMap.imps─┬─hitsMap.clicks─┐
│ 2020-01-01 │ 10 │ ['Chrome'] │ [4] │ [3] │
│ 2020-01-01 │ 12 │ ['Chrome','Firefox','IE','Opera'] │ [20,11,22,5] │ [1,3,0,1] │
└────────────┴──────┴───────────────────────────────────┴──────────────┴────────────────┘
SELECT
site,
browser,
impressions,
clicks
FROM
(
SELECT
site,
sumMap(hitsMap.browser, hitsMap.imps, hitsMap.clicks) AS imps_map
FROM nested_sum
GROUP BY site
)
ARRAY JOIN
imps_map.1 AS browser,
imps_map.2 AS impressions,
imps_map.3 AS clicks;
┌─site─┬─browser─┬─impressions─┬─clicks─┐
│ 12 │ Chrome │ 20 │ 1 │
│ 12 │ Firefox │ 11 │ 3 │
│ 12 │ IE │ 22 │ 0 │
│ 12 │ Opera │ 5 │ 1 │
│ 10 │ Chrome │ 4 │ 3 │
└──────┴─────────┴─────────────┴────────┘
2017-12-28 15:13:23 +00:00
```
2020-06-18 08:24:31 +00:00
When requesting data, use the [sumMap(key, value) ](../../../sql-reference/aggregate-functions/reference/summap.md ) function for aggregation of `Map` .
2017-12-28 15:13:23 +00:00
2018-10-19 11:25:22 +00:00
For nested data structure, you do not need to specify its columns in the tuple of columns for summation.
2023-04-10 14:23:00 +00:00
## Related Content
- Blog: [Using Aggregate Combinators in ClickHouse ](https://clickhouse.com/blog/aggregate-functions-combinators-in-clickhouse-for-arrays-maps-and-states )