是否可以更改现有 clickhouse table 的 table 引擎?
Is it possible to change a table engine of an existed clickhouse table?
是否可以像 MySQL 那样在 Clickhouse table 中更改 table 引擎,像这样:
CREATE TABLE example_table (id UInt32, data String) ENGINE=MergeTree() ORDER BY id;
ALTER example_table ENGINE=SummingMergeTree();
因为我没有在文档中找到这样的功能。
如果不可能,是否有任何计划在不久的将来实现它,或者是什么架构限制阻止这样做?
可以通过多种方式更改引擎。
但是无法更改 PARTITION BY / ORDER BY。这就是为什么它没有明确记录的原因。所以在 99.99999% 的情况下它没有任何意义。 SummingMergeTree 使用 table 的 ORDER BY
作为折叠规则,现有的 ORDER BY
通常不适合。
这是其中一种方法的示例(不太老套的一种),
(您可以将分区从一个 table 复制到另一个,这几乎是免费操作,它利用 FS 硬链接并且不复制真实数据)。 (COW -- 写时复制)。
CREATE TABLE example_table (id UInt32, data Float64)
ENGINE=MergeTree() ORDER BY id;
Insert into example_table values(1,1), (1,1), (2,1);
CREATE TABLE example_table1 (id UInt32, data Float64)
ENGINE=SummingMergeTree() ORDER BY id;
-- this does not copy any data (instant & almost free command)
alter table example_table1 attach partition tuple() from example_table;
SELECT * FROM example_table1;
┌─id─┬─data─┐
│ 1 │ 1 │
│ 1 │ 1 │
│ 2 │ 1 │
└────┴──────┘
optimize table example_table1 final;
select * from example_table1;
┌─id─┬─data─┐
│ 1 │ 2 │
│ 2 │ 1 │
└────┴──────┘
另一种方法(编辑元数据文件,如果 table 复制了,还 ZK 记录)
detach table example_table;
vi /var/lib/clickhouse/metadata/default/example_table.sql
replace MergeTree with SummingMergeTree
attach table example_table;
SHOW CREATE TABLE example_table
┌─statement──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ CREATE TABLE default.example_table
(
`id` UInt32,
`data` Float64
)
ENGINE = SummingMergeTree
ORDER BY id
SETTINGS index_granularity = 8192 │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
SELECT * FROM example_table;
┌─id─┬─data─┐
│ 1 │ 1 │
│ 1 │ 1 │
│ 2 │ 1 │
└────┴──────┘
optimize table example_table final;
SELECT * FROM example_table;
┌─id─┬─data─┐
│ 1 │ 2 │
│ 2 │ 1 │
└────┴──────┘
是否可以像 MySQL 那样在 Clickhouse table 中更改 table 引擎,像这样:
CREATE TABLE example_table (id UInt32, data String) ENGINE=MergeTree() ORDER BY id;
ALTER example_table ENGINE=SummingMergeTree();
因为我没有在文档中找到这样的功能。
如果不可能,是否有任何计划在不久的将来实现它,或者是什么架构限制阻止这样做?
可以通过多种方式更改引擎。
但是无法更改 PARTITION BY / ORDER BY。这就是为什么它没有明确记录的原因。所以在 99.99999% 的情况下它没有任何意义。 SummingMergeTree 使用 table 的 ORDER BY
作为折叠规则,现有的 ORDER BY
通常不适合。
这是其中一种方法的示例(不太老套的一种), (您可以将分区从一个 table 复制到另一个,这几乎是免费操作,它利用 FS 硬链接并且不复制真实数据)。 (COW -- 写时复制)。
CREATE TABLE example_table (id UInt32, data Float64)
ENGINE=MergeTree() ORDER BY id;
Insert into example_table values(1,1), (1,1), (2,1);
CREATE TABLE example_table1 (id UInt32, data Float64)
ENGINE=SummingMergeTree() ORDER BY id;
-- this does not copy any data (instant & almost free command)
alter table example_table1 attach partition tuple() from example_table;
SELECT * FROM example_table1;
┌─id─┬─data─┐
│ 1 │ 1 │
│ 1 │ 1 │
│ 2 │ 1 │
└────┴──────┘
optimize table example_table1 final;
select * from example_table1;
┌─id─┬─data─┐
│ 1 │ 2 │
│ 2 │ 1 │
└────┴──────┘
另一种方法(编辑元数据文件,如果 table 复制了,还 ZK 记录)
detach table example_table;
vi /var/lib/clickhouse/metadata/default/example_table.sql
replace MergeTree with SummingMergeTree
attach table example_table;
SHOW CREATE TABLE example_table
┌─statement──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ CREATE TABLE default.example_table
(
`id` UInt32,
`data` Float64
)
ENGINE = SummingMergeTree
ORDER BY id
SETTINGS index_granularity = 8192 │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
SELECT * FROM example_table;
┌─id─┬─data─┐
│ 1 │ 1 │
│ 1 │ 1 │
│ 2 │ 1 │
└────┴──────┘
optimize table example_table final;
SELECT * FROM example_table;
┌─id─┬─data─┐
│ 1 │ 2 │
│ 2 │ 1 │
└────┴──────┘