我确实在文档中发现了几个通过将参数传递给 ENGINE 部分来创建主键的示例。 但是我没有找到关于 ENGINE 的任何参数的任何描述,它的含义以及如何创建主键。 提前致谢。将此信息添加到它不存在的文档中会很棒。
最佳答案
MergeTree 存储引擎系列支持主键。 https://clickhouse.tech/docs/en/engines/table_engines/mergetree_family/mergetree/
Note that for most serious tasks, you should use engines from the MergeTree family.
它被指定为存储引擎的参数。
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.
Example without sampling support:
MergeTree(EventDate, (CounterID, EventDate), 8192)
Example with sampling support:
MergeTree(EventDate, intHash32(UserID), (CounterID, EventDate, intHash32(UserID)), 8192)
因此,(CounterID, EventDate)
或 (CounterID, EventDate, intHash32(UserID))
是这些示例中的主键。
在使用ReplicatedMergeTree时,还有两个额外的参数,标识shard和replica。
主键在创建表时指定,以后无法更改。
尽管名称如此,但主键并不是唯一的。它只是定义数据的排序顺序以最佳方式处理范围查询。您可以将具有相同主键值的许多行插入到一个表中。
关于database - 如何在 ClickHouse 中创建主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684805/