如何在ClickHouse中创建主键 [英] How to create primary keys in ClickHouse

查看:6608
本文介绍了如何在ClickHouse中创建主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在文档中发现了几个示例,这些示例通过将参数传递给ENGINE部分来创建主键。
但是我没有找到关于ENGINE的任何参数的说明,它的含义以及如何创建主键。
预先感谢。将此信息添加到不存在的文档中将非常有用。

I did found few examples in the documentation where primary keys are created by passing parameters to ENGINE section. But I did not found any description about any argument to ENGINE, what it means and how do I create a primary key. Thanks in advance. It would be great to add this info to the documentation it it's not present.

推荐答案

MergeTree存储引擎家族支持主键。
https://clickhouse.tech/docs/en/engines/ table_engines / mergetree_family / mergetree /

Primary key is supported for MergeTree storage engines family. https://clickhouse.tech/docs/en/engines/table_engines/mergetree_family/mergetree/


请注意,对于大多数严肃的任务,应使用
MergeTree系列的引擎。

Note that for most serious tasks, you should use engines from the MergeTree family.

它被指定为存储引擎的参数。

It is specified as parameters to storage engine.


引擎接受参数:日期类型列的名称,其中包含日期,采样表达式(可选),定义表主键的元组和索引粒度。

The engine accepts parameters: the name of a Date type column containing the date, a sampling expression (optional), a tuple that defines the table's primary key, and the index granularity.

不支持采样的示例:


MergeTree(EventDate, (CounterID, EventDate), 8192)



具有采样支持的示例:

Example with sampling support:


MergeTree(EventDate, intHash32(UserID), (CounterID, EventDate, intHash32(UserID)), 8192)

所以, (CounterID,EventDate)(CounterID,EventDate,intHash32(UserID))是这些示例中的主键。

So, (CounterID, EventDate) or (CounterID, EventDate, intHash32(UserID)) is primary key in these examples.

使用ReplicatedMergeTree时,还有两个附加参数,用于标识分片和副本。

When using ReplicatedMergeTree, there are also two additional parameters, identifying shard and replica.

https://clickhouse.tech/docs/en/engines/table_engines/mergetree_family/replication/#creating-replicated-tables

主键是在创建表时指定的,以后不能更改。

Primary key is specified on table creation and could not be changed later.

尽管名称,主键也不是唯一的。它只是定义数据的排序顺序以最佳方式处理范围查询。您可以在表中插入许多具有相同主键值的行。

Despite the name, primary key is not unique. It just defines sort order of data to process range queries in optimal way. You could insert many rows with same value of primary key to a table.

这篇关于如何在ClickHouse中创建主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆