【问题标题】:Is it possible to change a table engine of an existed clickhouse table?是否可以更改现有 clickhouse 表的表引擎?
【发布时间】:2021-10-13 09:29:30
【问题描述】:

是否可以像 MySQL 那样更改 clickhouse 表中的表引擎,如下所示:

CREATE TABLE example_table (id UInt32, data String) ENGINE=MergeTree() ORDER BY id;
ALTER example_table ENGINE=SummingMergeTree();

因为我在文档中没有找到这样的功能。

如果不可能,是否有计划在不久的将来实施它,或者哪些架构限制阻止了这样做?

【问题讨论】:

    标签: clickhouse


    【解决方案1】:

    可以通过多种方式更改引擎。

    但不可能更改 PARTITION BY / ORDER BY。这就是为什么它没有明确记录的原因。因此,在 99.99999% 的情况下,它没有任何意义。 SummingMergeTree 使用表的ORDER BY 作为折叠规则,现有的ORDER BY 通常不适合。

    这是一种方法的示例(不那么老套), (您可以将分区从一个表复制到另一个表,这几乎是免费操作,它利用 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 │
    └────┴──────┘
    

    另一种方式(编辑元数据文件,如果表复制了 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 │
    └────┴──────┘
    

    【讨论】:

    • 顺便说一句,您说第一种方法不会复制真实数据,但是当我在优化后尝试从原始表中进行选择时——结果是相同的(3 条记录)并且在这些表的数据目录有不同的文件(我检查了 inode 编号)。
    • en.wikipedia.org/wiki/Hard_link 可以创建多个指向相同数据的文件。 Alter table attach 不会复制数据,而是在现有数据上创建新链接。
    猜你喜欢
    • 1970-01-01
    • 2019-09-22
    • 2020-04-18
    • 2018-01-29
    • 2020-10-08
    • 1970-01-01
    • 2017-12-11
    • 2018-08-30
    • 1970-01-01
    相关资源
    最近更新 更多