提交 533665c9 编写于 作者: B BayoNet 提交者: Ivan Blinkov

DOCAPI-4552: EN review of VersionedCollapsingMergeTree topic (#4395)

上级 edff16aa
......@@ -3,12 +3,12 @@
This engine:
- Allows quick writing of continually changing states of objects.
- Deletes old states of objects in the background. It causes to significant reduction of the volume of storage.
- Allows quick writing of object states that are continually changing.
- Deletes old object states in the background. This significantly reduces the volume of storage.
See the section [Collapsing](#collapsing) for details.
The engine inherits from [MergeTree](mergetree.md#table_engines-mergetree) and adds the logic of rows collapsing to data parts merge algorithm. `VersionedCollapsingMergeTree` solves the same problem as the [CollapsingMergeTree](collapsingmergetree.md) but uses another algorithm of collapsing. It allows inserting the data in any order with multiple threads. The particular `Version` column helps to collapse the rows properly even if they are inserted in the wrong order. `CollapsingMergeTree` allows only strictly consecutive insertion.
The engine inherits from [MergeTree](mergetree.md#table_engines-mergetree) and adds the logic for collapsing rows to the algorithm for merging data parts. `VersionedCollapsingMergeTree` serves the same purpose as [CollapsingMergeTree](collapsingmergetree.md) but uses a different collapsing algorithm that allows inserting the data in any order with multiple threads. In particular, the `Version` column helps to collapse the rows properly even if they are inserted in the wrong order. In contrast, `CollapsingMergeTree` allows only strictly consecutive insertion.
## Creating a Table
......@@ -25,7 +25,7 @@ CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
[SETTINGS name=value, ...]
```
For a description of query parameters, see [query description](../../query_language/create.md).
For a description of query parameters, see the [query description](../../query_language/create.md).
**Engine Parameters**
......@@ -35,20 +35,20 @@ VersionedCollapsingMergeTree(sign, version)
- `sign` — Name of the column with the type of row: `1` is a "state" row, `-1` is a "cancel" row.
Column data type should be `Int8`.
The column data type should be `Int8`.
- `version` — Name of the column with the version of object state.
- `version` — Name of the column with the version of the object state.
Column data type should be `UInt*`.
The column data type should be `UInt*`.
**Query Clauses**
When creating a `VersionedCollapsingMergeTree` table, the same [clauses](mergetree.md) are required, as when creating a `MergeTree` table.
When creating a `VersionedCollapsingMergeTree` table, the same [clauses](mergetree.md) are required as when creating a `MergeTree` table.
<details markdown="1"><summary>Deprecated Method for Creating a Table</summary>
!!! attention
Do not use this method in new projects and, if possible, switch the old projects to the method described above.
Do not use this method in new projects. If possible, switch the old projects to the method described above.
```sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
......@@ -59,15 +59,15 @@ CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
) ENGINE [=] VersionedCollapsingMergeTree(date-column [, sampling_expression], (primary, key), index_granularity, sign, version)
```
All of the parameters excepting `sign` and `version` have the same meaning as in `MergeTree`.
All of the parameters except `sign` and `version` have the same meaning as in `MergeTree`.
- `sign` — Name of the column with the type of row: `1` — "state" row, `-1` "cancel" row.
- `sign` — Name of the column with the type of row: `1` is a "state" row, `-1` is a "cancel" row.
Column Data Type — `Int8`.
- `version` — Name of the column with the version of object state.
- `version` — Name of the column with the version of the object state.
Column data type should be `UInt*`.
The column data type should be `UInt*`.
</details>
......@@ -75,11 +75,11 @@ All of the parameters excepting `sign` and `version` have the same meaning as in
### Data
Consider the situation where you need to save continually changing data for some object, it is reasonable to have one row for an object and update it at any change. Update operation is expensive and slow for DBMS because it requires rewriting of the data in the storage. If you need to write data quickly, update not acceptable, but you can write the changes of an object sequentially as follows.
Consider a situation where you need to save continually changing data for some object. It is reasonable to have one row for an object and update the row whenever there are changes. However, the update operation is expensive and slow for a DBMS because it requires rewriting the data in the storage. Update is not acceptable if you need to write data quickly, but you can write the changes to an object sequentially as follows.
Use the particular column `Sign` when writing row. If `Sign = 1` it means that the row is a state of an object, let's call it "state" row. If `Sign = -1` it means the cancellation of the state of an object with the same attributes, let's call it "cancel" row. Also, use the particular column `Version` which should identify each state of an object with a separate number.
Use the `Sign` column when writing the row. If `Sign = 1` it means that the row is a state of an object (let's call it the "state" row). If `Sign = -1` it indicates the cancellation of the state of an object with the same attributes (let's call it the "cancel" row). Also use the `Version` column, which should identify each state of an object with a separate number.
For example, we want to calculate how much pages users checked at some site and how long they were there. At some moment of time we write the following row with the state of user activity:
For example, we want to calculate how many pages users visited on some site and how long they were there. At some point in time we write the following row with the state of user activity:
```
┌──────────────UserID─┬─PageViews─┬─Duration─┬─Sign─┬─Version─┐
......@@ -87,7 +87,7 @@ For example, we want to calculate how much pages users checked at some site and
└─────────────────────┴───────────┴──────────┴──────┴─────────┘
```
At some moment later we register the change of user activity and write it with the following two rows.
At some point later we register the change of user activity and write it with the following two rows.
```
┌──────────────UserID─┬─PageViews─┬─Duration─┬─Sign─┬─Version─┐
......@@ -96,11 +96,11 @@ At some moment later we register the change of user activity and write it with t
└─────────────────────┴───────────┴──────────┴──────┴─────────┘
```
The first row cancels the previous state of the object (user). It should copy all of the fields of the canceled state excepting `Sign`.
The first row cancels the previous state of the object (user). It should copy all of the fields of the canceled state except `Sign`.
The second row contains the current state.
As we need only the last state of user activity, the rows
Because we need only the last state of user activity, the rows
```
┌──────────────UserID─┬─PageViews─┬─Duration─┬─Sign─┬─Version─┐
......@@ -109,31 +109,31 @@ As we need only the last state of user activity, the rows
└─────────────────────┴───────────┴──────────┴──────┴─────────┘
```
can be deleted collapsing the invalid (old) state of an object. `VesionedCollapsingMergeTree` does this while merging of the data parts.
can be deleted, collapsing the invalid (old) state of the object. `VersionedCollapsingMergeTree` does this while merging the data parts.
Why we need 2 rows for each change read in the "Algorithm" paragraph.
To find out why we need two rows for each change, see "Algorithm".
**Peculiar properties of such approach**
**Notes on Usage**
1. The program that writes the data should remember the state of an object to be able to cancel it. "Cancel" string should be the copy of "state" string with the opposite `Sign`. It increases the initial size of storage but allows to write the data quickly.
2. Long growing arrays in columns reduce the efficiency of the engine due to load for writing. The more straightforward data, the higher efficiency.
3. `SELECT` results depend strongly on the consistency of object changes history. Be accurate when preparing data for inserting. You can get unpredictable results in inconsistent data, for example, negative values for non-negative metrics such as session depth.
1. The program that writes the data should remember the state of an object in order to cancel it. The "cancel" string should be a copy of the "state" string with the opposite `Sign`. This increases the initial size of storage but allows to write the data quickly.
2. Long growing arrays in columns reduce the efficiency of the engine due to the load for writing. The more straightforward the data, the better the efficiency.
3. `SELECT` results depend strongly on the consistency of the history of object changes. Be accurate when preparing data for inserting. You can get unpredictable results with inconsistent data, such as negative values for non-negative metrics like session depth.
### Algorithm
When ClickHouse merges data parts, it deletes each pair of rows having the same primary key and version and different `Sign`. The order of rows does not matter.
When ClickHouse merges data parts, it deletes each pair of rows that have the same primary key and version and different `Sign`. The order of rows does not matter.
When ClickHouse merges data parts, it orders rows by the primary key. If the `Version` column is not in the primary key, ClickHouse adds it to the primary key implicitly as the last field and use for ordering.
When ClickHouse inserts data, it orders rows by the primary key. If the `Version` column is not in the primary key, ClickHouse adds it to the primary key implicitly as the last field and uses it for ordering.
## Selecting Data
ClickHouse doesn't guarantee that all of the rows with the same primary key will be in the same resulting data part and even on the same physical server. It's true for writing the data and for subsequent merging of the data parts. Also, ClickHouse process `SELECT` queries with multiple threads, and it can not predict the order of rows in the result. So the aggregation is required if there is a need to get completely "collapsed" data from `VersionedCollapsingMergeTree` table.
ClickHouse doesn't guarantee that all of the rows with the same primary key will be in the same resulting data part or even on the same physical server. This is true both for writing the data and for subsequent merging of the data parts. In addition, ClickHouse processes `SELECT` queries with multiple threads, and it cannot predict the order of rows in the result. This means that aggregation is required if there is a need to get completely "collapsed" data from a `VersionedCollapsingMergeTree` table.
To finalize collapsing write a query with `GROUP BY` clause and aggregate functions that account for the sign. For example, to calculate quantity, use `sum(Sign)` instead of `count()`. To calculate the sum of something, use `sum(Sign * x)` instead of `sum(x)`, and so on, and also add `HAVING sum(Sign) > 0`.
To finalize collapsing, write a query with a `GROUP BY` clause and aggregate functions that account for the sign. For example, to calculate quantity, use `sum(Sign)` instead of `count()`. To calculate the sum of something, use `sum(Sign * x)` instead of `sum(x)`, and add `HAVING sum(Sign) > 0`.
The aggregates `count`, `sum` and `avg` could be calculated this way. The aggregate `uniq` could be calculated if an object has at list one state not collapsed. The aggregates `min` and `max` could not be calculated because `VersionedCollapsingMergeTree` does not save values history of the collapsed states.
The aggregates `count`, `sum` and `avg` can be calculated this way. The aggregate `uniq` can be calculated if an object has at least one non-collapsed state. The aggregates `min` and `max` can't be calculated because `VersionedCollapsingMergeTree` does not save the history of values of collapsed states.
If you need to extract the data with "collapsing" but without of aggregation (for example, to check whether rows are present whose newest values match certain conditions), you can use the `FINAL` modifier for the `FROM` clause. This approach is inefficient and should not be used with big tables.
If you need to extract the data with "collapsing" but without aggregation (for example, to check whether rows are present whose newest values match certain conditions), you can use the `FINAL` modifier for the `FROM` clause. This approach is inefficient and should not be used with large tables.
## Example of Use
......@@ -147,7 +147,7 @@ Example data:
└─────────────────────┴───────────┴──────────┴──────┴─────────┘
```
Creation of the table:
Creating the table:
```sql
CREATE TABLE UAct
......@@ -162,7 +162,7 @@ ENGINE = VersionedCollapsingMergeTree(Sign, Version)
ORDER BY UserID
```
Insertion of the data:
Inserting the data:
```sql
INSERT INTO UAct VALUES (4324182021466249494, 5, 146, 1, 1)
......@@ -171,7 +171,7 @@ INSERT INTO UAct VALUES (4324182021466249494, 5, 146, 1, 1)
INSERT INTO UAct VALUES (4324182021466249494, 5, 146, -1, 1),(4324182021466249494, 6, 185, 1, 2)
```
We use two `INSERT` queries to create two different data parts. If we insert the data with one query ClickHouse creates one data part and will not perform any merge ever.
We use two `INSERT` queries to create two different data parts. If we insert the data with a single query, ClickHouse creates one data part and will never perform any merge.
Getting the data:
......@@ -189,11 +189,11 @@ SELECT * FROM UAct
└─────────────────────┴───────────┴──────────┴──────┴─────────┘
```
What do we see and where is collapsing?
With two `INSERT` queries, we created 2 data parts. The `SELECT` query have performed in 2 threads, and we got a random order of rows.
Collapsing not occurred because there was no merge of the data parts yet. ClickHouse merges data part in an unknown moment of time which we can not predict.
What do we see here and where are the collapsed parts?
We created two data parts using two `INSERT` queries. The `SELECT` query was performed in two threads, and the result is a random order of rows.
Collapsing did not occur because the data parts have not been merged yet. ClickHouse merges data parts at an unknown point in time which we cannot predict.
Thus we need aggregation:
This is why we need aggregation:
```sql
SELECT
......@@ -211,7 +211,7 @@ HAVING sum(Sign) > 0
└─────────────────────┴───────────┴──────────┴─────────┘
```
If we do not need aggregation and want to force collapsing, we can use `FINAL` modifier for `FROM` clause.
If we don't need aggregation and want to force collapsing, we can use the `FINAL` modifier for the `FROM` clause.
```sql
SELECT * FROM UAct FINAL
......@@ -222,6 +222,6 @@ SELECT * FROM UAct FINAL
└─────────────────────┴───────────┴──────────┴──────┴─────────┘
```
This way of selecting the data is very inefficient. Don't use it for big tables.
This is a very inefficient way to select data. Don't use it for large tables.
[Original article](https://clickhouse.yandex/docs/en/operations/table_engines/versionedcollapsingmergetree/) <!--hide-->
../../../en/operations/table_engines/versionedcollapsingmergetree.md
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册