Clickhouse 列结构

Clickhouse columns structure

我将统计数据存储在 MySQL 中,想将其迁移到 Clickhouse。

对于一个实体,我有多种统计类型。例如:clicksviewsupvotes

现在我将其存储在 table 中,如下所示:entity_id | datetime | type | value,其中 type 是一个常量整数,对应于上述统计类型之一。

在 Clickhouse 中保持相同的结构还是创建下一个模式更好:entity_id | datetime | clicks | views | upvotes

据我了解,Clickhouse 是面向列的数据库。这是否意味着当我们需要添加新列时,Clickhouse 具有零成本更改?

Is it better to keep the same structure in Clickhouse or to create the next schema

如果 clicks | views | upvotes 与相同的 entity_id 相关,则为每个值创建一列是有意义的。通过这样做,您将 运行 一次查询并获得包含您需要的所有数据的行。

另一方面,如果您将 table 创建为 entity_id | datetime | type | value,那么您将可以灵活地拥有更多统计类型(可能 mouse_enter 作为第四种类型)。同时,您将重复 entity_iddatetime 值,这将消耗您的磁盘使用量,并且您的 table 将有 3 行。如您所见,每个决定之间有一个 trade-off。由您决定选择哪一个。如果您认为只有 clicks | views | upvotes 就足够了,那么使用此架构更有意义。

Clickhouse is column-oriented DB. Does it mean that the Clickhouse has zero-cost altering when we want to add new columns, when it will be necessary

是的,Clickhouse 是 column-oriented,每一列都是磁盘上的一个或多个单独文件。因此,如果您创建一个新列,开销将很少 none。但是,如果您使用 table 突变或更改类型,则更改列将需要时间,具体取决于操作。