The engine inherits from MergeTree. The difference is that when merging data parts forDocumentation Index
Fetch the complete documentation index at: https://private-7c7dfe99-page-updates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
SummingMergeTree tables ClickHouse replaces all the rows with the same primary key (or more accurately, with the same sorting key) with one row which contains summed 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.
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.
Creating a table
Parameters of SummingMergeTree
Columns
columns - a tuple with the names of columns where values will be summed. Optional parameter.
The columns must be of a numeric type and must not be in the partition or sorting key.
If columns is not specified, ClickHouse summarizes the values in all columns with a numeric data type that are not in the sorting key.
Query clauses
When creating aSummingMergeTree table the same clauses are required, as when creating a MergeTree table.
Usage example
Consider the following table:sum and GROUP BY clause in the query.
Data processing
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. 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() and GROUP BY clause should be used in a query as described in the example above.
Common rules for summation
The values in the columns with the numeric data type are summed. The set of columns is defined by the parametercolumns.
If the values were 0 in all of the columns for summation, the row is deleted.
If column is not in the primary key and is not summed, an arbitrary value is selected from the existing ones.
The values are not summed for columns in the primary key.
The summation in the AggregateFunction columns
For columns of AggregateFunction type ClickHouse behaves as AggregatingMergeTree engine aggregating according to the function.Nested structures
Table can have nested data structures that are processed in a special way. If the name of a nested table ends withMap and it contains at least two columns that meet the following criteria:
- the first column is numeric
(*Int*, Date, DateTime)or a string(String, FixedString), let’s call itkey, - the other columns are arithmetic
(*Int*, Float32/64), let’s call it(values...),
key => (values...), and when merging its rows, the elements of two data sets are merged by key with a summation of the corresponding (values...).
Examples:
Map.
For nested data structure, you do not need to specify its columns in the tuple of columns for summation.