• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    Clickhouse数据表、数据分区partition的基本操作代码

    发布者: 福建二哥 | 发布时间: 2025-6-20 09:10| 查看数: 107| 评论数: 0|帖子模式

    1. 数据表的基本操作
    1. 只有MergeTree系列、Merge、Distributed表引擎支持alter操作
    复制代码
    1.1 添加字段
    1. clickhouse1 :)
    2. clickhouse1 :) create table alter_table_test(
    3. :-] id UInt32,
    4. :-] name String,
    5. :-] city String
    6. :-] ) engine = MergeTree()
    7. :-] order by id;
    8. clickhouse1 :)
    9. clickhouse1 :) alter table alter_table_test add column if not exists score Float32 default 8.8 after city;
    10. clickhouse1 :)
    复制代码
    1.2 修改字段数据类型、添加或修改字段默认值
    1. clickhouse1 :)
    2. clickhouse1 :) alter table alter_table_test modify column if exists score Float64 default 0.0;
    3. clickhouse1 :)
    复制代码
    修改前后的字段数据类型需要兼容

    1.3 添加或修改字段备注
    1. clickhouse1 :)
    2. clickhouse1 :) alter table alter_table_test comment column if exists score '分数';
    3. clickhouse1 :)
    复制代码
    1.4 删除字段
    1. clickhouse1 :)
    2. clickhouse1 :) alter table alter_table_test drop column if exists score;
    3. clickhouse1 :)
    复制代码
    1.5 重命名或移动数据表
    1. clickhouse1 :)
    2. clickhouse1 :) rename table default.alter_table_test to default.alter_table_rename_test;
    3. clickhouse1 :)
    复制代码

    • 多个db.tb to db.tb用逗号分隔
    • 如果源表与目标表数据库不一样,则表示移动数据表, 但数据表的移动只能在同一服务器
    • 支持on cluster cluster_name操作

    1.6 清空数据表
    1. clickhouse1 :)
    2. clickhouse1 :) truncate table if exists default.alter_table_rename_test;
    3. clickhouse1 :)
    复制代码
    支持on cluster cluster_name操作

    1.7 insert数据


    2. 数据分区partition的基本操作

    测试表和测试数据的准备
    1. clickhouse1 :)
    2. clickhouse1 :) create table partition_table_test(
    3. :-] id UInt32,
    4. :-] name String,
    5. :-] city String
    6. :-] ) engine = MergeTree()
    7. :-] order by id
    8. :-] partition by city;
    9. clickhouse1 :)
    10. clickhouse1 :) insert into partition_table_test(id, name, city) values(1, 'name1', 'Beijing');
    11. clickhouse1 :) insert into partition_table_test(id, name, city) values(2, 'name2', 'Shanghai');
    12. clickhouse1 :)
    13. clickhouse1 :) create table partition_table_test2(
    14. :-] id UInt32,
    15. :-] name String,
    16. :-] city String
    17. :-] ) engine = ReplacingMergeTree()
    18. :-] order by id
    19. :-] partition by city;
    20. clickhouse1 :)
    复制代码
    2.1 查询数据表partition相关信息
    1. clickhouse1 :)
    2. clickhouse1 :) select database, table, partition, partition_id, name, path from system.parts where database = 'default' and table = 'partition_table_test';
    3. ┌─database─┬─table────────────────┬─partition─┬─partition_id─────────────────────┬─name───────────────────────────────────┬─path────────────────────────────────────────────────────────────────────────────────────────────────────┐
    4. │ default  │ partition_table_test │ Shanghai  │ 6a9748c898bf80cb661db240706867aa │ 6a9748c898bf80cb661db240706867aa_2_2_0 │ /root/clickhouse/store/9eb/9ebd4336-b065-48ac-9ebd-4336b06588ac/6a9748c898bf80cb661db240706867aa_2_2_0/ │
    5. │ default  │ partition_table_test │ Beijing   │ 8d2db6c332407299b732139fd8a261c0 │ 8d2db6c332407299b732139fd8a261c0_1_1_0 │ /root/clickhouse/store/9eb/9ebd4336-b065-48ac-9ebd-4336b06588ac/8d2db6c332407299b732139fd8a261c0_1_1_0/ │
    6. └──────────┴──────────────────────┴───────────┴──────────────────────────────────┴────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
    7. clickhouse1 :)
    复制代码
    一个partition_id下面存在多个name

    2.2 删除partition
    1. clickhouse1 :)
    2. clickhouse1 :) alter table partition_table_test drop partition 'Beijing'
    3. :-] ;
    4. clickhouse1 :)
    5. clickhouse1 :) select * from partition_table_test;
    6. ┌─id─┬─name──┬─city─────┐
    7. │  2 │ name2 │ Shanghai │
    8. └────┴───────┴──────────┘
    9. clickhouse1 :)
    复制代码
    上面我们删除了城市为Beijing的partition,然后再通过insert插入新的数据,就间接实现了数据更新

    2.3 复制partition
    1. clickhouse1 :)
    2. clickhouse1 :) alter table partition_table_test2 replace partition 'Shanghai' from partition_table_test;
    3. clickhouse1 :)
    4. clickhouse1 :) select * from partition_table_test2;
    5. ┌─id─┬─name──┬─city─────┐
    6. │  2 │ name2 │ Shanghai │
    7. └────┴───────┴──────────┘
    8. clickhouse1 :)
    复制代码

    • 将A表的数据partition,复制到B表的条件:两张表字段结构完全相同
      两张表partition by、order by一样
    • 会删除目标表partition_table_test2原来的城市Shanghai partition

    2.4 将partition中某一列的数据变为默认值
    1. clickhouse1 :)
    2. clickhouse1 :) alter table partition_table_test clear column name in partition 'Shanghai';
    3. clickhouse1 :)
    4. clickhouse1 :) select * from partition_table_test;
    5. ┌─id─┬─name─┬─city─────┐
    6. │  2 │      │ Shanghai │
    7. └────┴──────┴──────────┘
    8. clickhouse1 :)
    复制代码

    • 变更字段不能为primary key、order by、partition by定义的字段
    • 如果该字段未声明默认值,则以字段数据类型的默认值为准

    2.5 partition的卸载和装载
    1. clickhouse1 :)
    2. clickhouse1 :) alter table partition_table_test detach partition 'Shanghai';
    3. clickhouse1 :)
    4. clickhouse1 :) select * from partition_table_test;
    5. SELECT *
    6. FROM partition_table_test
    7. Query id: 45460933-7b2e-4389-a056-85d3d75184a8
    8. Ok.
    9. 0 rows in set. Elapsed: 0.005 sec.
    10. clickhouse1 :)
    11. clickhouse1 :) alter table partition_table_test attach partition 'Shanghai';
    12. clickhouse1 :)
    13. clickhouse1 :) select * from partition_table_test;
    14. ┌─id─┬─name─┬─city─────┐
    15. │  2 │      │ Shanghai │
    16. └────┴──────┴──────────┘
    17. clickhouse1 :)
    复制代码

    • detach后,该分区目录被移动到数据表目录的detached目录下
    • clickhouse除了能对detached目录下的分区目录执行attach命令, 不能执行其它操作
    • attach则将detached目录下的分区目录重新移回去
    到此这篇关于Clickhouse数据表、数据分区partition的基本操作的文章就介绍到这了,更多相关Clickhouse 数据分区partition内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/database/305331bjx.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表