Clickhouse的基本sql语句

1,建库语句

bash 复制代码
create database if not exists mytestdb on cluster default_cluster;

注意:用户在集群中创建数据库或者表时使用ON CLUSTER语句,保证各个ClickHouse节点上数据库、表的元信息相同。

2,建表

bash 复制代码
-- 创建复制表
create table mytestdb.test_log3 on cluster default_cluster(id UInt32,user_id String,app_id String,create_time String) ENGINE=ReplicatedMergeTree('/clickhouse/tables/{shard}/mytestdb.test_log3','{replica}') order by id;

-- 创建分布式表
create table mytestdb.test_log3_all on cluster default_cluster as mytestdb.test_log3 ENGINE=Distributed('default_cluster','mytestdb','test_log3',rand());

注意:需要同时建本地表和分布式表,建表时需要加cluster

3,查询数据

bash 复制代码
select * from mytestdb.test_log3_all

注意: 查询写入都用分布式表, 写入也可以用本地表,结合replied复制的特性也会自己在副本表复制数据的。

通常都可以使用这个引擎:

ReplicatedMergeTree(/clickhouse/tables/{shard}/default/test','{replica}');

4,删除数据

bash 复制代码
alter table xbtx.xbtx_func_tb on cluster default_cluster delete where app_id='com.sinosure.xbt';

注意:删除时不要操作分布式表,操作本地表

5,更新数据

bash 复制代码
alter table xbtx.xbtx_func_tb on cluster default_cluster update func_name='公文' where id='gongwen';

6,新增数据

bash 复制代码
insert into xbtx.xbtx_func_tb_all(id,func_name,app_id) values('xxxx','xxxx','xxxx');

7,删除表

bash 复制代码
-- 删除本地表:
drop table mytestdb.xbtx_app_log_tb on cluster default_cluster no delay;
-- 删除分布式表:
drop table mytestdb.xbtx_app_log_tb_all on cluster default_cluster no delay;

注意: 删除表时需要加on cluster

相关推荐
武帝为此17 分钟前
【Redis 数据库介绍】
数据库·redis·缓存
Elastic 中国社区官方博客29 分钟前
Elasticsearch:数据脱节如何破坏现代调查
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
Dxy123931021641 分钟前
MySQL性能优化深度解析
数据库·mysql·性能优化
铁锚1 小时前
Redis中KEYS命令的潜在风险与遍历建议
数据库·redis·缓存
清晓粼溪1 小时前
MySQL-01:基础知识
数据库·mysql
贝塔实验室1 小时前
Altium Designer 集成库介绍
arm开发·数据库·驱动开发·硬件工程·射频工程·基带工程·精益工程
哈哈老师啊1 小时前
Springboot学生接送服务平台8rzvo(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue图书商城系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
思成不止于此1 小时前
MySQL 基础核心知识点全梳理:从入门到实战
数据库·笔记·学习·mysql
爬山算法2 小时前
Redis(168) 如何使用Redis实现会话管理?
java·数据库·redis