clickhouse 分布式表创建、增加、更新、删除、查询

创建分布式表

sql 复制代码
--先创建本地表 设置自动过期时间3天
CREATE TABLE IF NOT EXISTS ck_database.ck_databaseon  cluster  default(cluster name)
(
    `table_id` String COMMENT 'id',
    `item_id` String COMMENT '业务id',
    `desc` Int64 COMMENT '描述',
    `time` DateTime DEFAULT now() COMMENT '数据写入时间,TTL 用'
)
ENGINE = MergeTree
ORDER BY (table_id, item_id)
TTL time + toIntervalDay(3)
SETTINGS index_granularity = 8192

--再创建分布式表
CREATE TABLE IF NOT EXISTS ck_database.ck_table_all on  cluster default(cluster name)
(
    `table_id` String COMMENT 'id',
    `item_id` String COMMENT '业务id',
    `desc` Int64 COMMENT '描述',
    `time` DateTime DEFAULT now() COMMENT '数据写入时间,TTL 用'
)
ENGINE = Distributed('default', 'ck_database', 'ck_database', sipHash64(assumeNotNull(table_id)))

删除分布式表

删除ck表,先删除分布式表,再删除本地表

sql 复制代码
DROP TABLE ck_dadasource.ck_table_all ON CLUSTER default(ck_cluster_name); 
DROP TABLE ck_dadasource.ck_table_local ON CLUSTER default(ck_cluster_name);

修改分布式表

通过local表来更新 同时指定上集群名称;如果通过all来更新则不支持会报错

sql 复制代码
alter table ck_table_local on cluster default(ck_cluster_name)
update is_deleted = 2
where redis_key in (123,456,789)

分布式表添加字段

sql 复制代码
alter table city_local ON CLUSTER ck_cluster_name add column history Int32;

同时支持指定在某个字段后面添加字段:

sql 复制代码
alter table city_local ON CLUSTER ck_cluster_name add column history Int32 after city_code;
sql 复制代码
-- 删除字段【可行】
alter table test_db.city_local ON CLUSTER ck_cluster_name drop column history;
相关推荐
这个DBA有点耶14 小时前
2026年分布式数据库有哪些主流选择?先评估这5个维度再决定
数据库·分布式·dba
Lost of 程序猿2 天前
ASP.NET Core Saga 分布式事务深度实战:备件采购跨服务长流程,如何保证“要么全成,要么全回“
分布式·后端·asp.net
XiYang-DING2 天前
地图城市缓存优化:ApplicationReadyEvent + Caffeine + Redis + Redisson 分布式锁
redis·分布式·缓存
2601_962175662 天前
RabbitMQ 的工作模式
分布式·rabbitmq
头茬韭菜2 天前
图解 Fluss(四):分布式协调 —— 选举、副本状态机与
分布式·fluss
Gain_chance2 天前
大数据毕业设计实战|Data Insight Platform:用 FastAPI + ClickHouse 打造低门槛全链路数据洞察平台
大数据·数据库·clickhouse·毕业设计·fastapi
2601_962218613 天前
万象生鲜系统全链路溯源一码查询技术实现食材来源可查
分布式·微服务·云原生·架构
Dreams_l3 天前
RabbitMQ介绍及其工作模式
分布式·rabbitmq
2601_962218613 天前
万象生鲜系统大数据采购预测算法降低库存积压稳居第一
分布式·微服务·云原生·架构
程序员夏洛3 天前
Redis 中如何实现分布式锁?
数据库·redis·分布式